Configure MySQL Replication on Linux: A Clear, Step-by-Step Guide
Ready to scale reads and boost availability without complicating writes? This clear, step-by-step guide walks you through MySQL replication on Linux—from binlogs and GTIDs to practical setup and operational tips—so you can deploy robust replica topologies with confidence.
Setting up MySQL replication on Linux is a proven method to improve read scalability, increase availability, and enable safe backups without impacting production write throughput. This guide walks you through the underlying concepts, practical configuration steps, operational considerations, and selection advice so system administrators, developers, and site owners can deploy robust replication topologies with confidence.
Understanding the fundamentals
MySQL replication copies data from one server (commonly called the primary or master) to one or more replica servers (commonly called secondary or slaves). There are several replication modes:
- Binary log (binlog) based replication: The traditional statement-based or row-based replication using the binary log.
- GTID (Global Transaction ID) replication: A modern, more robust approach that tracks transactions using GTIDs to simplify failover and consistency checks.
- Semi-synchronous replication: Adds durability by ensuring at least one replica acknowledges receipt of a transaction before commit returns to the client.
At a high level, the master writes all data-changing events to its binary log. Replicas connect to the master, request the binlog events, and apply them locally. Key components to understand:
- binlog_format: ROW, STATEMENT, or MIXED. ROW is often recommended for correctness.
- server-id: Unique numeric ID per server, required for replication.
- relay log: Logs downloaded from the master and applied locally on the replica.
- GTIDs: Enable transactional identities for easier promotion and consistency.
Typical use cases and when to choose replication
Replication is suitable for multiple scenarios:
- Read scaling: Offload read-heavy queries to replicas to reduce load on the primary.
- High availability: Replicas can be promoted to primary in failover procedures (manual or orchestrated).
- Analytics and reporting: Run expensive analytics queries on replicas without affecting OLTP workloads.
- Backup and disaster recovery: Use replicas for backups to avoid locking the primary during snapshot operations.
However, replication is not a substitute for synchronous clustering. There is replication lag potential; applications requiring strict synchronous durability should consider cluster solutions (Group Replication, Galera) or storage-level replication.
Advantages and trade-offs
Advantages:
- Simple to set up: Built into MySQL and MariaDB.
- Flexible topologies: One-to-many, chain, fan-in (multi-source), and circular replication.
- Low overhead: Read-only replicas impose minimal load on primary beyond serving binlog events.
Trade-offs and limitations:
- Eventual consistency: Replicas may lag and not reflect the latest writes immediately.
- Operational complexity: Managing failover, split-brain, and replication errors requires careful planning.
- Binary log storage: Primary must retain binlogs until replicas have fetched them to avoid broken replication.
Preparation: prerequisites and environment
Before starting, verify:
- Both master and replica run compatible MySQL/MariaDB versions.
- Network connectivity and proper firewall rules (default MySQL port 3306) exist between servers.
- Time synchronization (NTP or chrony) is configured to reduce skew for logging and monitoring.
- You have root or mysql administrative credentials on both nodes.
On Linux, ensure MySQL is installed and managed via systemd (service mysqld or mysql depending on distribution). Use package repositories or vendor-provided packages for stable versions.
Step-by-step configuration (Master and Replica)
1) Configure the master
Edit the MySQL configuration file (typically /etc/mysql/my.cnf or /etc/mysql/mysql.conf.d/mysqld.cnf) and add or update the following under [mysqld]:
- server-id = 1
- log_bin = /var/log/mysql/mysql-bin.log
- binlog_format = ROW
- expire_logs_days = 7 (or use binlog_expire_logs_seconds)
- gtid_mode = ON
enforce_gtid_consistency = ON (if you choose GTID replication)
Restart MySQL: sudo systemctl restart mysql (or mysqld).
Create a replication user with limited privileges:
- From the MySQL prompt on master:
CREATE USER ‘repl’@’REPLICA_IP’ IDENTIFIED BY ‘strong_password’;
GRANT REPLICATION SLAVE ON . TO ‘repl’@’REPLICA_IP’;
FLUSH PRIVILEGES;
Take a consistent backup. Two common methods:
- mysqldump with –master-data:
mysqldump –single-transaction –master-data=2 –databases mydb > dbdump.sql
This embeds the binary log coordinates into the dump which you will use on the replica. - Physical snapshot (LVM/ZFS): Useful for large datasets; capture binlog position before snapshot.
2) Configure the replica
On the replica’s my.cnf:
- server-id = 2 (must differ from master and other replicas)
- relay_log = /var/log/mysql/mysql-relay-bin
- read_only = ON (prevents accidental writes; sysadmins may permit SUPER)
- gtid_mode = ON and enforce_gtid_consistency = ON if using GTIDs
Restart MySQL on the replica.
Load the backup onto the replica:
- mysql < dbdump.sql
If using mysqldump with –master-data, note the CHANGE MASTER TO command embedded or record the log file and position. For GTID replication, you will use GTID-based setup.
3) Start replication
Basic non-GTID example:
- On replica:
CHANGE MASTER TO MASTER_HOST=’MASTER_IP’, MASTER_USER=’repl’, MASTER_PASSWORD=’strong_password’, MASTER_LOG_FILE=’mysql-bin.00000X’, MASTER_LOG_POS=YYYY;
START SLAVE;
GTID-based example:
- On replica:
RESET SLAVE ALL; (use carefully—resets slave state)
CHANGE MASTER TO MASTER_HOST=’MASTER_IP’, MASTER_USER=’repl’, MASTER_PASSWORD=’strong_password’, MASTER_AUTO_POSITION=1;
START SLAVE;
Check replication status:
- SHOW SLAVE STATUSG (or SHOW REPLICA STATUSG in newer versions). Key fields:
– Seconds_Behind_Master (lag indicator)
– Slave_IO_Running and Slave_SQL_Running (both should be Yes)
– Last_Error (if replication stopped)
Hardening and operational tips
Security and reliability practices:
- Use restricted replication user: GRANT REPLICATION SLAVE only and limit host by IP.
- Enable SSL/TLS for replication: Configure server/client certificates and set MASTER_SSL=1 and related options.
- Monitor lag and errors: Use monitoring tools (Prometheus exporters, Percona Monitoring and Management, or Datadog) to alert on replication state changes.
- Set binlog retention: Ensure primary retains binlogs long enough for slow replicas (binlog_expire_logs_seconds or expire_logs_days).
- Backups: Use replicas for backups; avoid heavy backup load on primary.
Handling common issues:
- If Slave_IO_Running = No, check network connectivity, firewall, and replication user credentials.
- If Slave_SQL_Running = No, inspect Last_SQL_Error to resolve data conflicts, missing tables, or corrupted relay logs. Commands like STOP SLAVE; RESET SLAVE; CHANGE MASTER; START SLAVE; can help after fixing root cause.
- Inconsistencies after manual DDL changes: prefer applying schema changes via tools that support online migration (gh-ost, pt-online-schema-change) to avoid replication breaks.
Advanced topics and topology choices
Choose topology based on goals:
- Master > multiple replicas: Common and simple for read scaling.
- Chained replication: Replica as intermediate master for downstream replicas—reduces load on primary but increases complexity.
- Multi-source replication: A single replica can receive binlogs from multiple masters (useful for consolidating data streams).
- Group Replication / Galera: For synchronous multi-master clustering when strict consistency is required.
Performance tuning:
- Use row-based replication to avoid non-deterministic statement issues.
- Optimize binlog I/O: place binlog on fast storage; ensure adequate fsync settings for durability vs throughput.
- Tune replica parallelism: slave_parallel_workers (MySQL) or slave_parallel_threads (MariaDB) to apply independent transactions in parallel, reducing apply lag for multi-partitioned workloads.
Choosing servers and hosting considerations
When selecting instances for primary and replicas, evaluate:
- CPU and memory: Adequate for workload and InnoDB buffer pool to keep hot working set in memory.
- Disk performance: Use SSD-backed NVMe where possible for low-latency I/O and faster transaction commit.
- Network: Low-latency, stable network between master and replicas reduces replication lag—consider colocated instances or high-bandwidth links.
- Snapshots and backups: Ensure the provider supports consistent snapshots (for LVM/ZFS or provider-level snapshots).
For users looking for reliable VPS providers, consider regional presence and network performance—for example, VPS.DO offers a range of VPS products including USA VPS, which can be suitable for deploying primary and replica nodes across multiple locations.
Summary and recommended best practices
MySQL replication on Linux delivers a flexible, low-overhead mechanism to scale reads, improve availability, and enable safer backup workflows. To build a robust replication setup:
- Enable binlog and use ROW format for correctness.
- Consider GTID mode to simplify failover and recovery.
- Secure replication with dedicated users and SSL/TLS.
- Use replicas for backups and reporting to reduce primary load.
- Monitor replication closely and tune for parallel apply where appropriate.
Deploying on well-performing VPS instances with reliable network and fast storage reduces replication lag and operational headaches. If you want to experiment or run production workloads, check hosting options such as VPS.DO and their USA VPS offerings for flexible instance choices that suit primary/replica deployment strategies.