第 1 步:添加 MySQL 存储库

禁用 MySQL 默认 AppStream 存储库:

sudo dnf remove @mysql
sudo dnf module reset mysql
sudo dnf module disable mysql

EL 8 没有 MySQL 存储库,因此我们将使用 EL 7 存储库。创建一个新的存储库文件。

sudo vi /etc/yum.repos.d/mysql-community.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=0

[mysql-connectors-community]
name=MySQL Connectors Community
baseurl=http://repo.mysql.com/yum/mysql-connectors-community/el/7/$basearch/
enabled=1
gpgcheck=0

[mysql-tools-community]
name=MySQL Tools Community
baseurl=http://repo.mysql.com/yum/mysql-tools-community/el/7/$basearch/
enabled=1
gpgcheck=0

第 2 步:在 CentOS 8 / RHEL 8 上安装 MySQL 5.7

添加存储库后,现在在 CentOS 8 / RHEL 8 上安装 MySQL 5.7。
禁用 MySQL 8 存储库:

sudo dnf config-manager --disable mysql80-community

然后为 MySQL 5.7 启用通道。

sudo dnf config-manager --enable mysql57-community

然后在 CentOS 8 / RHEL 8 上安装 MySQL 5.7:

sudo dnf install mysql-community-server

按y开始安装。

Last metadata expiration check: 0:02:41 ago on Mon 06 Jan 2020 08:54:52 PM EAT.
Dependencies resolved.
========================================================================================================================================================
 Package                                   Arch                      Version                                 Repository                            Size
========================================================================================================================================================
Installing:
 mysql-community-server                    x86_64                    5.7.28-1.el7                            mysql57-community                    199 M
Installing dependencies:
 ncurses-compat-libs                       x86_64                    6.1-7.20180224.el8                      BaseOS                               331 k
 mysql-community-client                    x86_64                    5.7.28-1.el7                            mysql57-community                     43 M
 mysql-community-common                    x86_64                    5.7.28-1.el7                            mysql57-community                    311 k
 mysql-community-libs                      x86_64                    5.7.28-1.el7                            mysql57-community                    4.2 M

Transaction Summary
========================================================================================================================================================
Install  5 Packages

Total download size: 247 M
Installed size: 1.0 G
Is this ok [y/N]: y

检查包 rpm 详细信息以确认它是5.7。

$ rpm -qi mysql-community-server 
Name        : mysql-community-server
Version     : 5.7.28
Release     : 1.el7
Architecture: x86_64
Install Date: Mon 06 Jan 2020 08:58:52 PM EAT
Group       : Applications/Databases
Size        : 910635041
License     : Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved. Under GPLv2 license as shown in the Description field.
Signature   : DSA/SHA1, Mon 30 Sep 2019 11:05:08 AM EAT, Key ID 8c718d3b5072e1f5
Source RPM  : mysql-community-5.7.28-1.el7.src.rpm
Build Date  : Fri 27 Sep 2019 11:11:06 AM EAT
Build Host  : loki02.no.oracle.com
Relocations : (not relocatable)
Packager    : MySQL Release Engineering <mysql-build@oss.oracle.com>
Vendor      : Oracle and/or its affiliates
URL         : http://www.mysql.com/
Summary     : A very fast and reliable SQL database server

第 3 步:在 CentOS 8 / RHEL 8 上配置 MySQL 5.7

2.1 – 安装后,启动mysqld服务。

sudo systemctl enable --now mysqld.service

2.2 – 为root用户复制生成的随机密码

sudo grep 'A temporary password' /var/log/mysqld.log |tail -1

记下打印的密码:

2020-01-06T18:06:19.947403Z 1 [Note] A temporary password is generated for root@localhost: AS*5Rx%YY5+c

2.3 - 启动 MySQL 安全安装以更改 root 密码,禁止 root 远程登录,删除匿名用户并删除测试数据库。

$ sudo mysql_secure_installation
Securing the MySQL server deployment.
Enter password for user root:

使用您生成的临时密码进行身份验证。这将要求您为 root 用户设置一个新密码。

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

New password: 
Re-enter new password: 

Estimated strength of the password: 100 
Do you wish to continue with the password provided?: Yes

Remove anonymous users?: Yes
Success.

Disallow root login remotely? : Yes
Success.

Remove test database and access to it? : Yes
 - Dropping test database...
Success.
 - Removing privileges on test database...
Success.

Reload privilege tables now? (Press y|Y for Yes) : Yes
Success.

All done!

您可以使用在线密码生成器来获取复杂的密码。

2.4 – 以 root 用户身份连接到 MySQL 数据库并创建一个测试数据库。

$ mysql -u root -p
Enter password: <Enter Root Password>
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 15
Server version: 5.7.28 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

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> SELECT VERSION();
+-----------+
| VERSION() |
+-----------+
| 5.7.28    |
+-----------+
1 row in set (0.00 sec)

mysql> QUIT
Bye

2.5 – 创建测试数据库和用户:

mysql> CREATE DATABASE test_db;
Query OK, 1 row affected (0.09 sec)

mysql> CREATE USER 'test_user'@'localhost' IDENTIFIED BY "Strong34S;#";
Query OK, 0 rows affected (0.04 sec)

mysql> GRANT ALL PRIVILEGES ON test_db.* TO 'test_user'@'localhost';
Query OK, 0 rows affected (0.02 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.02 sec)

可以通过运行删除此测试数据库和用户:

mysql> DROP DATABASE test_db;
Query OK, 0 rows affected (0.14 sec)

mysql> DROP USER 'test_user'@'localhost';
Query OK, 0 rows affected (0.11 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.01 sec)

mysql> QUIT
Bye

第 4 步:配置防火墙 -(仅适用于远程连接)

要允许远程连接,请在防火墙上允许端口 3306

sudo firewall-cmd --add-service=mysql --permanent
sudo firewall-cmd --reload

您还可以限制来自受信任网络的访问:

sudo firewall-cmd --permanent --add-rich-rule 'rule family="ipv4" \
service name="mysql" source address="10.10.10.0/24" accept'

第 5 步:配置远程访问

[root@izbp10fk8pd9zjv9y2g9ajz etc]# mysql -u root -p
Enter password:
use mysql;
select host from user where user='root';
update user set host = '%' where user ='root';
flush privileges;
最后修改:2022 年 03 月 09 日
如果觉得我的文章对你有用,请随意赞赏