共计 8179 个字符,预计需要花费 21 分钟才能阅读完成。
一、说明
1、Rocky Linux 9.x 系统默认集成了 MySQL 8.0 的仓库源,因此可以通过 dnf 命令直接在线安装,优点是操作简单快捷,缺点是 MySQL 的小版本随系统仓库更新,无法自由选择较新的小版本。
2、本文采用的是 MySQL 官方仓库源 进行安装,优点是可灵活选择并安装最新的小版本,缺点是相较系统仓库安装方式,步骤稍微多一些。
二、安装部署
1、安装 MySQL 官方 YUM 仓库:dnf install -y https://dev.mysql.com/get/mysql84-community-release-el9-2.noarch.rpm
# dnf install -y https://dev.mysql.com/get/mysql84-community-release-el9-2.noarch.rpm
mysql84-community-release-el9-2.noarch.rpm 5.0 kB/s | 14 kB 00:02
Dependencies resolved.
=====================================================================================================
Package Architecture Version Repository Size
=====================================================================================================
Installing:
mysql84-community-release noarch el9-2 @commandline 14 k
Transaction Summary
=====================================================================================================
Install 1 Package
Total size: 14 k
Installed size: 16 k
Downloading Packages:
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : mysql84-community-release-el9-2.noarch 1/1
Running scriptlet: mysql84-community-release-el9-2.noarch 1/1
Verifying : mysql84-community-release-el9-2.noarch 1/1
Installed:
mysql84-community-release-el9-2.noarch
Complete!
2、查看已启用仓库:dnf repolist all | grep mysql
# dnf repolist all | grep mysql
mysql-8.4-lts-community MySQL 8.4 LTS Community Se enabled
mysql-8.4-lts-community-debuginfo MySQL 8.4 LTS Community Se disabled
mysql-8.4-lts-community-source MySQL 8.4 LTS Community Se disabled
mysql-cluster-8.0-community MySQL Cluster 8.0 Communit disabled
mysql-cluster-8.0-community-debuginfo MySQL Cluster 8.0 Communit disabled
mysql-cluster-8.0-community-source MySQL Cluster 8.0 Communit disabled
mysql-cluster-8.4-lts-community MySQL Cluster 8.4 LTS Comm disabled
mysql-cluster-8.4-lts-community-debuginfo MySQL Cluster 8.4 LTS Comm disabled
mysql-cluster-8.4-lts-community-source MySQL Cluster 8.4 LTS Comm disabled
mysql-cluster-innovation-community MySQL Cluster Innovation R disabled
mysql-cluster-innovation-community-debuginfo MySQL Cluster Innovation R disabled
mysql-cluster-innovation-community-source MySQL Cluster Innovation R disabled
mysql-connectors-community MySQL Connectors Community enabled
mysql-connectors-community-debuginfo MySQL Connectors Community disabled
mysql-connectors-community-source MySQL Connectors Community disabled
mysql-innovation-community MySQL Innovation Release C disabled
mysql-innovation-community-debuginfo MySQL Innovation Release C disabled
mysql-innovation-community-source MySQL Innovation Release C disabled
mysql-tools-8.4-lts-community MySQL Tools 8.4 LTS Commun enabled
mysql-tools-8.4-lts-community-debuginfo MySQL Tools 8.4 LTS Commun disabled
mysql-tools-8.4-lts-community-source MySQL Tools 8.4 LTS Commun disabled
mysql-tools-community MySQL Tools Community disabled
mysql-tools-community-debuginfo MySQL Tools Community - De disabled
mysql-tools-community-source MySQL Tools Community - So disabled
mysql-tools-innovation-community MySQL Tools Innovation Com disabled
mysql-tools-innovation-community-debuginfo MySQL Tools Innovation Com disabled
mysql-tools-innovation-community-source MySQL Tools Innovation Com disabled
mysql80-community MySQL 8.0 Community Server disabled
mysql80-community-debuginfo MySQL 8.0 Community Server disabled
mysql80-community-source MySQL 8.0 Community Server disabled
3、禁用 8.4 仓库,启用 8.0 仓库:
# dnf config-manager –disable mysql-8.4-lts-community mysql-tools-8.4-lts-community
# dnf config-manager –enable mysql80-community mysql-tools-community
4、安装MySQL:dnf install -y mysql-community-server
# dnf install -y mysql-community-server
MySQL Connectors Community 38 kB/s | 90 kB 00:02
MySQL 8.0 Community Server 767 kB/s | 2.7 MB 00:03
MySQL Tools Community 262 kB/s | 1.2 MB 00:04
Last metadata expiration check: 0:00:01 ago on Tue 29 Jul 2025 08:00:28 PM CST.
Dependencies resolved.
=====================================================================================================
Package Arch Version Repository Size
=====================================================================================================
Installing:
mysql-community-server x86_64 8.0.43-1.el9 mysql80-community 50 M
Installing dependencies:
libaio x86_64 0.3.111-13.el9 baseos 23 k
......此处省略......
net-tools x86_64 2.0-0.64.20160912git.el9 baseos 294 k
Transaction Summary
=====================================================================================================
Install 8 Packages
Total download size: 59 M
Installed size: 338 M
Downloading Packages:
(1/8): mysql-community-common-8.0.43-1.el9.x86_64.rpm 336 kB/s | 556 kB 00:01
......此处省略......
(8/8): mysql-community-server-8.0.43-1.el9.x86_64.rpm 14 MB/s | 50 MB 00:03
-----------------------------------------------------------------------------------------------------
Total 9.4 MB/s | 59 MB 00:06
MySQL 8.0 Community Server 3.0 MB/s | 3.1 kB 00:00
Importing GPG key 0xA8D3785C:
Userid : "MySQL Release Engineering "
Fingerprint: BCA4 3417 C3B4 85DD 128E C6D4 B7B3 B788 A8D3 785C
From : /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql-2023
Key imported successfully
MySQL 8.0 Community Server 3.0 MB/s | 3.1 kB 00:00
Importing GPG key 0x3A79BD29:
Userid : "MySQL Release Engineering "
Fingerprint: 859B E8D7 C586 F538 430B 19C2 467B 942D 3A79 BD29
From : /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql-2022
Key imported successfully
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : mysql-community-common-8.0.43-1.el9.x86_64 1/8
......此处省略......
Verifying : libaio-0.3.111-13.el9.x86_64 8/8
Installed:
libaio-0.3.111-13.el9.x86_64 mysql-community-client-8.0.43-1.el9.x86_64
......此处省略......
mysql-community-server-8.0.43-1.el9.x86_64 net-tools-2.0-0.64.20160912git.el9.x86_64
Complete!
5、启动并设置服务开机启动:systemctl enable –now mysqld
# systemctl enable --now mysqld
# systemctl status mysqld
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; preset: disabled)
Active: active (running) since Tue 2025-07-29 20:02:06 CST; 5s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 3776 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 3897 (mysqld)
Status: "Server is operational"
Tasks: 38 (limit: 48698)
Memory: 490.7M
CPU: 3.685s
CGroup: /system.slice/mysqld.service
└─3897 /usr/sbin/mysqld
Jul 29 20:01:56 localhost.localdomain systemd[1]: Starting MySQL Server...
Jul 29 20:02:06 localhost.localdomain systemd[1]: Started MySQL Server.
6、查验安装版本:mysqladmin -V
# mysqladmin -V
mysqladmin Ver 8.0.43 for Linux on x86_64 (MySQL Community Server - GPL)
7、查看root用户临时密码:grep ‘temporary password’ /var/log/mysqld.log
# grep 'temporary password' /var/log/mysqld.log
2025-07-29T12:02:02.627256Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: ,iP-yZ,uj9d%
三、初始化操作
1、使用临时密码执行 mysql_secure_installation 命令进行初始化操作:mysql_secure_installation
# mysql_secure_installation
Securing the MySQL server deployment.
Enter password for user root: # 输入临时密码
The existing password for the user account root has expired. Please set a new password.
New password: # 设置新密码
Re-enter new password: # 确认新密码
The 'validate_password' component is installed on the server.
The subsequent steps will run with the existing configuration
of the component.
Using existing password for root.
Estimated strength of the password: 100
Change the password for root ? ((Press y|Y for Yes, any other key for No) : No # 输入No无需再次更换密码
... skipping.
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) : Yes # 输入Yes确认删除匿名用户
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) : Yes # 输入Yes禁止root远程登录
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) : Yes # 输入Yes删除test测试数据库
- 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) : Yes # 输入Yes重新加载权限表
Success.
All done!
2、初始化执行了以下安全加固措施:
1、重设了 root 密码
2、保留已有密码,不再二次更改
3、删除匿名用户
4、禁止 root 用户远程登录
5、删除 test 数据库
6、刷新权限表