MySQL Master-Slave Replication Setup: GTID, Monitoring, Errors
Note: when the MySQL primary server crashes, the business loses not only time but also transactions — recovery from backups can take hours. On one project with a load of 5000 queries per second, the downtime lasted 4 hours, leading to losses. Replication MySQL Master-Slave solves this issue: the replica copies the binary log from the master and applies events with a lag in milliseconds. Professional setup of Master-Slave replication is not just a backup server, but also the ability to distribute read queries, reducing load on the primary. In this article — practical configuration, typical errors, lag monitoring, and what we include in the work. Stable replication with GTID reduces failover time to minutes, and under normal load, the lag does not exceed 1–2 seconds. Professional setup starts at $1,500 for a basic configuration (one master, one replica).
What Does Master-Slave Replication Provide?
- Fault tolerance: when the master fails, we switch to the replica within minutes.
- Read scaling: we send SELECTs to multiple replicas, leaving the master for writes.
- Backups without downtime: we take a dump from the replica without affecting production.
Most projects come to replication after the first failure. We recommend setting it up from the start: it is cheaper and faster than emergency data recovery.
Step-by-Step MySQL Replication Setup
Master Configuration
On the master, enable the binary log and set server_id. Example configuration:
# /etc/mysql/mysql.conf.d/mysqld.cnf
[mysqld]
server_id = 1
log_bin = /var/log/mysql/mysql-bin
binlog_format = ROW
binlog_row_image = MINIMAL
sync_binlog = 1
expire_logs_days = 7
Create a user with REPLICATION SLAVE privileges:
CREATE USER 'repl'@'10.0.0.2' IDENTIFIED WITH caching_sha2_password BY 'repl_password';
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'10.0.0.2';
FLUSH PRIVILEGES; Taking an Initial Dump
To initialize the replica, you need a dump with the binlog position. Two methods:
| Method | Command | Locking | Suitable for |
|---|---|---|---|
| mysqldump | mysqldump --all-databases --master-data=2 --single-transaction --routines --triggers --events |
Minimal (short lock) | Up to 50 GB |
| Percona XtraBackup | xtrabackup --backup --target-dir=/backup/ |
No locks | Any, including 500+ GB |
The binlog position will be at the start of the dump file or in xtrabackup_binlog_info. On a recent project with 500 GB of data, XtraBackup reduced dump time by 3 times compared to mysqldump.
Replica Configuration
On the replica — its own server_id and read_only mode:
# /etc/mysql/mysql.conf.d/mysqld.cnf
[mysqld]
server_id = 2
relay_log = /var/log/mysql/mysql-relay
read_only = ON
log_bin = /var/log/mysql/mysql-bin
log_slave_updates = ONRestore the dump and start replication:
mysql -u root -p < /backup/full_dump.sql
mysql -e "CHANGE REPLICATION SOURCE TO SOURCE_HOST='10.0.0.1', SOURCE_USER='repl', SOURCE_PASSWORD='repl_password', SOURCE_LOG_FILE='mysql-bin.000042', SOURCE_LOG_POS=12345, SOURCE_SSL=1; START REPLICA; SHOW REPLICA STATUS\G"Check: Replica_IO_Running: Yes, Replica_SQL_Running: Yes, Seconds_Behind_Source — lag in seconds.
GTID or Positional Replication?
GTID (Global Transaction ID) — global transaction identifiers available since MySQL 5.6 and above. Their main advantage is that you don't need to manually specify the binlog file and position. During failover, the replica itself determines which transactions to apply. MySQL Documentation: GTID transactions are globally unique. Thanks to GTID, failover time is reduced from 10–15 minutes to 2–3 minutes, which reduces losses in high-load projects. GTID configuration:
gtid_mode = ON enforce_gtid_consistency = ON The CHANGE command uses SOURCE_AUTO_POSITION=1 instead of specifying file and position. GTID also simplifies skipping erroneous transactions — instead of SQL_SLAVE_SKIP_COUNTER, use SET GTID_NEXT='...'; BEGIN; COMMIT;.
If your MySQL version is 5.6+ and you plan frequent master switches, choose GTID. For older versions (5.5 and below), only positional replication is available. During the design phase, we help determine the optimal option for your load and SLA.
Replication Lag Monitoring and Management
Lag is the time difference between commit of a transaction on the master and its application on the replica. It is measured in seconds via Seconds_Behind_Source. Under normal load, lag does not exceed 1–2 seconds. If lag increases, check:
- disk performance on the replica;
- transaction size (large DDL can increase lag);
- network latency.
Lag monitoring — via mysqld_exporter metric mysql_slave_status_seconds_behind_master. Alert at lag > 30 seconds and > 60 seconds.
For high lag (>30 seconds), first check replica load: if CPU or I/O are maxed out, increase resources or add parallel workers (slave_parallel_workers). If the problem is network (latency >5 ms), consider a dedicated VLAN or geographic placement of replicas.
To speed up replication, use binlog_format = ROW and binlog_row_image = MINIMAL — this reduces the volume of transmitted data. Also increase slave_parallel_workers to 4-8 if MySQL 8.0+. On a powerful server, this gives a performance boost of up to 50%.
Typical Failure Scenarios
Table of failure scenarios and solutions
| Scenario | Cause | Solution |
|---|---|---|
| Duplicate entry | Direct write on replica | Enable read_only and skip the transaction |
| Row not found | Deletion on master before application | Synchronize data with master |
| Binlog position loss | Log truncation on master | Reinitialize replica from a fresh backup |
If replication stops, check SHOW REPLICA STATUS\G — key fields Slave_IO_Running and Slave_SQL_Running should be Yes.
Work Process and What's Included
- Analysis (2–4 hours): assessment of current schema, database size, network latency.
- Design (1–2 days): choice between positional and GTID, failover planning.
- Implementation (2–5 days): server configuration, user creation, dump, replication start.
- Testing (4–8 hours): replication speed check, lag test, switch to replica.
- Documentation and training (1 day): runbook handover, alert setup.
For a 500 GB database with 10,000 queries per second load, replication is set up in 3–4 days. Note: what's included in the work:
- Configuration of 2–4 MySQL servers considering topology (master + 1–3 replicas).
- Choice of dump method: mysqldump for databases up to 50 GB, Percona XtraBackup for databases up to 1 TB and more.
- GTID and
enforce_gtid_consistencysetup (if MySQL 5.6+). - Lag monitoring via Prometheus/Grafana: metric
mysql_slave_status_seconds_behind_master, alerts at lag >30 and >60 seconds. - Runbook for typical operations: master switch (manual failover), recovery after failure, skipping erroneous transactions.
- Engineer consultation after launch.
Contact us to set up replication for your project — we will assess the current infrastructure and propose an optimal solution.
Our Advantages
Our engineers are certified DBAs with 5+ years of experience in MySQL consulting on projects with loads up to 100,000 queries per second. We guarantee that lag will not exceed 30 seconds under normal load. More than 50 successful projects confirm our professionalism.
Order professional MySQL replication setup — our engineer will contact you within an hour and offer a solution for your tasks.







