分享最新优惠信息
购买主机更加划算

RHEL/CentOS 7/8、Fedora 35系统安装MySQL 8.0详细教程

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

本文将简单介绍在RHEL/CentOS 7/8、Fedora 35系统中通过YUM实用程序使用MySQL Yum存储库来安装和更新最新MySQL 8.0版本的简单过程。

MySQL 8.0

步骤 1:添加MySQL Yum存储库

1、这里使用官方MySQL Yum软件库,它将提供RPM包,用于安装最新版本的MySQL服务器、客户端、MySQL Utilities、MySQL Workbench、Connector/ODBC和Connector/Python,适用于RHEL/CentOS 6/7/8以及Fedora30-35版本。

注意仅适用全新安装MySQL服务器,如果已经有一个MySQL的使用第三方分发RPM软件包安装,那么建议你使用MySQL Yum Repository来升级或更换安装的MySQL。

在升级或替换旧的MySQL包之前,务必要备份好数据库中的重要数据。

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

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

3、为Linux平台下载软件包后,现在使用以下命令安装下载的软件包。

---------------RHEL/CentOS 8系统---------------
# yum localinstall mysql80-community-release-el8-1.noarch.rpm
---------------RHEL/CentOS 7系统---------------
# yum localinstall mysql80-community-release-el7-1.noarch.rpm
---------------RHEL/CentOS 6系统---------------
# yum localinstall mysql80-community-release-el6-1.noarch.rpm
---------------Fedora 35 系统---------------
# dnf localinstall mysql80-community-release-fc35-1.noarch.rpm
---------------Fedora 34系统---------------
# dnf localinstall mysql80-community-release-fc34-1.noarch.rpm
---------------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.*"      [Fedora系统]

验证MySQL Yum存储库

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

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

# yum install mysql-community-server
# dnf install mysql-community-server      [Fedora系统]

上面的命令为MySQL服务器mysql-community-servermysql-community-clientmysql-community-commonmysql-community-libs安装所有需要的包。

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

与此同时,还可以使用MySQL Community Server的不同子存储库安装不同的MySQL版本。最近的MySQL系列(目前为MySQL 8.0)子仓库默认开启,其他版本(如MySQL 5.x系列)子仓库默认关闭。

要从特定的子存储库安装特定版本,可以使用yum-config-managerdnf config-manager使用--enable--disable选项,如下所示:

# yum-config-manager --disable mysql57-community
# yum-config-manager --enable mysql56-community
------------------ Fedora系统------------------
# dnf config-manager --disable mysql57-community
# dnf config-manager --enable mysql56-community

步骤 4:启动MySQL服务器

成功安装 MySQL 后,就可以使用以下命令启动和启用MySQL服务器了:

# service mysqld start
# systemctl enable mysqld.service

可以适用以下命令验证MySQL服务器的状态。

# systemctl status mysqld.service
或者
# 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

Dec 02 05:15:19 localhost.localdomain systemd[1]: Started MySQL Server.

最后使用以下命令验证安装的 MySQL 版本。

# mysql --version

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

步骤 5:保护MySQL数据库

还是老样子,mysql_secure_installation命令允许通过执行重要设置来保护MySQL,例如设置root密码、删除匿名用户、删除root登录等。

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

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

# 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服务器

通过提供数据库用户名和密码连接到新安装的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

除了安装MySQL最新版本之外,还还可以借助以下命令对MySQL产品和组件进行更新。

# yum update mysql-server
# dnf update mysql-server       [Fedora系统]

更新MySQL版本

这样当MySQL有新的更新可用时,它会自动安装它们,如果没有,将收到一条消息,即NO packages marked for updates.

总结

通过以上几个步骤的设置,你就可以在RHEL/CentOS 7/8、Fedora 35系统上面成功安装了MySQL 8.0版本,相对来说还是比较简单的,不是很复杂。

未经允许不得转载:惠主机 » RHEL/CentOS 7/8、Fedora 35系统安装MySQL 8.0详细教程

相关文章