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

时间:2020-02-23 14:31:13  来源:igfitidea点击:

我们是否正在寻找指南来在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 Corporation Company在Oracle Database之后开发的。凭借其可靠的可靠性,性能和易用性,MySQL已成为基于Web的应用程序的首选数据库选择。

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

添加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

在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 2017 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.20160224.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 2017 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 <theitroad@localhost>
Vendor      : Oracle and/or its affiliates
URL         : http://www.mysql.com/
Summary     : A very fast and reliable SQL database server

在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

记下打印的密码:

2017-01-06T18:06:19.947403Z 1 [Note] A temporary password is generated for theitroad@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 Jan 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

配置防火墙(仅用于远程连接)

要允许远程连接,请在防火墙上允许端口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'