MySQL 8.0 Master-Slave
Server IP | Host Role |
---|---|
192.168.10.2 | MySQL Master |
192.168.10.3 | MySQL Slave |
MySQL Master
-
Download the MySQL installation package
- Server has internet access
- Server has no internet access
wget https://pdpublic.mingdao.com/private-deployment/offline/common/mysql-8.0.42-linux-glibc2.17-x86_64.tar.xz
# MySQL package download link, upload to the deployment server after downloading
https://pdpublic.mingdao.com/private-deployment/offline/common/mysql-8.0.42-linux-glibc2.17-x86_64.tar.xz -
Extract to the installation directory
tar -xvf mysql-8.0.42-linux-glibc2.17-x86_64.tar.xz
mv mysql-8.0.42-linux-glibc2.17-x86_64 /usr/local/mysql -
Create a MySQL user
useradd -U -M -s /sbin/nologin mysql
-
Create data and log directories and grant permissions
mkdir -p /data/mysql/ /data/logs/mysql
chown -R mysql:mysql /usr/local/mysql/ /data/mysql/ /data/logs/mysql/ -
Configure the systemd management file
cat > /etc/systemd/system/mysql.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
[Service]
User=mysql
Group=mysql
Type=forking
PIDFile=/usr/local/mysql/mysqld.pid
TimeoutSec=0
PermissionsStartOnly=true
ExecStart=/usr/local/mysql/bin/mysqld --daemonize --log-error=/data/logs/mysql/mysqld.log --datadir=/data/mysql --socket=/usr/local/mysql/mysql.sock --character-set-server=utf8mb4 --pid-file=/usr/local/mysql/mysqld.pid --server-id=1 --log-bin=mysql-bin --max_connections=2000 --slow_query_log=1 --slow_query_log_file=/data/logs/mysql/mysql-slow.log
LimitNOFILE=102400
Restart=on-failure
RestartPreventExitStatus=1
PrivateTmp=false
[Install]
WantedBy=multi-user.target
EOF -
Initialize
/usr/local/mysql/bin/mysqld --initialize --datadir=/data/mysql/ --user=mysql --log-error=/data/logs/mysql/mysqld.log
-
Start MySQL and enable autostart at boot
systemctl daemon-reload
systemctl enable mysql
systemctl start mysql -
Change MySQL password
/usr/local/mysql/bin/mysql -h127.0.0.1 -uroot -p$(grep 'temporary password' /data/logs/mysql/mysqld.log | awk '{print $NF}')
ALTER USER USER() IDENTIFIED BY '123456';
update mysql.user set host='%' where user='root';
FLUSH PRIVILEGES;
grant all privileges on *.* to 'root'@'%' with grant option;
FLUSH PRIVILEGES;- The root password changed in the command is
123456
, be sure to replace it during actual deployment.
- The root password changed in the command is
MySQL Slave
-
Download the MySQL installation package
- Server has internet access
- Server has no internet access
wget https://pdpublic.mingdao.com/private-deployment/offline/common/mysql-8.0.42-linux-glibc2.17-x86_64.tar.xz
# MySQL package download link, upload to the deployment server after downloading
https://pdpublic.mingdao.com/private-deployment/offline/common/mysql-8.0.42-linux-glibc2.17-x86_64.tar.xz -
Extract to the installation directory
tar -xvf mysql-8.0.42-linux-glibc2.17-x86_64.tar.xz
mv mysql-8.0.42-linux-glibc2.17-x86_64 /usr/local/mysql -
Create a MySQL user
useradd -U -M -s /sbin/nologin mysql
-
Create data and log directories and grant permissions
mkdir -p /data/mysql/ /data/logs/mysql
chown -R mysql:mysql /usr/local/mysql/ /data/mysql/ /data/logs/mysql/ -
Configure the systemd management file
cat > /etc/systemd/system/mysql.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
[Service]
User=mysql
Group=mysql
Type=forking
PIDFile=/usr/local/mysql/mysqld.pid
TimeoutSec=0
PermissionsStartOnly=true
ExecStart=/usr/local/mysql/bin/mysqld --daemonize --log-error=/data/logs/mysql/mysqld.log --datadir=/data/mysql --socket=/usr/local/mysql/mysql.sock --character-set-server=utf8mb4 --pid-file=/usr/local/mysql/mysqld.pid --server-id=2 --log-bin=mysql-bin --max_connections=2000 --slow_query_log=1 --slow_query_log_file=/data/logs/mysql/mysql-slow.log
LimitNOFILE=102400
Restart=on-failure
RestartPreventExitStatus=1
PrivateTmp=false
[Install]
WantedBy=multi-user.target
EOF -
Initialize
/usr/local/mysql/bin/mysqld --initialize --datadir=/data/mysql/ --user=mysql --log-error=/data/logs/mysql/mysqld.log
-
Start MySQL and enable autostart at boot
systemctl daemon-reload
systemctl enable mysql
systemctl start mysql -
Change MySQL password
/usr/local/mysql/bin/mysql -h127.0.0.1 -uroot -p$(grep 'temporary password' /data/logs/mysql/mysqld.log | awk '{print $NF}')
ALTER USER USER() IDENTIFIED BY '123456';
update mysql.user set host='%' where user='root';
FLUSH PRIVILEGES;
grant all privileges on *.* to 'root'@'%' with grant option;
FLUSH PRIVILEGES;- The root password changed in the command is
123456
, be sure to replace it during actual deployment.
- The root password changed in the command is
Configure Master-Slave
-
Log into the master node and configure the master-slave synchronization user
/usr/local/mysql/bin/mysql --socket=/usr/local/mysql/mysql.sock -uroot -p123456
# Create repl user
create user 'repl'@'%' identified by '123456';
grant replication slave on *.* to 'repl'@"%";
flush privileges;
show master status;- The master-slave synchronization user created in the command is
repl
, with the password123456
. Be sure to replace it during actual deployment.
- The master-slave synchronization user created in the command is
-
Log into the slave node and configure to enable master-slave
/usr/local/mysql/bin/mysql --socket=/usr/local/mysql/mysql.sock -uroot -p123456
change master to master_host="192.168.10.2",master_port=3306,master_user="repl",master_password="123456",master_log_file="mysql-bin.000001",master_log_pos=2936,get_master_public_key=1;
start slave;- In the change master statement, replace the
master_host
address with the actual master server IP during deployment. - The values for
master_log_file
andmaster_log_pos
in the change master statement should correspond with the output from executingshow master status;
on the master node. Adjust them according to actual deployment conditions.
- In the change master statement, replace the
-
Check master-slave synchronization status
show slave status\G
# Both Slave_IO_Running and Slave_SQL_Running being "Yes" in the output indicates normal master-slave synchronization