目录
1. 确认操作系统⽀持列表
https://www.mysql.com/support/supportedplatforms/database.html
MySQL :: MySQL Product Support EOL Announcements
2. 确认 MySQL 版本
MySQL的版本命名遵循⼀种特定的模式,通常采⽤X.Y.Z的形式,其中X、Y和Z分别代表主版本号、次版本号和修 订版本号。
3. MySQL 的下载
官⽅⽂档:
https://dev.mysql.com/doc/refman/8.0/en/
下载地址:
https://dev.mysql.com/downloads/mysql/
如何查看 glibc 的版本
ldd --version
4. MySQL ⼆进制包安装
4.1. 创建⽤户
# groupadd mysql
# useradd -g mysql mysql
4.2. 解压⼆进制包
# cd /usr/local/
# wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.35-linux-glibc2.17-
x86_64.tar.xz
# tar xvf mysql-8.0.35-linux-glibc2.17-x86_64.tar.xz
# ln -s mysql-8.0.35-linux-glibc2.17-x86_64 mysql
4.3. 编辑配置⽂件
# vim /etc/my.cnf
[client]
socket = /data/mysql/3306/data/mysql.sock
[mysqld]
basedir = /usr/local/mysql
datadir = /data/mysql/3306/data
user = mysql
port = 3306
socket = /data/mysql/3306/data/mysql.sock
log_error = /data/mysql/3306/data/mysqld.err
log_timestamps = system
log-bin = mysql-bin
server-id = 1
4.4 创建数据⽬录
# mkdir -p /data/mysql/3306/data
# chown mysql.mysql /data/mysql/3306/data/
4.5 初始化实例
# /usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf --initialize
4.6 启动实例
# /usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf &
4.7 登录实例
# grep password /data/mysql/3306/data/mysqld.err
# /usr/local/mysql/bin/mysql -uroot -S /data/mysql/3306/data/mysql.sock -p
4.8 修改密码
alter user user() identified by '123456';
如何判断实例是否启动成功 1. 查看错误⽇志。 2. 查看 mysqld 进程是否启动。
如何定位实例启动失败的原因 1. 查看错误⽇志。
2. 通过 mysqld 启动。# /usr/local/mysql/bin/mysqld --defaults-file=my.cnf &
3. 只指定⼏个必要的参数启动。
/usr/local/mysql/bin/mysqld --no-defaults --basedir=/usr/local/mysql -- datadir=/data/mysql/3306/data/ --user=mysql
5. Systemd
创建 systemd 服务配置⽂件
# vim /usr/lib/systemd/system/mysqld.service
[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
Type=forking
PIDFile=/data/mysql/3306/data/mysqld.pid
# Disable service start and stop timeout logic of systemd for mysqld service.
TimeoutSec=0
# Start main service
ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf --pidfile=/data/mysql/3306/data/mysqld.pid --daemonize $MYSQLD_OPTS
# Use this to switch malloc implementation
EnvironmentFile=-/etc/sysconfig/mysql
# Sets open_files_limit
LimitNOFILE = 65535
Restart=on-failure
RestartPreventExitStatus=1
PrivateTmp=false
使⽤ systemd 管理服务
# systemctl daemon-reload
# systemctl start mysqld
# systemctl stop mysqld
# systemctl status mysqld
设置开机⾃启动
# systemctl enable mysqld