MySQL Master-Slave Replication: 6 Easy Steps

February 29, 2024
15 min read

Imagine a scenario where a server crash wipes out your meticulously crafted database or a sudden surge in traffic brings your entire system to its knees. Sounds terrifying, right? But what if you have a perfect copy of that database and store it in a safe place? That’s essentially what MySQL master-slave replication does for your databases. It creates a mirror image of your main database (the master) on the other servers (the slaves). This way, if something happens to the master, you still have the slaves with all the information. But setting up might seem daunting for you! However, this guide will break it down into 6 simple steps to get you started.   

What is Master Slave Replication? 

Master-slave replication, also known as active/passive replication, is a technique used in databases to keep multiple copies of the same data synchronized across different servers. This enhances system resilience and facilitates efficient data distribution across multiple nodes in a distributed database environment. The master server acts as the central hub, diligently recording every change made to your data; meanwhile, the slave server silently observes and replicates every update in real-time. This ensures that even if the master stumbles, your data remains safe and sound. 

Master-slave replication offers several benefits, including:

  • Data Redundancy and Availability: By replicating data from a master database to one or more slave databases, master-slave replication ensures data redundancy and availability. When the master server fails, the slave databases can minimize downtime and provide uninterrupted data access.
  • Improve Read Performance: Read requests can be directed to the slave server, reducing processing load from the master and allowing it to focus on write operations. It can significantly improve read performance, especially for high-read traffic applications. 
  • Disaster Recovery: In case of a major disaster that affects the master servers, a healthy slave server can be promoted to become the new master. This helps you to recover your data in these unforeseen circumstances.  
  • Scalability: Additional slave servers can be added easily to scale the read capability of the databases as needed. This is useful for applications experiencing growth or fluctuating traffic patterns. 

How Master Slave Replication Works? 

Understanding the mechanics of master-slave replication is essential for building scalable database systems. Explore the intricacies of this process, from the initialization phase and log shipping to the application of changes on slave servers. This section provides a detailed insight into the core principles that govern how master-slave replication works, ensuring an efficient data flow within database environments. 

Initialization

During initialization, a consistent snapshot of the master database is created. This represents the state of the database at a specific time and is used as a starting point for replicating the slave servers. 

Log Shipping

As changes (inserts, deletes, updates) occur on the master database, these modifications are recorded in the form of binary logs or transaction logs. These logs are then sent to the slave servers to keep them synchronized with the master. It’s a way of keeping multiple databases in sync. 

Replication Process

The binary logs containing the changes are shipped from the master server to the slave servers so that they can exactly mimic what the main server is doing. This can be done through various methods, such as using MySQL’s binary log replication or PostgreSQL’s WAL (Write-Ahead Logging) shipping. 

Apply Changes

Each slave server independently applies the received changes to its own database copy. This is typically done in the same order in which they were committed on the master, ensuring data consistency. 

Asynchronous or Synchronous Replication

Asynchronous replication allows the master to continue its operation without waiting for acknowledgment from the slave. While this enhances performance, it may slightly delay data consistency. Whereas, synchronous replication ensures that the master waits for acknowledgment from at least one slave before considering a transaction as committed. This provides a higher level of data consistency but may impact performance due to increased latency. 

Read Scaling

One of the advantages of master-slave replication is improved read scaling. Read operations (queries) can be distributed among the slave servers, reducing the load on the master and improving overall system performance. This is achieved by directing read queries to the slave servers while write operations are executed on the master.

How to Setup Master Slave Replication in MySQL 

Master-Slave replication in MySQL allows you to create a resilient and scalable database environment. Follow these step-by-step instructions to configure a MySQL master server and one or more slave servers.  

Prerequisites

  • Two MySQL servers,
  • Network connectivity between the servers,
  • Command-line interface,
  • Root or administrative access. 

Step 1: Configure the Master Database

  • Edit the MySQL configuration file, located with a .cnf extension. Within this file, find the line containing bind-address = 127.0.0.1 and replace the 127.0.0.1 with the IP address of your master replication server.
  • Add the following lines under the [mysqld] section:
 
#unique ID for the master server
server_id = 1 		            		
#enable binary logging	
log_bin  = mysql-bin	                            	
# Specify the database name for replication	
binlog-do-db = [database_name]     
 
  • Restart the MySQL service to apply the changes. 

