MySQL 8.4 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 supports internet access
- Server does not support internet access
wget https://pdpublic.mingdao.com/private-deployment/offline/common/mysql-8.4.10-linux-glibc2.17-x86_64.tar.xz# Download link for the MySQL installation package; upload it to the deployment server after downloadinghttps://pdpublic.mingdao.com/private-deployment/offline/common/mysql-8.4.10-linux-glibc2.17-x86_64.tar.xz -
Extract the package to the installation directory
tar -xvf mysql-8.4.10-linux-glibc2.17-x86_64.tar.xzmv mysql-8.4.10-linux-glibc2.17-x86_64 /usr/local/mysql -
Create the MySQL user
useradd -U -M -s /sbin/nologin mysql -
Create data and log directories and grant permissions
mkdir -p /data/mysql/ /data/logs/mysqlchown -R mysql:mysql /usr/local/mysql/ /data/mysql/ /data/logs/mysql/ -
Create the
/etc/my.cnfconfiguration filecat > /etc/my.cnf <<'EOF'[mysqld]user = mysqlbasedir = /usr/local/mysqldatadir = /data/mysqlsocket = /usr/local/mysql/mysqld.sockpid-file = /usr/local/mysql/mysqld.pidlog-error = /data/logs/mysql/mysqld.logserver-id = 1bind-address = 0.0.0.0port = 3306skip-name-resolve = ONmax_connections = 5000character_set_server = utf8mb4collation_server = utf8mb4_0900_ai_cidefault_storage_engine = InnoDBinnodb_buffer_pool_size = 2Ginnodb_flush_log_at_trx_commit = 1slow_query_log = ONslow_query_log_file = /data/logs/mysql/mysql-slow.loglong_query_time = 1log_bin = /data/mysql/mysql-binsync_binlog = 1binlog_expire_logs_seconds = 2592000[client]port = 3306socket = /usr/local/mysql/mysqld.sockdefault-character-set = utf8mb4[mysql]default-character-set = utf8mb4EOF -
Configure the systemd management file
cat > /etc/systemd/system/mysql.service <<'EOF'[Unit]Description=MySQL Database ServerDocumentation=man:mysqld(8) http://dev.mysql.com/doc/After=network.targetWants=network-online.targetAfter=network-online.target[Service]Environment=MYSQLD_PARENT_PID=1User=mysqlGroup=mysqlType=forkingPIDFile=/usr/local/mysql/mysqld.pidExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf --daemonizeExecStop=/bin/kill -s SIGTERM $MAINPIDLimitNOFILE=102400LimitMEMLOCK=infinityRestart=on-failureOOMScoreAdjust=-500TimeoutSec=0[Install]WantedBy=multi-user.targetEOF -
Initialize MySQL
/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 automatically at boot
systemctl daemon-reloadsystemctl enable mysqlsystemctl start mysql -
Change the 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';grant all privileges on *.* to 'root'@'%' with grant option;- The root user password changed by the command is
123456; use a strong password in actual deployments. - If the password contains special characters, only
-or_are allowed. Do not use characters such as@ ! # &to avoid compatibility issues.
- The root user password changed by the command is
-
Test the database connection with the newly configured password
/usr/local/mysql/bin/mysql -h127.0.0.1 -uroot -p123456
MySQL Slave
-
Download the MySQL installation package
- Server supports internet access
- Server does not support internet access
wget https://pdpublic.mingdao.com/private-deployment/offline/common/mysql-8.4.10-linux-glibc2.17-x86_64.tar.xz# Download link for the MySQL installation package; upload it to the deployment server after downloadinghttps://pdpublic.mingdao.com/private-deployment/offline/common/mysql-8.4.10-linux-glibc2.17-x86_64.tar.xz -
Extract the package to the installation directory
tar -xvf mysql-8.4.10-linux-glibc2.17-x86_64.tar.xzmv mysql-8.4.10-linux-glibc2.17-x86_64 /usr/local/mysql -
Create the MySQL user
useradd -U -M -s /sbin/nologin mysql -
Create data and log directories and grant permissions
mkdir -p /data/mysql/ /data/logs/mysqlchown -R mysql:mysql /usr/local/mysql/ /data/mysql/ /data/logs/mysql/ -
Create the
/etc/my.cnfconfiguration fileThe
server-idvalues of the master and slave nodes must be unique. This slave usesserver-id = 2.cat > /etc/my.cnf <<'EOF'[mysqld]user = mysqlbasedir = /usr/local/mysqldatadir = /data/mysqlsocket = /usr/local/mysql/mysqld.sockpid-file = /usr/local/mysql/mysqld.pidlog-error = /data/logs/mysql/mysqld.logserver-id = 2bind-address = 0.0.0.0port = 3306skip-name-resolve = ONmax_connections = 5000character_set_server = utf8mb4collation_server = utf8mb4_0900_ai_cidefault_storage_engine = InnoDBinnodb_buffer_pool_size = 2Ginnodb_flush_log_at_trx_commit = 1slow_query_log = ONslow_query_log_file = /data/logs/mysql/mysql-slow.loglong_query_time = 1log_bin = /data/mysql/mysql-binsync_binlog = 1binlog_expire_logs_seconds = 2592000[client]port = 3306socket = /usr/local/mysql/mysqld.sockdefault-character-set = utf8mb4[mysql]default-character-set = utf8mb4EOF -
Configure the systemd management file
cat > /etc/systemd/system/mysql.service <<'EOF'[Unit]Description=MySQL Database ServerDocumentation=man:mysqld(8) http://dev.mysql.com/doc/After=network.targetWants=network-online.targetAfter=network-online.target[Service]Environment=MYSQLD_PARENT_PID=1User=mysqlGroup=mysqlType=forkingPIDFile=/usr/local/mysql/mysqld.pidExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf --daemonizeExecStop=/bin/kill -s SIGTERM $MAINPIDLimitNOFILE=102400LimitMEMLOCK=infinityRestart=on-failureOOMScoreAdjust=-500TimeoutSec=0[Install]WantedBy=multi-user.targetEOF -
Initialize MySQL
/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 automatically at boot
systemctl daemon-reloadsystemctl enable mysqlsystemctl start mysql -
Change the 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';grant all privileges on *.* to 'root'@'%' with grant option;- The root user password changed by the command is
123456; use a strong password in actual deployments. - If the password contains special characters, only
-or_are allowed. Do not use characters such as@ ! # &to avoid compatibility issues.
- The root user password changed by the command is
-
Test the database connection with the newly configured password
/usr/local/mysql/bin/mysql -h127.0.0.1 -uroot -p123456
Configure Master-Slave Replication
-
Log in to the master node and configure the replication user
/usr/local/mysql/bin/mysql --socket=/usr/local/mysql/mysqld.sock -uroot -p123456# Create replcreate user 'repl'@'%' identified by '123456';grant replication slave on *.* to 'repl'@"%";show binary log status\G- The replication user created by the command is
repl, and its password is123456; use a strong password in actual deployments. - If the password contains special characters, only
-or_are allowed. Do not use characters such as@ ! # &to avoid compatibility issues.
- The replication user created by the command is
-
Log in to the slave node and configure replication
/usr/local/mysql/bin/mysql --socket=/usr/local/mysql/mysqld.sock -uroot -p123456change replication source tosource_host='192.168.10.2',source_port=3306,source_user='repl',source_password='123456',source_log_file='mysql-bin.000001',source_log_pos=2936,get_source_public_key=1;start replica;- Replace
source_hostin thechange replication source tostatement with the actual IP address of the source server. - The
source_log_fileandsource_log_posvalues come from the output ofshow binary log status\Gon the source node. Use the actual values from your deployment.
- Replace
-
Check the master-slave synchronization status
show replica status\G# In the output, Replica_IO_Running and Replica_SQL_Running being Yes means that master-slave synchronization is normal.