图怪兽_f6e1744f7ebe52226a52e70b2aa2ace8_99710.png
upload successful

本教程为Centos7安装Mysql8的完整教程。

检查当前环境

检查当前环境是否有mysql相关的安装记录,如果有则需要完整移除,避免后续安装过程中出现问题。

查看当前系统是否安装过mysql

rpm -qa | grep -i mysql           

检查是否存在mysql-community-common-8.0.13-1.el7.x86_64mysql80-community-release-el7-1.noarch文件

rpm -qa | grep -i mysql mysql-community-common-8.0.13-1.el7.x86_64 mysql80-community-release-el7-1.noarch

如果存在,则移除

yum remove mysql-community-common-8.0.13-1.el7.x86_64
yum remove mysql-community-release

查找与Mysql有关的文件

[root@localhost program]# find / -name mysql
/etc/selinux/targeted/active/modules/100/mysql
/usr/lib64/mysql
/usr/share/mysql

移除这三个相关文件。

[root@localhost program]# rm -rf /usr/lib64/mysql/
[root@localhost program]# rm -rf /usr/share/mysql/
[root@localhost program]# rm -rf /etc/selinux/targeted/active/modules/100/mysql/
[root@localhost program]# find / -name mysql

使用Yum安装Mysql-Server

yum install mysql-community-server

如果得到如下运行结果,说明没有配置Mysql相关的yum源。

Loaded plugins: fastestmirror, langpacks
Determining fastest mirrors
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
base | 3.6 kB 00:00:00
extras | 2.9 kB 00:00:00
updates | 2.9 kB 00:00:00
(1/4): extras/7/x86_64/primary_db | 243 kB 00:00:00
(2/4): base/7/x86_64/group_gz | 153 kB 00:00:00
(3/4): updates/7/x86_64/primary_db | 12 MB 00:00:01
(4/4): base/7/x86_64/primary_db | 6.1 MB 00:00:02
No package mysql-community-server available.
Error: Nothing to do

配置Mysql8.0的安装源

rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm

安装Mysql8.0

yum --enablerepo=mysql80-community install mysql-community-server

根据提示,一路选择Y即可

得到如下提示,说明安装成功!

Installed:
mysql-community-libs.x86_64 0:8.0.27-1.el7 mysql-community-libs-compat.x86_64 0:8.0.27-1.el7
mysql-community-server.x86_64 0:8.0.27-1.el7

Dependency Installed:
mysql-community-client.x86_64 0:8.0.27-1.el7 mysql-community-client-plugins.x86_64 0:8.0.27-1.el7
mysql-community-common.x86_64 0:8.0.27-1.el7

Replaced:
mariadb-libs.x86_64 1:5.5.68-1.el7

Complete!

启动Mysql

执行下面命令启动mysql

[root@localhost program]# service mysqld start

运行结果如下

Redirecting to /bin/systemctl start mysqld.service
[root@localhost program]#

使用service mysqld status查看mysql启动状态,显示结果如下。

[root@localhost program]# service mysqld status
Redirecting to /bin/systemctl status mysqld.service
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Tue 2021-11-16 19:32:35 CST; 41s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 17884 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 17963 (mysqld)
Status: "Server is operational"
CGroup: /system.slice/mysqld.service
└─17963 /usr/sbin/mysqld

Nov 16 19:32:29 localhost systemd[1]: Starting MySQL Server...
Nov 16 19:32:35 localhost systemd[1]: Started MySQL Server.

设置成开机启动

[root@localhost program]# systemctl enable mysqld
[root@localhost program]# systemctl daemon-reload

登录Mysql

Mysql默认在启动时,默认会生成一个初始密码,使用下面命令获取:

[root@localhost program]# grep "A temporary password" /var/log/mysqld.log 
2021-11-16T11:32:31.731757Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: HE)ZBV0.q:r0

这里得到的密码是:HE)ZBV0.q:r0

在输入mysql -uroot -p连接到Mysql服务端,输入上面这个步骤得到的密码,执行结果如下。

[root@localhost program]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.27

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

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>

修改密码

由于当前密码是Mysql自动生成的,所以在后续操作中,Mysql会提示让我们修改

执行修改命令时,会得到如下提示。

mysql> alter user 'root'@'localhost' IDENTIFIED BY '123456';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
mysql>

简单来说,就是当前的密码策略,不允许我们使用这么简单的密码,但是作为自己练习来说,又不像设置太复杂,咋办? 当然是修改密码策略呗。

执行 show variables like 'validate_password.%';可以查看安全策略设置,但是由于使用初始密码,mysql会给出下面这个提示。

mysql> show variables like 'validate_password.%';
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql>

于是,我们只能先修改成一个稍微复杂一点的

mysql> alter user 'root'@'localhost' IDENTIFIED BY 'root@123456!Q@W';
Query OK, 0 rows affected (0.01 sec)

再次执行 show variables like 'validate_password.%';,得到如下信息

mysql> show variables like 'validate_password.%';
+--------------------------------------+--------+
| Variable_name | Value |
+--------------------------------------+--------+
| validate_password.check_user_name | ON |
| 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)
  • validate_password.length 是密码的最小长度,默认是8,我们把它改成6
    输入:set global validate_password.length=6;
  • validate_password.policy 验证密码的复杂程度,我们把它改成0
    输入:set global validate_password.policy=0;
  • validate_password.check_user_name 用户名检查,用户名和密码不能相同,我们也把它关掉
    输入:set global validate_password.check_user_name=off;

修改完成后,验证修改结果如下。

mysql> show variables like 'validate_password.%';
+--------------------------------------+-------+
| Variable_name | Value |
+--------------------------------------+-------+
| validate_password.check_user_name | OFF |
| validate_password.dictionary_file | |
| validate_password.length | 6 |
| validate_password.mixed_case_count | 1 |
| validate_password.number_count | 1 |
| validate_password.policy | LOW |
| validate_password.special_char_count | 1 |
+--------------------------------------+-------+
7 rows in set (0.01 sec)

再次执行修改密码的语句,就可以修改成功了!

mysql> alter user 'root'@'localhost' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.01 sec)

开启远程访问

默认情况下,Mysql做了安全策略管理,就是不允许白名单以及localhost以外的客户端连接到Mysql-Server。

如果我们要远程连接Mysql,可以通过白名单的方式控制,也可以开启任意匹配。

由于是自己练习使用,我就使用第二种方法(任何一个人都可以连接过来)

mysql> update user set host='%' where user='root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

开放防火墙端口

Mysql默认采用3306作为访问端口。

如果服务器开启了防火墙,其他机器是无法访问到mysql的,因此需要开放3306这个端口允许外部访问

[root@localhost ~]# firewall-cmd --zone=public --add-port=3306/tcp --permanent
success
[root@localhost ~]# firewall-cmd --reload
success