由于rhel8(或者centos8、rocky8等)红帽系Linux发行版中mysql数据库默认采用了mysql8版本,而当前对MySQL 5.7 的使用最多。为了兼容之前的 MySQL 5.7 数据库,需要手动安装上一版本的 mysql 数据库
永久链接:http://blog.ryjer.com/posts/a1ec96d713.html
1、通过 yum 仓库安装
1 关闭内置 mysql 8.0 模块
1
2
  | 
sudo dnf remove @mysql
sudo dnf module reset mysql && sudo dnf module disable mysql
  | 
 
屏蔽 yum mysql80 仓库
1
  | 
 sudo yum-config-manager --disable mysql80-community  
  | 
 
2 添加 yum 仓库
下载 noarch 仓库安装包,由于没有 centos8 的,所以使用el7-9 的包
1
  | 
wget https://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm
  | 
 
安装
1
2
3
  | 
rpm -Uvh mysql57-community-release-el7-9.noarch.rpm
# 或者使用 yum
yum localinstall mysql57-community-release-el7-9.noarch.rpm
  | 
 
安装后会产生 仓库文件 /etc/yum.repos.d/mysql-community.repo
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
  | 
[mysql-connectors-community]
name=MySQL Connectors Community
baseurl=http://repo.mysql.com/yum/mysql-connectors-community/el/7/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
[mysql-tools-community]
name=MySQL Tools Community
baseurl=http://repo.mysql.com/yum/mysql-tools-community/el/7/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
# Enable to use MySQL 5.5
[mysql55-community]
name=MySQL 5.5 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.5-community/el/7/$basearch/
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
# Enable to use MySQL 5.6
[mysql56-community]
name=MySQL 5.6 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.6-community/el/7/$basearch/
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
[mysql57-community]
name=MySQL 5.7 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
[mysql80-community]
name=MySQL 8.0 Community Server
baseurl=http://repo.mysql.com/yum/mysql-8.0-community/el/7/$basearch/
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
[mysql-tools-preview]
name=MySQL Tools Preview
baseurl=http://repo.mysql.com/yum/mysql-tools-preview/el/7/$basearch/
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
  | 
 
和 /etc/yum.repos.d/mysql-community-source.repo
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
  | 
[mysql-connectors-community-source]
name=MySQL Connectors Community - Source
baseurl=http://repo.mysql.com/yum/mysql-connectors-community/el/7/SRPMS
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
[mysql-tools-community-source]
name=MySQL Tools Community - Source
baseurl=http://repo.mysql.com/yum/mysql-tools-community/el/7/SRPMS
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
[mysql55-community-source]
name=MySQL 5.5 Community Server - Source
baseurl=http://repo.mysql.com/yum/mysql-5.5-community/el/7/SRPMS
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
[mysql56-community-source]
name=MySQL 5.6 Community Server - Source
baseurl=http://repo.mysql.com/yum/mysql-5.6-community/el/7/SRPMS
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
[mysql57-community-source]
name=MySQL 5.7 Community Server - Source
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/SRPMS
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
[mysql80-community-source]
name=MySQL 8.0 Community Server - Source
baseurl=http://repo.mysql.com/yum/mysql-8.0-community/el/7/SRPMS
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
[mysql-tools-preview-source]
name=MySQL Tools Preview - Source
baseurl=http://repo.mysql.com/yum/mysql-tools-preview/el/7/SRPMS
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
  | 
 
3 安装 mysql 5.7
标准的做法如下,直接从 mysql 官方仓库安装。但是该仓库位于国外,速度极慢,你需要花费大量的时间下载软件包
1
  | 
yum install mysql-community-server -y
  | 
 
这里最新版是 mysql 5.7.35,对应的安装包如下
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
  | 
===================================================================================
 软件包					 架构		 版本						 仓库				 大小
===================================================================================
安装:
 mysql-community-server	 x86_64	 5.7.35-1.el7			 mysql57-community	 174 M
安装依赖关系:
 mysql-community-client	 x86_64	 5.7.35-1.el7			 mysql57-community	 25 M
 mysql-community-common	 x86_64	 5.7.35-1.el7			 mysql57-community	 310 k
 mysql-community-libs	 x86_64	 5.7.35-1.el7			 mysql57-community	 2.4 M
 ncurses-compat-libs	 x86_64	 6.1-7.20180224.el8.1	 baseos				 327 k
事务概要
===================================================================================
  | 
 
查看版本,验证安装
1
  | 
mysql  Ver 14.14 Distrib 5.7.35, for Linux (x86_64) using  EditLine wrapper
  | 
 
