Setting Up MySQL 8.0 Master-Slave Replication (Single Master-Single Slave) on openEuler 22.03
| Role | Operating System | IP Address | MySQL Version |
|---|---|---|---|
| Master Node | openEuler 22.03 LTS-SP2 | 192.168.48.131 | 8.0.37 for Linux on x86_64 (Source distribution) |
| Slave Node | openEuler 22.03 LTS-SP2 | 192.168.48.132 | 8.0.37 for Linux on x86_64 (Source distribution) |
# Install MySQL server
yum install -y mysql-server
# Allow MySQL port 3306 in firewall (skip if firewall is inactive)
firewall-cmd --zone=public --add-port=3306/tcp --permanent
firewall-cmd --reload
firewall-cmd --zone=public --list-ports | grep 3306
# Disable SELinux
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
reboot
# Start MySQL service
systemctl start mysqld
# Verify MySQL startup
ps -ef | grep mysql
# Enable MySQL auto-start on boot
systemctl enable mysqld
# Log into MySQL
mysql -uroot -p
First login to this MySQL version has an empty password; press Enter directly.
Master Node Configuration
1.1 Modify MySQL Configuration File
cat >> /etc/my.cnf <<EOF
server-id=1
read-only=0
binlog_format=MIXED
log-slave-updates=true
log-bin=mysql-repl-log
EOF
1.2 Restart MySQL Service
systemctl restart mysqld
1.3 Create Replication User
CREATE USER 'luna'@'192.168.48.132' IDENTIFIED WITH mysql_native_password BY 'Luna@456';
GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'luna'@'192.168.48.132';
FLUSH PRIVILEGES;
1.4 Check Binary Log Coordinates
SHOW MASTER STATUS\G;
- Current binary log filename:
mysql-repl-log.000001 - Current offset:
2405
Slave Node Configuration
2.1 Modify MySQL Configuration File
cat >> /etc/my.cnf <<EOF
server-id=2
EOF
2.2 Restart MySQL Service
systemctl restart mysqld
2.3 Configure Master-Slave Connection
CHANGE MASTER TO
MASTER_HOST='192.168.48.131',
MASTER_USER='luna',
MASTER_PASSWORD='Luna@456',
MASTER_LOG_FILE='mysql-repl-log.000001',
MASTER_LOG_POS=2405;
2.4 Start Replication
START SLAVE;
SHOW SLAVE STATUS\G;
Both Slave_IO_Running and Slave_SQL_Running should show Yes.
Validate Replication
3.1 Create Test Data on Master
CREATE DATABASE dev_test;
USE dev_test;
CREATE TABLE employee (
emp_id INT,
emp_name VARCHAR(50)
) CHARSET=utf8;
INSERT INTO employee VALUES
(101, 'Alice'),
(102, 'Bob'),
(103, 'Charlie');
3.2 Verify Data on Slave
SHOW DATABASES;
SELECT * FROM dev_test.employee;
The dev_test database and employee table with inserted records should appear on the slave.
Troubleshooting
Slave_IO_Running: No
Common Causes & Fixes
- Incorrect binary log coordinates: Re-check coordinates from
SHOW MASTER STATUSon the master and reconfigure the slave. - Failed remote connection: Verify firewall, SELinux, and network connectivity.
- Duplicate server UUID/MAC address: Generated if nodes are cloned; modify
/var/lib/mysql/auto.cnfto change UUID.
Slave_IO_Running: Connecting
Cause: The replication user was created using the default caching_sha2_password plugin, which requires a secure TLS connectino for remote access.
Fix:
ALTER USER 'luna'@'192.168.48.132' IDENTIFIED WITH mysql_native_password BY 'Luna@456';
Authenticasion Plugin Difference
CREATE USER 'luna'@'192.168.48.132' IDENTIFIED BY 'Luna@456';
CREATE USER 'luna'@'192.168.48.132' IDENTIFIED WITH mysql_native_password BY 'Luna@456';
- First Statement: Uses MySQL 8.0+ default
caching_sha2_password, which is more secure but requires TLS for remote access. - Second Statement: Uses legacy
mysql_native_password, compatible with older MySQL clients and remote connections with out TLS.