MySQL/MariaDB replication allows data written to a Primary (source) server to be automatically copied to one or more Replica (formerly “Slave”) servers. This is commonly used for read scaling, high availability, and backup offloading. This is Part 1 — configuring the Primary server.
Environment
Primary server IP: 192.168.10.10
Replica server IP: 192.168.10.11
Database to replicate: myappdb
MySQL version: same on both servers (required)
Step 1 — Configure the Primary Server
Edit /etc/my.cnf or /etc/mysql/mysql.conf.d/mysqld.cnf and add under [mysqld]:
[mysqld]
server-id = 1
log_bin = /var/log/mysql/mysql-bin.log
binlog_format = ROW
binlog_do_db = myappdb # Remove if you want to replicate all databases
expire_logs_days = 7
max_binlog_size = 100M
systemctl restart mysqld
Step 2 — Create a Replication User on the Primary
mysql -u root -p
-- Create dedicated replication user
CREATE USER 'replicator'@'192.168.10.11' IDENTIFIED BY 'StrongPassword123!';
GRANT REPLICATION SLAVE ON *.* TO 'replicator'@'192.168.10.11';
FLUSH PRIVILEGES;
Step 3 — Take a Consistent Database Dump
# Lock tables and get the binary log position (do NOT exit this session)
mysql -u root -p -e "FLUSH TABLES WITH READ LOCK;"
# In a SECOND terminal, get the current log position
mysql -u root -p -e "SHOW MASTER STATUS;"
# Note the File and Position values — you'll need them for the Replica setup
# Example: File=mysql-bin.000003, Position=154
# Dump the database
mysqldump -u root -p --databases myappdb --master-data > /tmp/myappdb_dump.sql
# Release the lock
mysql -u root -p -e "UNLOCK TABLES;"
Step 4 — Transfer the Dump to the Replica
scp /tmp/myappdb_dump.sql root@192.168.10.11:/tmp/
Continue with Part 2 to configure the Replica server and start replication.
