1、生成repo文件
首先,在目录/etc/yum.repos.d/下建mysql-community.repo文件。
然后根据需要生成不同版本的内容。
1.1、mysql5.7 版本的repo内容
[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
1.2、mysql8 版本的repo内容
# Enable to use MySQL 8.0
[mysql80-community]
name=MySQL 8.0 Community Server
baseurl=http://repo.mysql.com/yum/mysql-8.0-community/el/7/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
2、开始安装
2.1、禁用默认MySQL模块
(仅限EL8系统)基于EL8的系统(如RHEL8和Oracle Linux 8)包括默认启用的MySQL模块。
sudo yum module disable mysql
2.2、安装
通过以下命令安装MySQL(对于启用dnf的系统,将命令中的yum替换为dnf):
sudo yum install mysql-community-server
这将安装MySQL服务器的包(mysql-community-server
)以及运行服务器所需组件的包,包括客户端的包(mysql-community-client
)、客户端和服务器的常见错误消息和字符集(mysql-community-common
)以及共享客户端库(mysql-community-libs
)。
我在实际操作时,都提示了如下错误信息:
GPG key retrieval failed: [Errno 14] curl#37 - "Couldn't open file /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql"
可以到这个到这个位置 下载文件 RPM-GPG-KEY-mysqlhttps://download.csdn.net/download/zxlyx/21653434
下载完成后,把文件上传到服务器的 /etc/pki/rpm-gpg/ 目录下。
2.3、启动 MySQL服务
启动MySQL server 使用以下命令:
systemctl start mysqld
你可以查看 MySQL server 状态使用如下命令:
systemctl status mysqld
2.4、产生默认密码
[root@iZ8vb72 ~]# sudo grep 'temporary password' /var/log/mysqld.log
2021-08-27T09:09:15.584394Z 1 [Note] A temporary password is generated for root@localhost: gUx?FWtcx6Ca
上面的命令执行完成后,在输出的最后“gUx?FWtcx6Ca”即是生成的默认密码;
2.5、产生默认密码
通过使用生成的临时密码登录并为超级用户帐户设置自定义密码,尽快更改根密码:
mysql -uroot -p
输入上面生成的默认密码如下效果:
[root@iZ8vb72 ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.35
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> ALTER USER 'root'@'localhost' IDENTIFIED BY 'root1234';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
这是因为 mysql-5.7.35 有这个验证,所以需要先设置一个复杂的密码,如下操作:
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass4!';
设置好密码后,可以不必退出,直接做如下操作。
mysql> show variables like 'validate_password%';
+--------------------------------------+--------+
| 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> set global validate_password_policy=LOW;
Query OK, 0 rows affected (0.00 sec)
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'root1234';
Query OK, 0 rows affected (0.00 sec)
此处,不必退出,也可以继续下面的操作。
2.6、授权法设置远程登录
你想myuser使用mypassword从任何主机连接到mysql服务器的话。
mysq> GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'%' IDENTIFIED BY 'mypassword' WITH GRANT OPTION;
mysq> flush privileges;
mysq> exit;
如果你想允许用户myuser从ip为192.168.1.6的主机连接到mysql服务器,并使用mypassword作为密码
mysql> GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'192.168.1.3' IDENTIFIED BY 'mypassword' WITH GRANT OPTION;
mysql> flush privileges;
--------------------------------------------
转自《mysql官网》