1.通过wget下载mysql8.0包
(如果包不是最新的则需要注意在安装MYSQL前进行Mysql的GPG升级)
[root@VM-8-4-centos local]# wget https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
2.配置mysql的yum源
[root@VM-8-4-centos local]# sudo rpm -Uvh mysql80-community-release-el7-3.noarch.rpm
3.开始安装Mysql
[root@VM-8-4-centos local]# yum install -y mysql-community-server
注意:上面提到过,通过wget获取的MYSQL包不是最新的前提下有可能报错,这是因为MYSQL的GPG升级了,需要重新获取
如果无这个报错可忽略跳过本步骤
报错信息如下:
Failing package is: mysql-community-client-8.0.28-1.el7.x86_64
GPG Keys are configured as: file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
这个时候需要重执行(2022则为今年的年份),执行完GPG升级,需要重新安装
# rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2023
# yum install -y mysql-community-server
4.安装完成
Installed:
mysql-community-libs.x86_64 0:8.0.29-1.el7 mysql-community-libs-compat.x86_64 0:8.0.29-1.el7 mysql-community-server.x86_64 0:8.0.29-1.el7
Dependency Installed:
mysql-community-client.x86_64 0:8.0.29-1.el7 mysql-community-client-plugins.x86_64 0:8.0.29-1.el7 mysql-community-common.x86_64 0:8.0.29-1.el7
mysql-community-icu-data-files.x86_64 0:8.0.29-1.el7
Replaced:
mariadb-libs.x86_64 1:5.5.68-1.el7
Complete!
5.启动MYSQL服务,并查看状态,并设置开机自启
[root@VM-8-4-centos local]# systemctl start mysqld =>启动mysql
[root@VM-8-4-centos local]# systemctl status mysqld =>查看状态
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Thu 2022-06-30 22:57:57 CST; 23s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 21861 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 21946 (mysqld)
Status: "Server is operational"
CGroup: /system.slice/mysqld.service
└─21946 /usr/sbin/mysqld
Jun 30 22:57:49 VM-8-4-centos systemd[1]: Starting MySQL Server...
Jun 30 22:57:57 VM-8-4-centos systemd[1]: Started MySQL Server.
[root@VM-8-4-centos local]# systemctl enable mysqld =>开机自启
6.查看初始化密码
[root@VM-8-4-centos local]# cat /var/log/mysqld.log | grep password
2022-06-30T14:57:52.075005Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: <FET2wpip9P7
7.登录
[root@VM-8-4-centos local]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.29
Copyright (c) 2000, 2022, 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>
8.修改密码
在修改密码后才可以修改mysql的密码策略!
mysql> alter user 'root'@'localhost' IDENTIFIED BY '*********';
Query OK, 0 rows affected (0.02 sec)
9.修改mysql的密码策略
# 策略说明
# validate_password.length 是密码的最小长度,默认是8,我们把它改成6
mysql> set global validate_password.length=6;
# validate_password.policy 验证密码的复杂程度,我们把它改成0
mysql> set global validate_password.policy=0;
# validate_password.check_user_name 用户名检查,用户名和密码不能相同,我们也把它关掉
mysql> set global validate_password.check_user_name=off;
# 再执行修改密码的命令
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'xxxxxx';
# 密码设成功
10.设置允许远程登陆
mysql>select host, user, authentication_string, plugin from user;
mysql> update user set user.host='%'where user.user='root';
# MySQL8修改加密方式规则,caching_sha2_password需要改成mysql_native_password
mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'xxxxx';
Query OK, 0 rows affected (0.01 sec)
mysql> flush privileges; => 刷新
Query OK, 0 rows affected (0.00 sec)
11.开放3306端口,并且重启防火墙,然后查看已放开的端口
root@VM-8-4-centos local]# firewall-cmd --zone=public --add-port=3306/tcp --permanent
success
[root@VM-8-4-centos local]# firewall-cmd --reload
success
[root@VM-8-4-centos local]# firewall-cmd --list-ports
3306/tcp
[root@VM-8-4-centos local]#