1. 清理

1.1. 卸载旧的

先检查有无旧的

1
2
rpm -qa | grep -i mysql
rpm -qa | grep -i mariadb

有的话就卸载yum remove或者rpm -e --nodeps,总之要清除干净。yum remove不带版本号,rpm -e --nodeps需要带版本号

1
2
3
4
5
6
7
8
rpm -qa | grep -i mysql
mysql-community-client-plugins-8.0.40-1.el7.x86_64
mysql-community-server-8.0.40-1.el7.x86_64
mysql-community-common-8.0.40-1.el7.x86_64
mysql-community-libs-8.0.40-1.el7.x86_64
mysql-community-icu-data-files-8.0.40-1.el7.x86_64
mysql80-community-release-el7-8.noarch
mysql-community-client-8.0.40-1.el7.x86_64
1
2
3
4
5
6
7
yum remove mariadb
yum remove mysql-community-server
# 或者
rpm -e --nodeps MariaDB-compat-10.11.9-1.el7.centos.x86_64
rpm -e --nodeps MariaDB-common-10.11.9-1.el7.centos.x86_64
rpm -e --nodeps mysql-community-server-8.0.40-1.el7.x86_64

1.2. 删除文件夹

1
2
3
4
5
6
7
whereis mysql
mysql: /usr/lib64/mysql

find / -name mysql
/usr/lib64/mysql
/var/lib/mysql
/var/lib/mysql/mysql

然后移除

1
2
3
4
rm -rf /usr/lib64/mysql
rm -rf /var/lib/mysql
rm -rf /var/lib/mysql/mysql
rm -f /var/log/mysqld.log

2. 安装

2.1. 下载安装

1
2
wget https://dev.mysql.com/get/mysql80-community-release-el7-11.noarch.rpm
yum -y install mysql80-community-release-el7-11.noarch.rpm

2.2. 安装工具

1
yum -y install yum-utils

2.3. 重建缓存

1
yum clean all && yum makecache

2.4. 安装MySQL

安装需要不检查证书--nogpgchec,如果追求安全的话还安装证书,再执行以下命令才会成功

1
yum install -y mysql-community-server --nogpgcheck

3. 初始化

3.1. 初始配置

MySQL8开始默认是表的大小写敏感的,如果你想配置不敏感,必须在MySQL初始化的时候配置,否则以后再也无法修改了,只能重装。

/etc/my.cnf中配置

1
2
3
4
5
6
7
8
9
10
11
12
13
[mysqld]
# 大小写不敏感
lower_case_table_names=1
# 服务端默认字符集
character_set_server=utf8mb4
# 连接层默认
collation_server=utf8mb4_unicode_ci
[mysql]
# 数据库默认字符集
default-character-set=utf8mb4
[client]
# 客户端来源数据的默认字符集
default-character-set=utf8mb4

3.2. 启动

不要执行此步骤网上说要执行mysqld 的初始化,/usr/sbin/mysqld --initialize --lower-case-table-names=1实测根据此方案不用加,加了反而会报错

1
2
3
4
# 启动
systemctl start mysqld
# 开机自启
systemctl enable mysqld

我这里出现过启动失败,但是在不同的服务器又有成功的

使用systemctl status mysqld.service查看异常情况

报权限错误Failed to set datadir to '/var/lib/mysql/' (OS errno: 13 - Permission denied)
这里看起来是没有权限,则删除了/var/lib/mysql文件夹下所有的数据就好了rm -rf /var/lib/mysql/*(安装之前本来是删除过的,不知道咋回事)

3.3. 查看默认密码

在日志中查看密码

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

会打印默认密码 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: wyaNYeKI<5Xx
其中wyaNYeKI<5Xx就是密码

3.4. 初始化配置

执行mysql_secure_installation然后配置默认行为

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
51
52
53
54
55
Securing the MySQL server deployment.

Enter password for user root: //输入默认密码
The 'validate_password' component is installed on the server.
The subsequent steps will run with the existing configuration
of the component.
Using existing password for root.

Estimated strength of the password: 100
Change the password for root ? ((Press y|Y for Yes, any other key for No) : y

New password: //输入新密码

Re-enter new password:

Estimated strength of the password: 100
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y //确认,因为不安全,需要确认一下
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y //移除匿名用户
Success.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y //禁止root远程访问,这个一般是禁止的,可以在数据库新建一个账号远程连接
Success.

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y //移除测试数据库
- Dropping test database...
Success.

- Removing privileges on test database...
Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y //立即刷新权限,一般选择y,马上生效
Success.

All done!