/  Database   /  How to Set Up a MySQL Replica for High Availability

How to Set Up a MySQL Replica for High Availability

In today’s tech age, downtime is your worst enemy. Consider this: a user clicks a button on your app and nothing, because your database is down! Ow. This is where MySQL Replication comes to the rescue. If you’re a database learning student or a DevOps, cloud, and backend learner, replication mastery is a career winner.

Here, we’ll divide the step-by-step tutorial on how to establish a MySQL replica (also referred to as a “slave”) to keep your database up and running, speedy, and stable.

What is MySQL Replication?

At its most basic, MySQL replication is merely copying data from one MySQL server (known as the Master) to another (known as the Replica or Slave). This setup is significant for:

High Availability: The replica can act as a substitute for the failed master.

Load Balancing: Spread read requests among replicas in order to prevent overwhelming the master.

Disaster Recovery: Always have a backup handy in order to minimize data loss.

Step 1: Prepare Your Environment

You will require two MySQL servers – you will have the master and you will have the replica. These can be virtual machines, cloud instances (AWS, Azure, GCP), or even Docker containers if you work locally.

For this case:

Master IP: 192.168.1.100

Copy IP: 192.168.1.101

Step 2: Define the Master Database

Update the master MySQL configuration file:

sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf

Add or replace the following lines:

server-id = 1

log_bin = /var/log/mysql/mysql-bin.log

Restart MySQL on the master:

sudo systemctl restart mysql

Create a replication user on the master:

CREATE USER ‘replica_user’@’%’ IDENTIFIED BY ‘strong_password’; GRANT REPLICATION SLAVE ON. TO ‘replica_user’@’%’; FLUSH PRIVILEGES; Lock the master tables to give an immutable snapshot:

FLUSH TABLES WITH READ LOCK;

SHOW MASTER STATUS;

Save the File and Position values. You will be needing them later.

Step 3: Configure the Replica Database

Adjust the replica MySQL configuration file:

sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf

Insert or Replace

server-id = 2

Restart MySQL on the replica:

sudo systemctl restart mysql

Import master data (optional but highly recommended):

mysqldump -u root -p –all-databases –master-data > dump.sql

mysql -u root -p < dump.sql

Step 4: Synchronize the Replica to the Master

On the replica, run:

CHANGE MASTER TO MASTER_HOST=’192.168.1.100′, MASTER_USER=’replica_user’, MASTER_PASSWORD=’strong_password’, MASTER_LOG_FILE=’mysql-bin.000001′, — use your master’s log file MASTER_LOG_POS= 154; — use your master’s position

Then begin the replication:

START SLAV

Step 5: Check Replication Status

Run on the replica

SHOW SLAVE STATUS
G

Look for:

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

Both must be Yes! Otherwise, debug by verifying network connectivity, user permissions, or log file data.

Step 6: Release Master Lock

Once replication has commenced, go back to the master and drop the tables:

UNLOCK TABLES;

Why is this important?

In interviews: You will probably be asked how you would ensure database availability or minimize downtime in production.

In real-world projects: Replication is used in high volume applications, banking applications, e-commerce websites, and SaaS applications.

In certifications: Cloud and DevOps exams (AWS, GCP, etc.) typically include high-availability topics like replication.

Bonus Tips

Practice setting up multi-replica environments.

Learn about semi-synchronous replication for more sophisticated setups.

Check out MySQL Group Replication and Galera Cluster if you wish to level up!

Leave a comment