1. 下载MySQL源安装包

1
2
# 下载Mysql源安装包
[root@test1 usr]# wget http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm

2. 安装MySQL源

1
2
3
4
5
6
7
# 安装MySQL源
[root@test1 usr]# yum localinstall mysql57-community-release-el7-8.noarch.rpm
# 检查MySQL源是否安装成功
[root@test1 usr]# yum repolist enabled|grep "mysql.*-community.*"
mysql-connectors-community/x86_64 MySQL Connectors Community 108
mysql-tools-community/x86_64 MySQL Tools Community 90
mysql57-community/x86_64 MySQL 5.7 Community Server 347

3. 安装MySQL

1
2
# 安装MySQL
[root@test1 usr]# yum install mysql-community-server

4. 启动MySQL服务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 设置开机启动
[root@test1 usr]# systemctl enable mysqld
[root@test1 usr]# systemctl daemon-reload
# 启用MySQL服务
[root@test1 usr]# systemctl start mysqld
# 查看MySQL服务状态
[root@test1 usr]# systemctl status mysqld
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since 五 2019-06-28 15:27:15 CST; 25s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 14758 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
Process: 14684 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 14762 (mysqld)
CGroup: /system.slice/mysqld.service
└─14762 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld...

6月 28 15:26:48 test1 systemd[1]: Starting MySQL Server...
6月 28 15:27:15 test1 systemd[1]: Started MySQL Server.

5. 登入MySQL

MySQL安装完成后,在/var/log/mysqld.log文件中给root生成了一个默认密码。通过下面方式找到root默认密码并登入。

1
2
3
4
5
6
7
8
9
# 查询安装生成的临时登录密码
[root@test1 usr]# grep 'temporary password' /var/log/mysqld.log
2019-06-28T07:26:51.584687Z 1 [Note] A temporary password is generated for root@localhost: jiX7)#lcCocn
# 使用临时密码登录
[root@test1 usr]# mysql -u root -p
Enter password: jiX7)#lcCocn
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.26

6. 修改MySQL登入密码

MySQL 5.7默认安装了密码安全检查插件(validate_password),默认密码检查策略要求必须包含:大小写字母、数字和特殊符号,并且长度不能少于8位,否则会提示ERROR 1819 (HY000): Your password does not satisfy the current policy requirements

1
2
3
# 修改密码
mysql> set password for 'root'@'localhost'=password('所要修改的密码');
Query OK, 0 rows affected, 1 warning (0.00 sec)

7. 修改MySQL密码策略

在/etc/my.cnf添加validate_password_policy配置,指定密码策略。

1
2
3
# 选择0(LOW),1(MEDIUM),2(STRONG)其中一种,选择2需要提供密码字典文件
# MySQL官网密码策略详细说明:https://dev.mysql.com/doc/refman/5.7/en/validate-password-options-variables.html#sysvar_validate_password_policy
validate_password_policy=0

如果不需要密码策略,可以在my.cnf文件中添加以下配置禁用即可:

1
validate_password=off

修改完配置文件后,重新启动MySQL服务使配置生效。

1
2
# 重启MySQL服务
[root@test1 etc]# systemctl restart mysqld

默认配置文件路径:

配置文件:/etc/my.cnf
日志文件:/var/log//var/log/mysqld.log
服务启动脚本:/usr/lib/systemd/system/mysqld.service
socket文件:/var/run/mysqld/mysqld.pid

如果忘记root密码,则按以下操作恢复:

在[mysqld]的段中加上一句:skip-grant-tables 保存并且退出vi。

1
2
3
$ mysql  -u root
$ update mysql.user set authentication_string=password('123qwe') where user='root' and Host = 'localhost';
$ flush privileges