4 启动 mysql 服务器
如果你之前已经安装过 mysql,使用dnf 或 yum 卸载后数据文件目录 /var/lib/mysql 会有残留。如果之前的版本高于当前按照的版本,则有可能不兼容导致启动失败。请使用 mysqldump 导出数据后删除数据目录下的文件
1
2
  | 
systemctl start mysqld.service
systemctl status mysqld.service
  | 
 
第一次启动,mysql 安全机制会产生一个默认密码到日志文件中,可用使用以下命令查看日志中产生的默认密码
1
  | 
grep 'temporary password' /var/log/mysqld.log
  | 
 
1
  | 
2021-08-29T11:04:29.752733Z 1 [Note] A temporary password is generated for root@localhost: gH_Q.d3G&N>i
  | 
 
上面,我的密码是 gH_Q.d3G&N>i,可用使用这个密码登录 mysql
然后输入上面的密码即可登录。需要注意的是,mysql 要求登录后立即修改密码,其会阻止任何其他操作
1
  | 
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
  | 
 
mysql 5.7 支持以下语句设置密码,但这并不符合 sql 规范
1
2
3
4
  | 
# 修改当前用户和连接密码
set password = password('新密码');
# 修改指定用户和连接密码
set password for 用户名@localhost = password('新密码'); 
  | 
 
在 mysql 8 中不再支持以上方式,仅支持正规的传统 sql 语句修改密码
1
  | 
ALTER USER 'root'@'localhost' IDENTIFIED BY '新密码';
  | 
 
5 关闭密码策略
可以通过以下语句查看 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.00 sec)
  | 
 
在mysql 配置文件 /etc/my.cnf 的 [mysqld]中添加 validate_password=off 可以直接关闭密码验证插件
1
2
3
4
5
  | 
[mysqld]
# 关闭密码插件
validate_password=off
# 防止密码过期
default_password_lifetime=0
  | 
 
修改完后记得需要重新启动MySQL服务
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
  | 
validate_password相关参数说明:
validate_password_dictionary_file:密码策略文件,策略为STRONG才需要
validate_password_length:密码最少长度
validate_password_mixed_case_count:大小写字符长度,至少1个
validate_password_number_count :数字至少1个
validate_password_special_char_count:特殊字符至少1个上述参数是默认策略MEDIUM的密码检查规则。
validate_password_policy:密码策略,默认为MEDIUM策略,共有如下三种密码策略:
策略 检查规则
0 or LOW Length
1 or MEDIUM Length; numeric, lowercase/uppercase, and special characters
2 or STRONG Length; numeric, lowercase/uppercase, and special characters; dictionary file
临时改:
1. set global validate_password_policy=0;
2. set global validate_password_length=1;
永久改,my.cnf里:
validate_password_policy=0 #0(弱),1(中),2(强)
  | 
 
6 设置默认字符集为 utf8mb4
修改配置文件 /etc/my.cnf ,分别在 [mysqld]中添加 character-set-server=utf8mb4,[client]中添加 default-character-set=utf8mb4、[mysql]中添加default-character-set=utf8mb4。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
  | 
[mysqld]
# 设置数据库字符集为 utf8mb4
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci
[client]
default-character-set=utf8mb4
[mysql]
default-character-set=utf8mb4
  | 
 
然后重启 mysql,完成配置
1
  | 
systemctl restart mysqld.service
  | 
 
2、迁移 mysql 8 到 mysql 5.7
由于 mysql 8表字符集编码和排序规则变化,mysqldump 导出的 mysql 8 数据库归档无法直接使用mysql导入 mysql 5.7,需要进行一些文本替换
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
  | 
# 1. 复制一个副本,保护源文件
cp 原文件 副本文件
# 进行文本替换 utf8mb4_0900_ai_ci 替换为 utf8_general_ci
sed -i 's|utf8mb4_0900_ai_ci|utf8_general_ci|g' 副本文件
# utf8_croatian_ci替换为utf8_general_ci
sed -i 's|utf8_croatian_ci|utf8_general_ci|g' 副本文件
# utf8mb4_general_ci替换为utf8_general_ci
sed -i 's|utf8mb4_general_ci|utf8_general_ci|g' 副本文件
# utf8mb4 替换为 utf8
sed -i 's|utf8mb4|utf8|g' 副本文件
  | 
 
然后就可以导入数据库归档到 mysql 5.7 了
1
  | 
mysql -u 用户名 -p 密码 数据库名 < f
  | 
 
参考文章
将Mysql 8.0数据导入到5.7及以下版本解决方案_程序员zz的博客-CSDN博客