如何在 RHEL/CentOS 8/7 和 Fedora 35 上安装 MySQL 8.0
MySQL是一个开源免费的关系数据库管理系统 ( RDBMS ),根据GNU(通用公共许可证)发布。它用于在任何单个服务器上运行多个数据库,为每个创建的数据库提供多用户访问。
本文将引导您通过YUM 实用程序使用MySQL Yum存储库在RHEL/CentOS 8/7/6/和Fedora上安装和更新最新MySQL 8.0版本的过程。
步骤1:添加MySQL Yum存储库
1.我们将使用官方MySQL Yum软件存储库,它将提供用于为 RHEL /CentOS 8/7/6/和Fedora 30-35安装最新版本的 MySQL 服务器、客户端、MySQL 实用程序、MySQL Workbench、Connector/ODBC 和 Connector/Python 的RPM 包。
重要提示:这些说明仅适用于服务器上全新安装的MySQL,如果已经使用第三方分发的 RPM 包安装了 MySQL,那么我建议您使用MySQL Yum 存储库升级或替换已安装的MySQL包”。
在升级或替换旧的 MySQL 包之前,请不要忘记使用我们的备份 MySQL 数据库指南获取所有重要的数据库备份和配置文件。
2.现在下载并将以下MySQL Yum 存储库添加到各自的 Linux 发行系统的存储库列表中,以安装最新版本的 MySQL(即2018 年 7 月 27 日发布的8.0)。
--------------- 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-server、mysql-community-client、mysql-community-common和mysql-community-libs所需的所有软件包。
步骤3:安装不同的MySQL发布版本
6.您还可以使用 MySQL Community Server 的不同子存储库安装不同的 MySQL 版本。最新的 MySQL 系列(当前为MySQL 8.0)的子存储库默认处于激活状态,所有其他版本(例如 MySQL 5.x 系列)的子存储库默认处于停用状态。
要从特定子存储库安装特定版本,您可以使用--enable
或--disable
选项使用yum-config-manager或dnf config-manager,如下所示:
# 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
步骤4:启动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 安全命令之前使用以下命令查看密码。
# 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 。如果您在安装过程中遇到任何问题,请随时使用我们的评论部分寻求解决方案。