Step 2: Create Replication User

  • Log into your MySQL master server.

	mysql -u root -p
  • Create the replication user
 
CREATE USER ‘replication’@’%’ IDENTIFIED BY ‘password’;
GRANT REPLICATION SLAVE ON *.* TO ‘replication’@’%’;
FLUSH PRIVILEGES;
 

Replace ‘%’ with the slave server’s IP address if you want to restrict access. 

Step 3: Retrieve Log File Position

  • Execute the following command on the master server:

	SHOW MASTER STATUS;

  • Document the values of File and Position as you will need those for configuring the slave. 

Step 4: Copy Data or Create New Database 

  • If you want to replicate existing, lock tables to prevent writes on the master using FLUSH TABLES WITH READ LOCK command. 
  • Create a backup of your data using mysqldump command. Then, unlock the tables using UNLOCK TABLES. Lastly, transfer the backup to the slave server. 
  • If you don’t need existing data, create a new database on the slave server. 

Step 5: Configure the Slave Server

  • Modify the slave’s MySQL configuration file (same location as the master). Add the following lines under the [mysqld] section: 
 
#unique ID for the slave (different from the master)	
server-id = 2 	
#enable relay logging		
Relay-log = mysql-relay-bin 	
 
  • Restart the MySQL service on the salve. 

Step 6: Start Replication

  • On the slave server, execute the following command, replacing the values with the ones you obtained from the master:
 
CHANGE MASTER TO
	MASTER_HOST = ‘master_server_ip’ ,
	MASTER_USER = ‘replication’ ,
	MASTER_PASSWORD = ‘password’ ,
	MASTER_LOF_FILE = ‘mysql-bin.000001’ ,
	MASTER_LOG_POS = 900 ;
START SLAVE ;
 

By following these 6 easy steps, you’ve successfully set up MySQL master-slave replication. This configuration provides data redundancy and the ability to efficiently distribute read and write loads. 

However, manually setting up master-slave replication in MySQL involves configuring both the master and slave servers, creating and configuring a user, and managing the replication process. While functional, it can be error-prone, time-consuming, and challenging to scale or operate securely. 

This is where Airbyte comes into the picture. It provides an efficient solution for automating and simplifying the setup and management of replication processes. With Airbyte, you can easily configure migration settings, monitor replication jobs, and ensure data consistency across distributed environments. 

Leverage Airbyte's No-code Data Pipeline for Effortless MySQL Replication

Airbyte is a data engineering platform that bridges the gap between different data sources and destinations. It helps you to move data from various places, like databases, APIs, and files, to other locations, like data warehouses, lakes, etc. This flexibility empowers you to consolidate and manage your data in one spot for analysis and decision-making. 

Here are some key features of Airbyte: 

  • Simplified Configuration: Airbyte provides a visual interface for configuring replication settings. Instead of manually editing files and writing commands, you can simply select your source and destination and configure options through intuitive UI.
  • Connectors Library: With Airbyte’s rich set of 350+ pre-built connectors, you can migrate your data seamlessly from various sources to your desired destinations.  Whether it's databases, SaaS applications, or file storage services, Airbyte offers a wide range of connectors to meet diverse data integration needs.
  • Custom Connectors: In scenarios where the existing pre-built connectors may not cover specific use cases or sources, Airbyte enables the creation of custom connectors. You can leverage Airbyte's Connector Development Kit (CDK) to design and implement custom connectors tailored to your unique requirements.
  • Change Data Capture (CDC): Airbyte’s CDC functionality efficiently tracks and captures incremental changes made to data sources. By monitoring inserts, updates, and deletes, Airbyte CDC ensures that only appended or relevant changes are replicated to target destinations, minimizing the data transfer process.
  • Error Handling and Monitoring: It includes robust error handling mechanisms and monitoring capabilities to ensure the reliability of the data replication process.

Wrapping Up! 

By carefully configuring the master and slave servers, your organization can establish a resilient data replication strategy. However, following the steps listed above will help you set the foundation for a well-orchestrated MySQL master-slave replication, ensuring data redundancy, efficient distribution of read and write loads, and facilitating seamless data synchronization across your database environments.

Limitless data movement with free Alpha and Beta connectors
Introducing: our Free Connector Program
The data movement infrastructure for the modern data teams.
Try a 14-day free trial