MySQL 5.7 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 with Internet Access
- Server without Internet Access
wget https://pdpublic.mingdao.com/private-deployment/offline/common/mysql-5.7.42-linux-glibc2.12-x86_64.tar.gz
# MySQL installation package download link. Upload to the deployment server after downloading.
https://pdpublic.mingdao.com/private-deployment/offline/common/mysql-5.7.42-linux-glibc2.12-x86_64.tar.gz -
Extract to the installation directory
tar -zxvf mysql-5.7.42-linux-glibc2.12-x86_64.tar.gz
mv mysql-5.7.42-linux-glibc2.12-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 -
Configure the systemd service 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=utf8 --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 it to start on 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}')
set PASSWORD = PASSWORD('123456');
GRANT ALL ON *.* to root@'%' IDENTIFIED BY '123456';
FLUSH PRIVILEGES;
quit;- The root user password changed in the command is
123456
. Please replace it during actual deployment.
- The root user password changed in the command is
MySQL Slave
-
Download the MySQL installation package
- Server with Internet Access
- Server without Internet Access
wget https://pdpublic.mingdao.com/private-deployment/offline/common/mysql-5.7.42-linux-glibc2.12-x86_64.tar.gz
# MySQL installation package download link. Upload to the deployment server after downloading.
https://pdpublic.mingdao.com/private-deployment/offline/common/mysql-5.7.42-linux-glibc2.12-x86_64.tar.gz -
Extract to the installation directory
tar -zxvf mysql-5.7.42-linux-glibc2.12-x86_64.tar.gz
mv mysql-5.7.42-linux-glibc2.12-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 -
Configure the systemd service 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=utf8 --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 it to start on 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}')
set PASSWORD = PASSWORD('123456');
GRANT ALL ON *.* to root@'%' IDENTIFIED BY '123456';
FLUSH PRIVILEGES;
quit;- The root user password changed in the command is
123456
. Please replace it during actual deployment.
- The root user password changed in the command is
Configure Master-Slave
-
Log in to the master node and configure the replication user
/usr/local/mysql/bin/mysql --socket=/usr/local/mysql/mysql.sock -uroot -p123456
grant replication slave on *.* to 'repl'@"%" identified by "123456";
flush privileges;
show master status;- The replication user created in the command is
repl
, with the password123456
. Please replace it during actual deployment.
- The replication user created in the command is
-
Log in to the slave node and configure 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=1265;
start slave;- In the
change master
statement, replace themaster_host
address with the actual master server IP during deployment. - The values of
master_log_file
andmaster_log_pos
in thechange master
statement should be based on the output ofshow master status;
executed on the master node. Adjust them according to the actual deployment.
- In the
-
Check replication status
show slave status\G
# In the output, both Slave_IO_Running and Slave_SQL_Running being Yes indicates normal replication.