Replication in MySQL is a powerful feature that allows you to create copies of your database across multiple servers. This setup is essential for improving database availability, load balancing, and disaster recovery. Whether you're managing a high-traffic website or preparing for potential hardware failures, MySQL replication can help ensure your data is always accessible and secure.
In this guide, we’ll walk you through the process of setting up MySQL replication, covering everything from prerequisites to configuration and troubleshooting. Let’s dive in!
MySQL replication is a process where data from one MySQL server (the primary server, also known as the master) is copied to one or more MySQL servers (known as replicas or slaves). The primary server writes all changes to a binary log, which the replicas read and apply to their own databases.
Before setting up replication, ensure the following:
server-id in the MySQL configuration file.Edit the MySQL Configuration File
Open the MySQL configuration file (my.cnf or my.ini, depending on your system) on the primary server and add the following lines:
[mysqld]
server-id=1
log_bin=mysql-bin
binlog_do_db=your_database_name
server-id: A unique identifier for the server (e.g., 1 for the primary server).log_bin: Enables binary logging.binlog_do_db: Specifies the database(s) to replicate. Replace your_database_name with the name of the database you want to replicate.Restart MySQL
Restart the MySQL service to apply the changes:
sudo systemctl restart mysql
Create a Replication User
Log in to the MySQL shell and create a user for replication:
CREATE USER 'replica_user'@'%' IDENTIFIED BY 'password';
GRANT REPLICATION SLAVE ON *.* TO 'replica_user'@'%';
FLUSH PRIVILEGES;
Replace replica_user and password with your desired username and password.
Get the Binary Log Coordinates
Lock the database and note the current binary log file and position:
FLUSH TABLES WITH READ LOCK;
SHOW MASTER STATUS;
This will output something like:
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 | 154 | your_db_name | |
+------------------+----------+--------------+------------------+
Take note of the File and Position values, as you’ll need them later.
Important: Keep the session open to maintain the lock until the replica is configured.
Backup the Database
Create a backup of the primary database and transfer it to the replica server:
mysqldump -u root -p --all-databases > primary_backup.sql
scp primary_backup.sql replica_user@replica_server:/path/to/backup/
Edit the MySQL Configuration File
On the replica server, open the MySQL configuration file and add the following lines:
[mysqld]
server-id=2
relay_log=relay-log
server-id: A unique identifier for the replica server (e.g., 2).relay_log: Specifies the relay log file name.Restart MySQL
Restart the MySQL service on the replica server:
sudo systemctl restart mysql
Restore the Database Backup
Import the backup from the primary server:
mysql -u root -p < /path/to/backup/primary_backup.sql
Set Up Replication
Log in to the MySQL shell on the replica server and configure replication:
CHANGE MASTER TO
MASTER_HOST='primary_server_ip',
MASTER_USER='replica_user',
MASTER_PASSWORD='password',
MASTER_LOG_FILE='mysql-bin.000001',
MASTER_LOG_POS=154;
Replace the values with:
primary_server_ip: The IP address of the primary server.replica_user and password: The replication user credentials.MASTER_LOG_FILE and MASTER_LOG_POS: The binary log file and position from the primary server.Start the Replica
Start the replication process:
START SLAVE;
Verify Replication
Check the replication status:
SHOW SLAVE STATUS\G
Look for the following lines:
Slave_IO_Running: YesSlave_SQL_Running: YesIf both are Yes, replication is successfully set up.
Replication Not Starting
MASTER_LOG_FILE and MASTER_LOG_POS values.Duplicate Entry Errors
--skip-slave-start option to prevent the replica from starting automatically during troubleshooting.Network Connectivity Issues
3306) is open.Setting up MySQL replication is a straightforward process that can significantly enhance your database’s performance, availability, and reliability. By following this step-by-step guide, you can configure a robust replication setup tailored to your needs. Whether you’re scaling your application or preparing for disaster recovery, MySQL replication is a must-have tool in your database management arsenal.
Have you set up MySQL replication before? Share your experiences or ask questions in the comments below!