网站搜索

如何在 RHEL/CentOS 8/7 和 Fedora 35 上安装 MySQL 8.0


MySQL 是一个开源免费关系数据库管理系统 (RDBMS),在 GNU 下发布(通用公共许可证) 。它通过提供对每个创建的数据库的多用户访问来在任何单个服务器上运行多个数据库。

本文将引导您在 RHEL/CentOS 8/7/6/ 上安装和更新最新 MySQL 8.0 版本的过程,以及Fedora 通过 YUM 实用程序使用 MySQL Yum 存储库。

第 1 步:添加 MySQL Yum 存储库

1.我们将使用官方MySQL Yum软件存储库,该存储库将提供用于安装最新版本的MySQL服务器、客户端、MySQL Utilities、MySQL Workbench、Connector/的RPM包用于 RHEL/CentOS 8/7/6/Fedora 30-35 的 ODBC 和 Connector/Python。

重要:这些说明仅适用于在服务器上全新安装 MySQL(如果已有 MySQL) > 使用第三方分发的 RPM 软件包安装,那么我建议您使用 MySQL Yum Repository 升级或替换已安装的 MySQL 软件包”。

在升级或替换旧的 MySQL 软件包之前,不要忘记使用我们的备份 MySQL 数据库指南来获取所有重要的数据库备份和配置文件。

2. 现在下载以下 MySQL Yum 存储库 并将其添加到您各自的 Linux 发行版系统的存储库列表中,以安装最新版本的 MySQL(即 8.02018 年 7 月 27 日发布)。

--------------- On RHEL/CentOS 8 ---------------
wget https://repo.mysql.com/mysql80-community-release-el8-1.noarch.rpm
--------------- On RHEL/CentOS 7 ---------------
wget https://repo.mysql.com/mysql80-community-release-el7-1.noarch.rpm
--------------- On RHEL/CentOS 6 ---------------
wget https://dev.mysql.com/get/mysql80-community-release-el6-1.noarch.rpm
--------------- On Fedora 35 ---------------
wget https://dev.mysql.com/get/mysql80-community-release-fc35-1.noarch.rpm
--------------- On Fedora 34 ---------------
wget https://dev.mysql.com/get/mysql80-community-release-fc34-1.noarch.rpm
--------------- On Fedora 33 ---------------
wget https://dev.mysql.com/get/mysql80-community-release-fc33-1.noarch.rpm

3. 下载适用于您的 Linux 平台的软件包后,现在使用以下命令安装下载的软件包。

--------------- On RHEL/CentOS 8 ---------------
yum localinstall mysql80-community-release-el8-1.noarch.rpm
--------------- On RHEL/CentOS 7 ---------------
yum localinstall mysql80-community-release-el7-1.noarch.rpm
--------------- On RHEL/CentOS 6 ---------------
yum localinstall mysql80-community-release-el6-1.noarch.rpm
--------------- On Fedora 35 ---------------
dnf localinstall mysql80-community-release-fc35-1.noarch.rpm
--------------- On Fedora 34 ---------------
dnf localinstall mysql80-community-release-fc34-1.noarch.rpm
--------------- On Fedora 33 ---------------
yum localinstall mysql80-community-release-fc33-1.noarch.rpm

上述安装命令将 MySQL Yum 存储库添加到系统的存储库列表中,并下载 GnuPG 密钥以验证软件包的完整性。

4.您可以使用以下命令验证MySQL Yum存储库是否已成功添加。

yum repolist enabled | grep "mysql.*-community.*"
dnf repolist enabled | grep "mysql.*-community.*"      [On Fedora versions]

第2步:安装最新的MySQL版本

5.使用以下命令安装最新版本的MySQL(当前为8.0)。

yum install mysql-community-server
dnf install mysql-community-server      [On Fedora versions]

上面的命令安装 MySQL 服务器 mysql-community-servermysql-community-clientmysql-community-commonmysql-community-libs

步骤3:安装不同的MySQL发行版本

6.您还可以使用MySQL社区服务器的不同子存储库安装不同的MySQL版本。最新MySQL系列(目前为MySQL 8.0)的子存储库默认处于激活状态,所有其他版本(例如MySQL 5.x系列)的子存储库默认处于停用状态。

要从特定子存储库安装特定版本,您可以使用 yum-config-manager 使用 --enable--disable 选项或dnf配置管理器,如图所示:

yum-config-manager --disable mysql57-community
yum-config-manager --enable mysql56-community
------------------ Fedora Versions ------------------
dnf config-manager --disable mysql57-community
dnf config-manager --enable mysql56-community

第四步:启动MySQL服务器

7.成功安装MySQL后,可以使用以下命令启动并启用MySQL服务器:

service mysqld start
systemctl enable mysqld.service

您可以借助以下命令验证 MySQL 服务器的状态。

systemctl status mysqld.service
OR
service mysqld status

这是在我的 CentOS 7 机器下运行 MySQL 的示例输出。

Redirecting to /bin/systemctl status  mysqld.service
mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled)
   Active: active (running) since Thu 2015-10-29 05:15:19 EDT; 4min 5s ago
  Process: 5314 ExecStart=/usr/sbin/mysqld --daemonize $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
  Process: 5298 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 5317 (mysqld)
   CGroup: /system.slice/mysqld.service
           └─5317 /usr/sbin/mysqld --daemonize

Oct 29 05:15:19 localhost.localdomain systemd[1]: Started MySQL Server.

8. 现在最后使用以下命令验证已安装的 MySQL 版本。

mysql --version

mysql  Ver 8.0.27 for Linux on x86_64 (MySQL Community Server - GPL)

第 5 步:确保 MySQL 安装的安全

9.命令mysql_secure_installation允许您通过执行重要设置(例如设置root密码、删除匿名用户、删除root登录名等)来保护您的MySQL安装。

注意:MySQL版本8.0或更高版本在/var/log/mysqld.log中生成临时随机密码安装后。

在运行 MySQL secure 命令之前,使用以下命令查看密码。

grep 'temporary password' /var/log/mysqld.log

知道密码后,您现在可以运行以下命令来保护您的 MySQL 安装。

mysql_secure_installation

注意:输入新的Root密码是指来自文件/var/log/mysqld.log的临时密码。

现在请仔细按照屏幕上的说明进行操作,作为参考,请参阅下面上述命令的输出。

样本输出
Securing the MySQL server deployment.

Enter password for user root: Enter New Root Password

VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?

Press y|Y for Yes, any other key for No: y

There are three levels of password validation policy:

LOW    Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary                  file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 2
Using existing password for root.

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

New password: Set New MySQL Password

Re-enter new password: Re-enter New MySQL Password

Estimated strength of the password: 100 
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.

Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
 - Dropping test database...
Success.

 - Removing privileges on test database...
Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.

All done! 

第6步:连接MySQL服务器

10. 通过提供用户名和密码连接到新安装的 MySQL 服务器。

mysql -u root -p

示例输出:

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 8.0.27 MySQL Community Server - GPL

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>

第7步:使用 Yum 更新 MySQL

11.除了全新安装之外,您还可以借助以下命令对 MySQL 产品和组件进行更新。

yum update mysql-server
dnf update mysql-server       [On Fedora versions]

MySQL 有新的更新可用时,它会自动安装它们,否则您将收到一条消息,指出没有标记为更新的软件包

就这样,您已经在系统上成功安装了MySQL 8.0。如果您在安装时遇到任何问题,请随时使用我们的评论部分寻求解决方案。