如何部署Oracle Linux 9.x MySQL 8.x?
- By : Will
- Category : MySQL & MariaDB
MySQL & MariaDB
1 前言
MySQL相信不用笔者多做介绍,本章将要使用yum部署MySQL 8。
2 最佳实践
2.1 系统环境
OS = Oracle Linux 9.x x86_64
IP = any
Host Name = any.cmdschool.org
2.2 安装前准备
2.2.1 配置安装源
vim /etc/yum.repos.d/mysql-8.4-community-rhel9.repo
加入如下配置,
[mysql80-community] name=MySQL 8.0 Community Server baseurl=http://repo.mysql.com/yum/mysql-8.4-community/el/9/$basearch/ enabled=1 gpgcheck=1 gpgkey=http://repo.mysql.com/RPM-GPG-KEY-mysql-2023
2.2.2 部署软件包
yum install -y mysql-community-server
2.3 配置MySQL服务
2.3.1 启动服务并设置自启动
systemctl start mysqld.service systemctl enable mysqld.service
另外,我们建议使用如下命令确实服务已经启动,
systemctl status mysqld.service
2.3.2 关闭密码复杂度要求
cp /etc/my.cnf /etc/my.cnf.default vim /etc/my.cnf
加入如下配置
[mysqld] # Disable password complexity check validate_password.policy=low validate_password.length=4
设置完成后,你需要重启数据库服务使配置生效,
systemctl restart mysqld.service
2.3.3 查询初始密码
grep "temporary password" /var/log/mysqld.log
可见如下显示,
2025-10-27T01:53:54.989429Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: Qg!hG(cJ*2ja
2.3.4 运行初始化向导
mysql_secure_installation
向导如下,
[...] Enter password for user root:****** [...] New password:****** Re-enter new password:****** [...] Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y [...] Remove anonymous users? (Press y|Y for Yes, any other key for No) : y [...] Disallow root login remotely? (Press y|Y for Yes, any other key for No) : n [...] Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y [...] Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y [...]
2.3.5 登录测试
mysql -uroot -p
2.3.6 设置防火墙(可选)
firewall-cmd --permanent --add-service mysql firewall-cmd --reload firewall-cmd --list-all
2.4 迁移数据目录
2.4.1 登录测试
systemctl stop mysqld.service
2.4.2 迁移数据到新的存储目录
mv /var/lib/mysql /data/
mkdir /var/lib/mysql
2.4.3 修改配置声明存储目录位置
vim /etc/my.cnf
修改如下配置,
[mysqld] # datadir=/var/lib/mysql datadir=/data/mysql #...
根据配置重建“socket”参数所需的目录,
mkdir /var/lib/mysql chown mysql:mysql /var/lib/mysql
2.4.4 设置密码复杂度
vim /etc/my.cnf
修改如下配置,
[mysqld] #... validate_password.policy=low validate_password.length=4
2.4.5 关闭SELinux
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config setenforce 0
2.4.6 尝试启动服务
systemctl start mysqld.service
2.5 授予应用程序访问前线
mysql -uroot -p create database if not exists mydb; create user 'mydb'@'localhost' identified by 'mydbpwd'; grant all privileges on mydb.* to 'mydb'@'localhost'; flush privileges;
没有评论