1. 下载安装文件到 /usr/local/src/
[root@honeybee ~]# cd /usr/local/src/
[root@honeybee src]# wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz
2. 创建安装目录、数据存放目录、日志目录
[root@honeybee src]# mkdir -p /usr/local/mysql
[root@honeybee src]# mkdir -p /data/mysql
[root@honeybee src]# mkdir -p /var/log/mysql
3. 解压压缩包到安装目录
[root@honeybee src]# tar -xzf mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz
[root@honeybee src]# mv mysql-5.7.20-linux-glibc2.12-x86_64/* /usr/local/mysql/
4. 创建mysql用户、组
[root@honeybee src]# cd /usr/local/mysql/
[root@honeybee mysql]# groupadd mysql
[root@honeybee mysql]# useradd -r -g mysql mysql
5. 设置目录权限
[root@honeybee mysql]# chown -R mysql /usr/local/mysql/
[root@honeybee mysql]# chown -R mysql:mysql /data/mysql
[root@honeybee mysql]# chgrp -R mysql /data/mysql
[root@honeybee mysql]# chown -R mysql:mysql /var/log/mysql
6. 初始化数据库
[root@honeybee mysql]# bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql
遇到如下错误:
[root@honeybee mysql]# bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql
bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory
安装libaio
[root@honeybee mysql]# yum install libaio
重新运行初始化
[root@honeybee mysql]# bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql
2017-11-12T14:06:27.150049Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2017-11-12T14:06:29.285338Z 0 [Warning] InnoDB: New log files created, LSN=45790
2017-11-12T14:06:29.516354Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2017-11-12T14:06:29.623677Z 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: ae0c4a8c-c7b2-11e7-a1cd-00163e102209.
2017-11-12T14:06:29.627181Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2017-11-12T14:06:29.627612Z 1 [Note] A temporary password is generated for root@localhost: rdCixlilG2*D
记住上面的初始密码:rdCixlilG2*D
7. 添加系统服务
[root@honeybee mysql]# cp -a ./support-files/mysql.server /etc/init.d/mysqld
8. 创建或修改/etc/my.cnf文件
my.cnf中关键配置:
[client]
port = 3306
socket=/tmp/mysql.sock
default-character-set=utf8
[mysqld]
basedir = /usr/local/mysql
datadir = /data/mysql
socket=/tmp/mysql.sock
pid-file = /data/mysql/mysql.pid
character_set_server=utf8
[mysqld_safe]
log-error = /var/log/mysql/error.log
pid-file = /data/mysql/mysql.pid
9. 启动mysql
[root@honeybee mysql]# service mysqld start
Starting MySQL. [ OK ]
10. 登录、更改密码
[root@honeybee mysql]# bin/mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.20-log
Copyright (c) 2000, 2017, 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> set password=password('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)
11. 为root用户添加远程连接的能力
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
mysql> select host,user,authentication_string from mysql.user;
+-----------+---------------+-------------------------------------------+
| host | user | authentication_string |
+-----------+---------------+-------------------------------------------+
| localhost | root | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| localhost | mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| localhost | mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| % | root | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+-----------+---------------+-------------------------------------------+
4 rows in set (0.00 sec)
12. 添加环境变量
修改 /etc/profile文件
PATH=$PATH:/usr/local/mysql/bin
使修改立即生效
[root@honeybee mysql]# source /etc/profile
13. 忘记密码时,可用如下方法重置:
[root@honeybee mysql]# /usr/local/mysql/bin/mysqld_safe --user=root --skip-grant-tables --skip-networking &
[root@honeybee mysql]# mysql -uroot
mysql> use mysql;
mysql> update user set authentication_string=password('123456') where user='root';
mysql> flush privileges;