MySQL replication setup: Master–Slave and Master–Master Print

  • 0

MySQL replication allows data from one database server to be copied automatically to one or more other servers. Replication is commonly used for redundancy, scalability, backups, and read load distribution.

This article describes how to configure the two most common replication topologies: Master–Slave and Master–Master.

Replication concepts

MySQL replication is based on binary logs (binlog). The primary server writes changes to its binary log, and replicas read and apply these changes.

  • Master: server that writes data and produces binary logs
  • Slave / Replica: server that reads and applies changes
  • Position: exact point in the binary log where replication starts

Prerequisites

  • Two or more MySQL servers with network connectivity
  • Unique server-id on each server
  • Binary logging enabled
  • MySQL versions compatible with each other
  • Port 3306 open between servers

Master–Slave replication

This is the most common setup. The master handles all writes, while one or more slaves replicate data and can be used for read-only queries or backups.

Step 1: Configure the master

Edit the MySQL configuration file (usually /etc/mysql/my.cnf or /etc/mysql/mysql.conf.d/mysqld.cnf):

[mysqld]
server-id=1
log-bin=mysql-bin

Restart MySQL after making changes.

Step 2: Create replication user

CREATE USER 'replica'@'%' IDENTIFIED BY 'strong_password';
GRANT REPLICATION SLAVE ON *.* TO 'replica'@'%';
FLUSH PRIVILEGES;

Step 3: Get master status

FLUSH TABLES WITH READ LOCK;
SHOW MASTER STATUS;

Note the File and Position values. Keep the session open.

Step 4: Prepare slave

Configure the slave server:

[mysqld]
server-id=2

Restart MySQL on the slave.

Step 5: Configure replication on slave

CHANGE MASTER TO
MASTER_HOST='master_ip',
MASTER_USER='replica',
MASTER_PASSWORD='strong_password',
MASTER_LOG_FILE='mysql-bin.000001',
MASTER_LOG_POS=12345;
START SLAVE;
SHOW SLAVE STATUS\G

Both Slave_IO_Running and Slave_SQL_Running should be set to Yes.

Master–Master replication

In a Master–Master setup, both servers act as master and slave simultaneously. This allows writes on both nodes, but requires careful configuration to avoid conflicts.

Important considerations

  • Auto-increment values must not collide
  • Conflict handling is manual
  • Not recommended without clear understanding of risks

Step 1: Configure both servers

On server A:

[mysqld]
server-id=1
log-bin=mysql-bin
auto-increment-increment=2
auto-increment-offset=1

On server B:

[mysqld]
server-id=2
log-bin=mysql-bin
auto-increment-increment=2
auto-increment-offset=2

Step 2: Create replication users

Create replication users on both servers, allowing mutual access.

Step 3: Configure replication both ways

Each server must be configured as a slave pointing to the other server using CHANGE MASTER TO.

Step 4: Start replication

START SLAVE;
SHOW SLAVE STATUS\G

Monitoring and maintenance

  • Regularly check SHOW SLAVE STATUS
  • Monitor replication lag (Seconds_Behind_Master)
  • Back up masters, not slaves
  • Keep binary logs under control

Common issues

  • Replication stopped due to duplicate keys
  • Network interruptions
  • Incompatible MySQL versions
  • Insufficient disk space for binary logs

Need assistance?

If you require help with replication setup, troubleshooting, or advanced topologies, please open a support ticket in the Client Area.


Was this answer helpful?

« Back