业务侧需要一个MySQL实例,基于用新不用旧的原则。在新的 Oracle Linux 9.6 上安装 MySQL 8.4.5 LTS版本。由于是隔离环境,环境使用本地 YUM 源 , 数据目录规划在/data/mysql;安装iso在/home下
1. 准备本地 YUM 源
# 挂载 ISO
sudo mkdir -p /media/ol9-iso
sudo mount -o loop /home/OracleLinux-9.6.iso /media/ol9-iso
# 创建本地源配置
sudo tee /etc/yum.repos.d/ol9-local.repo > /dev/null <<'EOF'
[ol9-local-baseos]
name=Oracle Linux 9.6 BaseOS
baseurl=file:///media/ol9-iso/BaseOS
gpgcheck=0
enabled=1
[ol9-local-appstream]
name=Oracle Linux 9.6 AppStream
baseurl=file:///media/ol9-iso/AppStream
gpgcheck=0
enabled=1
EOF
# 禁用所有网络源
sudo find /etc/yum.repos.d/ -type f -name "*.repo" -not -name "ol9-local.repo" -exec mv {} {}.bak \;
# 刷新缓存
sudo yum clean all
sudo yum --disablerepo="*" --enablerepo="ol9-local-*" makecache
2. 安装依赖包
# 安装基础依赖
sudo yum --disablerepo="*" --enablerepo="ol9-local-*" install -y \
libaio \
libaio-devel \
tar \
perl-Data-Dumper \
numactl-devel \
ncurses\* # 使用通配符安装所有ncurses相关包
# 验证ncurses安装
rpm -qa | grep ncurses
ncurses-base-6.2-10.20210508.el9.noarch
ncurses-libs-6.2-10.20210508.el9.x86_64
ncurses-6.2-10.20210508.el9.x86_64
ncurses-c++-libs-6.2-10.20210508.el9.x86_64
ncurses-devel-6.2-10.20210508.el9.x86_64
ncurses-term-6.2-10.20210508.el9.noarch
3. 创建 MySQL 用户和目录
# 创建用户组
sudo groupadd mysql
sudo useradd -r -g mysql -s /bin/false mysql
# 创建数据目录
sudo mkdir -p /data/mysql
sudo chown -R mysql:mysql /data/mysql
sudo chmod 750 /data/mysql
4. 安装 MySQL 8.4.5
# 下载 MySQL
cd /home
--隔离环境,自行下载后upload
--sudo wget https://cdn.mysql.com//Downloads/MySQL-8.4/mysql-8.4.5-linux-glibc2.28-x86_64.tar.xz
# 解压到安装目录
sudo mkdir -p /usr/local/mysql
sudo tar -xvf mysql-8.4.5-linux-glibc2.28-x86_64.tar.xz -C /usr/local/mysql --strip-components=1
sudo chown -R mysql:mysql /usr/local/mysql
5. 配置环境变量
sudo tee /etc/profile.d/mysql.sh > /dev/null <<'EOF'
export PATH=$PATH:/usr/local/mysql/bin
EOF
source /etc/profile
6. 创建 MySQL 配置文件
sudo tee /etc/my.cnf > /dev/null <<'EOF'
[mysqld]
# 目录设置
basedir=/usr/local/mysql
datadir=/data/mysql
socket=/data/mysql/mysql.sock
pid-file=/data/mysql/mysql.pid
# 网络设置
port=3306
bind-address=0.0.0.0
# 字符集
character-set-server=utf8mb4
collation-server=utf8mb4_0900_ai_ci
# 日志
log-error=/data/mysql/error.log
# 内存配置
innodb_buffer_pool_size=1G
max_connections=1000
# 其他设置
default-storage-engine=InnoDB
explicit_defaults_for_timestamp=1
skip-name-resolve
[mysql]
socket=/data/mysql/mysql.sock
EOF
7. 初始化 MySQL
sudo /usr/local/mysql/bin/mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/data/mysql
8. 创建 Systemd 服务
sudo tee /etc/systemd/system/mysqld.service > /dev/null <<'EOF'
[Unit]
Description=MySQL Server
After=network.target
[Service]
User=mysql
Group=mysql
ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf
ExecStop=/usr/local/mysql/bin/mysqladmin -u root shutdown
Restart=always
RestartSec=3
LimitNOFILE=65535
# 自动重启设置
StartLimitInterval=5
StartLimitBurst=3
[Install]
WantedBy=multi-user.target
EOF
# 启用服务
sudo systemctl daemon-reload
sudo systemctl enable mysqld
sudo systemctl start mysqld
9. 设置 root 密码
--检查 MySQL 服务状态
sudo systemctl status mysqld
[root@DB-190 home]# systemctl status mysqld
● mysqld.service - MySQL Server
Loaded: loaded (/etc/systemd/system/mysqld.service; enabled; preset: disabled)
Active: active (running) since Thu 2025-07-03 13:20:30 CST; 5min ago
Main PID: 35317 (mysqld)
Tasks: 35 (limit: 47342)
Memory: 509.6M
CPU: 1.463s
CGroup: /system.slice/mysqld.service
└─35317 /usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf
Jul 03 13:20:30 DB-190 systemd[1]: Started MySQL Server.
--
# 等待MySQL启动
sleep 10
--
--确认 socket 文件存在
sudo ls -l /data/mysql/mysql.sock
--
[root@DB-190 home]# sudo ls -l /data/mysql/mysql.sock
srwxrwxrwx 1 mysql mysql 0 Jul 3 13:20 /data/mysql/mysql.sock
--连接测试
# 使用指定 socket 连接
sudo /usr/local/mysql/bin/mysql -u root -S /data/mysql/mysql.sock -p
--
sudo /usr/local/mysql/bin/mysql -u root -S /data/mysql/mysql.sock -p
Enter password:
--
# 设置密码-执行密码修改
sudo /usr/local/mysql/bin/mysqladmin -u root -S /data/mysql/mysql.sock password "P@ssw0rd1"
--
[root@DB-190 home]# sudo /usr/local/mysql/bin/mysqladmin -u root -S /data/mysql/mysql.sock password "P@ssw0rd2025"
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.
--
[root@DB-190 home]# sudo /usr/local/mysql/bin/mysql -u root -S /data/mysql/mysql.sock -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.4.5 MySQL Community Server - GPL
Copyright (c) 2000, 2025, 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>
10. 防火墙配置
--如果设置了firewalld
sudo firewall-cmd --permanent --add-port=3306/tcp
sudo firewall-cmd --reload
11. 验证安装
# 检查版本
mysql -u root -p -e "SELECT VERSION();"
# 检查数据目录
mysql -u root -p -e "SHOW VARIABLES LIKE 'datadir';"
# 检查服务状态
sudo systemctl status mysqld
--
[root@DB-190 home]# sudo firewall-cmd --permanent --add-port=3306/tcp
sudo firewall-cmd --reload
FirewallD is not running
FirewallD is not running
[root@DB-190 home]# # 检查版本
mysql -u root -p -e "SELECT VERSION();"
# 检查数据目录
mysql -u root -p -e "SHOW VARIABLES LIKE 'datadir';"
# 检查服务状态
sudo systemctl status mysqld
Enter password:
+-----------+
| VERSION() |
+-----------+
| 8.4.5 |
+-----------+
Enter password:
+---------------+--------------+
| Variable_name | Value |
+---------------+--------------+
| datadir | /data/mysql/ |
+---------------+--------------+
● mysqld.service - MySQL Server
Loaded: loaded (/etc/systemd/system/mysqld.service; enabled; preset: disabled)
Active: active (running) since Thu 2025-07-03 13:20:30 CST; 10min ago
Main PID: 35317 (mysqld)
Tasks: 36 (limit: 47342)
Memory: 514.1M
CPU: 2.066s
CGroup: /system.slice/mysqld.service
└─35317 /usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf
Jul 03 13:20:30 DB-190 systemd[1]: Started MySQL Server.
[root@DB-190 home]#
[root@DB-190 home]# tree /data/mysql/
/data/mysql/
├── auto.cnf
├── binlog.000001
├── binlog.index
├── ca-key.pem
├── ca.pem
├── client-cert.pem
├── client-key.pem
├── error.log
├── #ib_16384_0.dblwr
├── #ib_16384_1.dblwr
├── ib_buffer_pool
├── ibdata1
├── ibtmp1
├── #innodb_redo
………………
├── private_key.pem
├── public_key.pem
├── server-cert.pem
├── server-key.pem
├── sys
│ └── sys_config.ibd
├── undo_001
└── undo_002
5 directories, 185 files
12.最终配置检查表
项目 | 命令 | 预期值 |
数据目录 | mysql -uroot -p -e "SHOW VARIABLES LIKE 'datadir'" | /data/mysql/ |
版本号 | mysql -uroot -p -e "SELECT VERSION()" | 8.4.5 |
服务状态 | systemctl status mysqld | active (running) |
端口监听 | ss -tulnp | grep 3306 | LISTEN 状态 |
开机启动 | systemctl enabled mysqld | enabled |
注意:生产环境建议使用 --initialize 代替 --initialize-insecure 生成随机密码(查看 /data/mysql/error.log)