一、添加 yum 源

下载 MySQL 5.7 rpm 包:

1
wget https://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm

安装 rpm 包:

1
sudo rpm -ivh mysql57-community-release-el7-9.noarch.rpm

完成后会在 /etc/yum.repos.d/ 目录下生成两个repo文件:

mysql-community.repo、

mysql-community-source.repo

二、安装 MySQL

进入 /etc/yum.repos.d 目录:

1
cd /etc/yum.repos.d/

执行 yum 安装命令:

1
sudo yum install mysql-server

安装过程中,会有提示确认信息,一般一路输入 y 即可。

如果显示以下输出,则表明安装已成功。

1
2
3
4
5
6
7
Installed:
mysql-community-server.x86_64 0:5.7.37-1.el7

Dependency Installed:
mysql-community-client.x86_64 0:5.7.37-1.el7 mysql-community-common.x86_64 0:5.7.37-1.el7 mysql-community-libs.x86_64 0:5.7.37-1.el7

Complete!

三、启动 MySQL

启动 MySQL:

1
systemctl start mysqld

查看 MySQL 状态:

1
systemctl status mysqld

重启 MySQL:

1
systemctl restart mysqld

停止 MySQL:

1
systemctl stop mysqld

设置开机启动:

1
systemctl enable mysqld

四、登录 MySQL

获取 root 用户首次(临时)登录密码:

1
grep 'temporary password' /var/log/mysqld.log

该命令会显示以下输出,root 用户首次登录密码为 5XetjH9sk9*J :

1
[Note] A temporary password is generated for root@localhost: 5XetjH9sk9*J

登录 MySQL:

1
mysql -u root -p

进入 MySQL 后,需要修改用户临时密码,否则执行命令时会提示错误,如执行 show databases,会显示以下错误信息:

1
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

修改用户密码:

1
ALTER USER 'root'@'localhost' IDENTIFIED BY '这里输入新密码,如2020Super_Star';

查询 MySQL 密码策略:

1
SHOW VARIABLES LIKE 'validate_password%';

该命令会显示以下信息:

1
2
3
4
5
6
7
8
9
10
11
12
+--------------------------------------+--------+
| Variable_name | Value |
+--------------------------------------+--------+
| validate_password_check_user_name | OFF |
| validate_password_dictionary_file | |
| validate_password_length | 8 |
| validate_password_mixed_case_count | 1 |
| validate_password_number_count | 1 |
| validate_password_policy | MEDIUM |
| validate_password_special_char_count | 1 |
+--------------------------------------+--------+
7 rows in set (0.01 sec)

也就是说,密码要求长度要大于8位,包含大写字母、小写字母、数字、标点符号。如:2020Super_Star

(END)