SyntaxStudy
Sign Up
MySQL Setting Up Primary-Replica Replication
MySQL Advanced 5 min read

Setting Up Primary-Replica Replication

Replication Setup

Configure the primary with a unique server_id and binlog, create a replication user, then configure the replica to connect.

Example
-- On PRIMARY (my.cnf):
-- server_id = 1
-- log_bin = /var/log/mysql/bin.log
-- binlog_format = ROW
-- Create replication user
CREATE USER "replicator"@"replica_ip" IDENTIFIED BY "strong_password";
GRANT REPLICATION SLAVE ON *.* TO "replicator"@"replica_ip";
FLUSH PRIVILEGES;
SHOW MASTER STATUS;  -- note File and Position

-- On REPLICA:
-- server_id = 2
CHANGE REPLICATION SOURCE TO SOURCE_HOST="primary_ip",
  SOURCE_USER="replicator", SOURCE_PASSWORD="strong_password",
  SOURCE_LOG_FILE="bin.000001", SOURCE_LOG_POS=154;
START REPLICA;
SHOW REPLICA STATUS\G
Pro Tip

Use GTID-based replication (gtid_mode=ON) in production — it is more robust and easier to manage than position-based.