MySQL: MySQL Master-Slave Replication and High-Availability…

Last updated: 2026-08-26

Master-slave replication is the foundation of MySQL high availability—both read-write separation and failover rely on it.

This lesson explains the principles and configuration of master-slave replication.

1. What You'll Learn



2. A True Story

(1) Pain Point: A single server cannot handle the read and write load

An e-commerce platform has only one database server, so during major sales events, all read and write requests are concentrated on a single machine. Order queries slow down, write operations get queued, and the CPU reaches full capacity—the database becomes the bottleneck for the entire system. What’s even more dangerous is that if this machine goes down, the entire business comes to a complete halt.

(2) A Solution Using Master-Slave Replication and Read-Write Separation

Deploy master-slave replication: Writes go to the master database, and reads go to the slave database. If the master database goes down, the slave database is promoted to master, ensuring business continuity.

Dimension Single-server Master-slave replication + read-write separation
Read Performance Single-Server Limit Horizontal Scaling (Adding Slave Databases)
Write Performance Read-Write Contention Write-Exclusive Primary Database
Availability 99% 99.9%
Disaster Recovery Manual Recovery (hourly) Automatic Failover (minutely)


3. The Principle of Replication

100%
graph LR
    A[Master<br/>Primary Database] -->|binlog| B[Slave IO Thread]
    B -->|relay log| C[Slave SQL Thread]
    C -->|Replay| D[Slave Data<br/>Data from the database]
Step Description
1 Master writes to binlog
2 Slave IO thread pulls binlog
3 Write to local relay log
4 Slave SQL thread replays relay log


4. Configuring Master-Slave Replication

▶ Example: Master Configuration

INI
# my.cnf
[mysqld]
server-id = 1
log_bin = mysql-bin
binlog_format = ROW
SQL
-- Create a replication user
CREATE USER 'repl'@'%' IDENTIFIED BY 'ReplPass123!';
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%';

-- View master status
SHOW MASTER STATUS;

Output:

TEXT 📖 Display only
Configuration saved. Restart the service to apply changes.

▶ Example: Slave Configuration

INI
# my.cnf
[mysqld]
server-id = 2
relay_log = relay-bin
SQL
-- Configure master-slave relationship
CHANGE MASTER TO
    MASTER_HOST='master_ip',
    MASTER_USER='repl',
    MASTER_PASSWORD='ReplPass123!',
    MASTER_LOG_FILE='mysql-bin.000001',
    MASTER_LOG_POS=154;

-- Start the slave database
START SLAVE;

-- Check slave database status
SHOW SLAVE STATUS\G

Output:

TEXT 📖 Display only
Output displayed
💡 \G is a MySQL command-line client directive that displays query results vertically. It only works in the mysql CLI, not in application code or GUI tools.



5. Separation of Read and Write Operations

100%
graph TB
    A[Applications] -->|Write| B[Master]
    A -->|Read| C[Slave 1]
    A -->|Read| D[Slave 2]
    B -->|Replicate| C
    B -->|Replicate| D


6. Semi-synchronous Replication

▶ Example: Enabling Semi-Synchronous Replication

SQL
-- On the master: install and enable the semi-sync plugin
INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so';
SET GLOBAL rpl_semi_sync_master_enabled = 1;
SET GLOBAL rpl_semi_sync_master_timeout = 10000;  -- 10 seconds

-- On the slave: install and enable the semi-sync plugin
INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so';
SET GLOBAL rpl_semi_sync_slave_enabled = 1;
STOP SLAVE IO_THREAD;
START SLAVE IO_THREAD;
▶ Try it Yourself

Output:

TEXT 📖 Display only
Output displayed
Copy Method Description
Asynchronous replication Master does not wait for slave confirmation (default)
Semi-synchronous replication Returns only after at least one slave acknowledges receipt
Full synchronous replication All slaves must acknowledge (poor performance)


7. MySQL Group Replication

SQL
-- Install the Group Replication plugin
INSTALL PLUGIN group_replication SONAME 'group_replication.so';

-- Start Group Replication
SET GLOBAL group_replication_bootstrap_group = ON;
START GROUP_REPLICATION;
SET GLOBAL group_replication_bootstrap_group = OFF;

❓ FAQ

Q What should I do about master-slave latency?
A Check the network, optimize large transactions, and use parallel replication.
Q What should I do if the primary database goes down?
A Manual or automatic failover (MHA/Orchestrator/ProxySQL).
Q Can there be multiple masters?
A Group Replication supports multi-master mode, but single-master mode is recommended.
Q What should I do if there is a delay in master-slave replication?
A Perform critical reads from the master database, use semi-synchronous replication, enable parallel replication, and optimize large transactions.
Q What should I do if the primary database goes down?
A Promote a secondary database to the primary, either manually or automatically using MHA/Orchestrator.

📖 Summary


📝 Exercises

  1. Basic Question (Difficulty: ⭐): Configure a master-slave replication environment.

  2. Advanced Problem (Difficulty ⭐⭐): Verify master-slave data synchronization.

  3. Challenge (Difficulty: ⭐⭐⭐): Simulate a primary database failure and perform a manual failover.

Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