ubuntu 24.04 部署 mysql 8.4.3 LTS
安装依赖
apt -y install libaio1t64
ln -sv /usr/lib/x86_64-linux-gnu/libaio.so.1t64.0.2 /usr/lib/x86_64-linux-gnu/libaio.so.1
创建用户
groupadd -g 28 -o -r mysql && useradd -M -N -g mysql -o -r -d /data/mysql -s /bin/false -c "MySQL Server" -u 28 mysql
下载 mysql
wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.4.3-linux-glibc2.28-x86_64.tar.xz
解压 mysql
tar xvf mysql-8.4.3-linux-glibc2.28-x86_64.tar.xz -C /usr/local/
ln -sv /usr/local/mysql-8.4.3-linux-glibc2.28-x86_64 /usr/local/mysql
创建目录
mkdir -pv /data/apps/mysql/{data,logs,tmp,mysql-files} && chown -R mysql:mysql /data/apps/mysql && chmod 750 /data/apps/mysql/mysql-files && chmod 750 /data/apps/mysql/data
创建配置文件
touch /data/apps/mysql/my.cnf && chown root:root /data/apps/mysql/my.cnf && chmod 644 /data/apps/mysql/my.cnf
[mysqld]
datadir=/data/apps/mysql/data
socket=/tmp/mysql.sock
port=3306
log-error=/data/apps/mysql/logs/localhost.localdomain.err
user=mysql
secure_file_priv=/data/apps/mysql/mysql-files
local_infile=OFF
初始化 mysql
/usr/local/mysql/bin/mysqld --defaults-file=/data/apps/mysql/my.cnf --initialize
2024-11-01T15:15:49.899267+08:00 0 [System] [MY-015017] [Server] MySQL Server Initialization - start.
2024-11-01T15:15:49.901523+08:00 0 [System] [MY-013169] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.4.3) initializing of server in progress as process 2843
2024-11-01T15:15:49.910424+08:00 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2024-11-01T15:15:50.190221+08:00 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2024-11-01T15:15:51.836977+08:00 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: Qmxm(vpiy03!
2024-11-01T15:15:53.897621+08:00 0 [System] [MY-015018] [Server] MySQL Server Initialization - end.
mysqld.service
cat > /lib/systemd/system/mysqld.service << EOF
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
# Have mysqld write its state to the systemd notify socket
Type=notify
# Disable service start and stop timeout logic of systemd for mysqld service.
TimeoutSec=0
PIDFile=/data/apps/mysql/data/mysqld.pid
# Start main service
ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/data/apps/mysql/my.cnf $MYSQLD_OPTS --pid-file=/data/apps/mysql/data/mysqld.pid $MYSQLD_OPTS
# Use this to switch malloc implementation
EnvironmentFile=-/etc/sysconfig/mysql
# Sets open_files_limit
LimitNOFILE = 10000
Restart=on-failure
RestartPreventExitStatus=1
# Set environment variable MYSQLD_PARENT_PID. This is required for restart.
Environment=MYSQLD_PARENT_PID=1
PrivateTmp=false
EOF
启用 mysql
systemctl enable mysqld --now
Created symlink /etc/systemd/system/multi-user.target.wants/mysqld.service → /usr/lib/systemd/system/mysqld.service.
安全设置
/usr/local/mysql/bin/mysql_secure_installation -S /data/apps/mysql/mysql.sock
登录验证
/usr/local/mysql/bin/mysql -S /data/apps/mysql/mysql.sock -uroot -p
修改 root 密码
ALTER USER 'root'@'localhost' IDENTIFIED BY 'password';
参考文档
https://dev.mysql.com/doc/mysql-secure-deployment-guide/8.0/en/secure-deployment-install.html