MySQL的安装与配置——详细过程
MySQL部署
1.1 下载安装包
[root@node01 ~]# wget https://cdn.mysql.com//archives/mysql-5.7/mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz
1.2 解压安装
二进制安装方式
[root@node01 ~]# tar zxf mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz [root@node01 ~]# ls mysql-5.7.23-linux-glibc2.12-x86_64 bin COPYING docs include lib man README share support-files [root@node01 ~]# mv mysql-5.7.23-linux-glibc2.12-x86_64 /usr/local/mysql [root@node01 ~]#
1.3 配置环境变量
[root@node01 ~]# tail -1 /etc/profile export PATH=$PATH:/usr/local/mysql/bin [root@node01 ~]# source /etc/profile [root@node01 ~]# mysql --version mysql Ver 14.14 Distrib 5.7.23, for linux-glibc2.12 (x86_64) using EditLine wrapper [root@node01 ~]#
1.4 初始化数据库
[root@node01 ~]# mkdir /data/mysql/mysql3306 -p [root@node01 ~]# useradd -M -s /sbin/nologin mysql [root@node01 ~]# chown mysql.mysql -R /data/mysql/mysql3306 [root@node01 mysql]# /usr/local/mysql/bin/mysqld --initialize --basedir=/usr/local/mysql --datadir=/data/mysql/mysql3306 --user=mysql 2019-08-22T09:42:25.450894Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2019-08-22T09:42:26.130690Z 0 [Warning] InnoDB: New log files created, LSN=45790 2019-08-22T09:42:26.338634Z 0 [Warning] InnoDB: Creating foreign key constraint system tables. 2019-08-22T09:42:26.433743Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 26735bac-c4c1-11e9-8f5e-fa809f3a1b00. 2019-08-22T09:42:26.440572Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened. 2019-08-22T09:42:26.441100Z 1 [Note] A temporary password is generated for root@localhost: QNbJyS(Du3aq [root@node01 mysql]# echo $? 0 [root@node01 mysql]#
root用户密码 QNbJyS(Du3aq 如果忘记保持密码,cat /var/log/mysqld.log 可以查看
1.5 创建配置文件
[root@node01 ~]# cat /data/mysql/mysql3306/my.cnf [client] port = 3306 socket = /tmp/mysql3306.sock [mysqld] ft_min_word_len = 1 character-set-server = utf8 datadir=/data/mysql/mysql3306 port = 3306 socket = /tmp/mysql3306.sock skip-external-locking slave-skip-errors=1032,1062 skip-name-resolve max_allowed_packet = 1024M sort_buffer_size = 32M net_buffer_length = 1M read_buffer_size = 8M read_rnd_buffer_size = 8M myisam_sort_buffer_size = 50M query_cache_size = 64M max_connections = 1024 table_open_cache = 64 query_cache_size= 240M query_cache_type=1 #slow_query_log=1 long_query_time =1 expire_logs_days = 5 max_heap_table_size = 128M tmp_table_size = 128M max_heap_table_size=1048576000 server-id = 1 log-bin=mysql-bin binlog_format=ROW innodb_buffer_pool_size = 2G innodb_file_per_table=1 [mysqldump] quick max_allowed_packet = 200M [mysql] no-auto-rehash [isamchk] key_buffer = 20M sort_buffer_size = 20M read_buffer = 2M write_buffer = 2M [myisamchk] key_buffer = 20M sort_buffer_size = 20M read_buffer = 2M write_buffer = 2M [mysqlhotcopy] interactive-timeout [root@apollo-node01 ~]#
1.6 启动数据库
[root@node01 ~]# /usr/local/mysql/bin/mysqld --defaults-file=/data/mysql/mysql3306/my.cnf --user=mysql & [root@node01 ~]# netstat -lnpt |grep mysql tcp6 0 0 :::3306 :::* LISTEN 115262/mysqld [root@node01 mysql]# mysql -S /tmp/mysql3306.sock -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.7.23-log Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> mysql>
1.7 创建管理用户
mysql> grant all privileges on *.* to 'apollo'@'%' identified by '123.com'; ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement. mysql>
原因是MySQL5.7特性要更新root账号密码
mysql> ALTER USER USER() IDENTIFIED BY '123.com'; Query OK, 0 rows affected (0.01 sec) mysql> grant all privileges on *.* to 'apollo'@'%' identified by '123.com'; Query OK, 0 rows affected, 1 warning (0.01 sec) mysql> select user,host from mysql.user; +---------------+-----------+ | user | host | +---------------+-----------+ | apollo | % | | mysql.session | localhost | | mysql.sys | localhost | | root | localhost | +---------------+-----------+ 4 rows in set (0.00 sec) mysql>
到这里完成数据库配置操作