在CentOS 8 / RHEL 8 Linux上安装MySQL 5.7

是否在寻找指南来帮助您在CentOS 8 / RHEL 8 Linux服务器/工作站上安装MySQL 5.7? CentOS / RHEL 8 AppStream存储库仅包含MySQL 8.0软件包。并非所有应用程序都支持MySQL 8。例如,在撰写本文时,Jira需要MySQL 5.7或更低版​​本。那么如何在CentOS 8 / RHEL 8上安装MySQL 5.7?

MySQL是非常流行的开源关系数据库管理系统。由Oracle数据库公司背后的公司Oracle Corporation开发。凭借久经考验的可靠性,性能和易用性,MySQL是基于Web的应用程序的数据库选择。

请按照以下步骤在CentOS 8 / RHEL 8系统上运行MySQL 5.7服务器。

步骤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:

sudo dnf --enablerepo=mysql57-community install mysql-community-server

按下 ÿ 开始安装。

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

检查包装转速详细信息, 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 <[email protected]>
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用户随机密码

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

记下打印的密码。

2020-01-06T18:06:19.947403Z 1 [Note] A temporary password is generated for [email protected]: 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: 
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 --permanentsudo firewall-cmd --reload

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

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

感谢您使用该指南在CentOS 8 / RHEL 8上安装MySQL 5.7。

最佳MySQL学习书:

  • 启动SQL –针对初学者的动手方法–针对SQL的实际含义的简短,针对性的入门。在这里,读者简要介绍了该语言的所有基础知识。
  • 头先SQL – SQL的大脑–学习者指南。
  • SQL Cookbook:查询解决方案和数据库 适用于数据库开发人员的技术–本书包含许多技巧和技巧,可用于日常数据库管理。
  • 自学MS SQL Server –一本相当古老的书,但从较高的角度涵盖了SQL Server的所有方面。
  • 有效的SQL –易于阅读的指南,探讨SQL功能。请注意,应用布局观念可能需要SQL知识。
Sidebar