Website Vidya
Linux & ServersStep-by-Step Guide

How to Configure MySQL Master-Slave Replication on Ubuntu 24.04

Learn how to configure mysql master-slave replication on ubuntu 24.04 with binary logging, dedicated replication users, and replica synchronization.

RR
Rajesh RayLead Systems Educator
Published:
5 min read
PDF Cheat Sheet
How to Configure MySQL Master-Slave Replication on Ubuntu 24.04
AI Executive SummaryWebsiteVidya AI

5 min read

Learn how to configure mysql master-slave replication on ubuntu 24.04 with binary logging, dedicated replication users, and replica synchronization.

Key Highlights
  • Prerequisites and Supported Releases
  • Step 1: Configuring the Source (Master) Server
  • Step 2: Creating the Replication User Account on the Source
Grounded in full engineering runbookWebsiteVidya AI Engine

In high-traffic enterprise architectures, executing all database read and write queries against a single standalone MySQL instance leads to CPU exhaustion, I/O bottlenecks, and catastrophic single-point-of-failure vulnerabilities.

Database replication resolves these scalability challenges by copying data asynchronously from a primary database (Source / Master) to one or more secondary databases (Replicas / Slaves).

With replication established, all write operations (INSERT, UPDATE, DELETE) target the primary node, while read queries (SELECT) are distributed across replicas, multiplying read throughput and enabling automated disaster recovery failover.

MySQL on Ubuntu 24.04 LTS uses the modern Source-Replica terminology and provides robust binary log tracking for rock-solid transaction synchronization.

Advertisement

In this guide, we walk through how to configure mysql master-slave replication on ubuntu 24.04 step-by-step across two Ubuntu server nodes.

Prerequisites and Supported Releases

This guide is thoroughly verified on Ubuntu 24.04 LTS (Noble Numbat) & Ubuntu 22.04 LTS. The instructions apply cleanly to cloud instances (AWS EC2, Google Compute Engine, DigitalOcean, Hetzner), bare-metal servers, and local virtual machines.

Before proceeding, ensure you have the following requirements satisfied:

Advertisement
  • Two separate servers running Ubuntu 24.04 LTS (Node 1: Source / 192.168.1.10, Node 2: Replica / 192.168.1.20).
  • MySQL 8.0 installed on both servers.
  • A non-root user with sudo administrative privileges on both machines.
  • Private network connectivity between the two database nodes.

Step 1: Configuring the Source (Master) Server

The primary database server must be configured with a unique numeric server-id, binary logging enabled, and bound to its private network IP address so the replica can connect.

Open the MySQL server configuration file at /etc/mysql/mysql.conf.d/mysqld.cnf on the Source Server:

Configure the networking and binary logging directives:

# On SOURCE SERVER (192.168.1.10):
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf

# 1. Bind to private IP address (or 0.0.0.0 with firewall):
bind-address = 192.168.1.10

# 2. Unique server ID (must be unique across cluster):
server-id = 1

# 3. Enable binary logging and specify database to replicate:
log_bin = /var/log/mysql/mysql-bin.log
binlog_do_db = production_db

# Save and exit (Ctrl+O, Enter, Ctrl+X in nano).

# Restart MySQL daemon:
sudo systemctl restart mysql

Step 2: Creating the Replication User Account on the Source

The replica node requires a dedicated MySQL user account granted the REPLICATION SLAVE privilege to read the primary node's binary logs.

Advertisement

Log into the MySQL command console on the Source Server:

Create the user and restrict its connection origin to the replica server's IP address (192.168.1.20):

-- On SOURCE SERVER:
sudo mysql -u root -p

-- Create dedicated replication user:
CREATE USER 'replica_user'@'192.168.1.20' IDENTIFIED WITH caching_sha2_password BY 'SecureReplicationPass123!';

-- Grant replication privileges:
GRANT REPLICATION SLAVE ON *.* TO 'replica_user'@'192.168.1.20';
FLUSH PRIVILEGES;

-- Inspect current binary log coordinates:
SHOW MASTER STATUS;
-- +------------------+----------+--------------+------------------+
-- | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
-- +------------------+----------+--------------+------------------+
-- | mysql-bin.000001 |      845 | production_db|                  |
-- +------------------+----------+--------------+------------------+
-- NOTE DOWN the File name and Position number!

Step 3: Exporting and Transferring Initial Database Snapshot

Before replication can begin, the replica database must contain an exact baseline snapshot of the source data matching the recorded binary log position.

Use mysqldump with the --master-data=2 or --source-data=2 flag to create a consistent export:

Transfer the dump file to the replica server using scp:

# On SOURCE SERVER: Create consistent database dump:
sudo mysqldump -u root -p --databases production_db --single-transaction > production_db.sql

# Transfer dump file to replica server:
scp production_db.sql [email protected]:~/

Step 4: Configuring the Replica (Slave) Server

On the Replica Server (192.168.1.20), configure a distinct server-id (e.g. 2) and relay log settings.

Open /etc/mysql/mysql.conf.d/mysqld.cnf on the replica:

Add the replica parameters and restart MySQL:

# On REPLICA SERVER (192.168.1.20):
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf

# Unique server ID (MUST be different from source):
server-id = 2
relay_log = /var/log/mysql/mysql-relay-bin.log
log_bin = /var/log/mysql/mysql-bin.log
binlog_do_db = production_db
read_only = 1

# Save and exit.
sudo systemctl restart mysql

# Import the database snapshot transferred from source:
sudo mysql -u root -p < ~/production_db.sql

Step 5: Initializing and Starting Replication on the Replica

Connect to MySQL on the Replica Server and instruct the engine to connect to the primary source node.

Use the CHANGE REPLICATION SOURCE TO statement (or legacy CHANGE MASTER TO) with the coordinates recorded in Step 2:

Start the replication threads and inspect active status:

-- On REPLICA SERVER:
sudo mysql -u root -p

-- Configure source connection coordinates:
CHANGE REPLICATION SOURCE TO
  SOURCE_HOST='192.168.1.10',
  SOURCE_USER='replica_user',
  SOURCE_PASSWORD='SecureReplicationPass123!',
  SOURCE_LOG_FILE='mysql-bin.000001',
  SOURCE_LOG_POS=845;

-- Start replication worker threads:
START REPLICA;

-- Verify operational health:
SHOW REPLICA STATUS\G

Verifying Critical Replication Status Fields

Inspect the output of SHOW REPLICA STATUS\G to verify that both replication threads are running without errors:

Review this checklist of vital replication health indicators:

Step 6: Testing End-to-End Replication Synchronization

Verify real-time data replication by creating a test table and inserting records on the primary source server:

Confirm that the record replicates instantaneously to the replica node:

-- On SOURCE SERVER (192.168.1.10):
USE production_db;
CREATE TABLE replication_test (id INT AUTO_INCREMENT PRIMARY KEY, message VARCHAR(255));
INSERT INTO replication_test (message) VALUES ('Replication works on Ubuntu 24.04!');

-- On REPLICA SERVER (192.168.1.20):
USE production_db;
SELECT * FROM replication_test;
-- Output confirms instant synchronization:
-- +----+---------------------------------------+
-- | id | message                               |
-- +----+---------------------------------------+
-- |  1 | Replication works on Ubuntu 24.04!    |
-- +----+---------------------------------------+

Troubleshooting and Common Errors

When configuring Ubuntu servers, unexpected permission errors, network timeouts, or lock contention can occur. Review these common issues and verified resolutions:

Replica_IO_Running: No (Error connecting to source)

Cause: Network connectivity failure, blocked port 3306 in UFW, or wrong password for replica_user.

Resolution: Check UFW firewall rules on the source server: sudo ufw allow from 192.168.1.20 to any port 3306 proto tcp.

sudo ufw status | grep 3306

Replica_SQL_Running: No (Error executing duplicate key or missing table)

Cause: Manual writes occurred on the replica causing duplicate key collisions.

Resolution: Enforce read_only = 1 in /etc/mysql/mysql.conf.d/mysqld.cnf on the replica to prevent direct writes.

sudo mysql -e 'SET GLOBAL read_only = ON;'

Fatal error: The replica I/O thread stops because master and replica have equal server_id

Cause: Both database servers share the same server-id number in mysqld.cnf.

Resolution: Assign unique numeric server-ids (e.g. source = 1, replica = 2) and restart MySQL.

grep server-id /etc/mysql/mysql.conf.d/mysqld.cnf

Security Best Practices and Hardening

Restrict MySQL port 3306 strictly to the replica server's IP address using UFW; never expose database ports publicly.

Always set read_only = 1 on replicas to prevent application developers or scripts from executing accidental writes on replicas.

Consider using SSL encryption for replication traffic over untrusted networks by passing SOURCE_SSL=1 in the replication configuration.

Deepen your Linux system administration skills with our related production-tested guides:

Authoritative References and Official Documentation

Frequently Asked Questions

Quick answers to common questions and configurations

4 FAQs
In default asynchronous replication, the source writes to its binary log and commits transactions without waiting for replicas to confirm receipt. In semi-synchronous replication, the source waits until at least one replica has received and logged the transaction before committing.
Yes. You can specify multiple binlog_do_db = db_name lines in mysqld.cnf, or omit the directive entirely to replicate all databases across the cluster.
Setting read_only = 1 blocks all write queries from regular user accounts on the replica, preventing split-brain data discrepancies while still permitting the replication thread (which runs with superuser privileges) to apply incoming updates.
Global Transaction Identifiers (GTID) assign a unique identifier to every committed transaction across the cluster. GTID-based replication eliminates the need to track manual log file names and byte offsets, simplifying automated failover.
RR
Written byVerified Author

Rajesh Ray

I’m Rajesh Rai, a full-stack developer, Linux administrator, and technology enthusiast passionate about making complex technology easier to understand and use.

SPAM-FREE MODERATED DISCOURSE

Join the Conversation & Perspective

Have thoughts, counterpoints, or reflections on this story? Engage directly with fellow readers and editorial contributors.

AT
MP
JV
+2

No comments on this guide yet. Be the first to share your thoughts or ask a question!

Recommended Reading10 Guides

Related Technical Guides

Tested runbooks, server administration walkthroughs, and troubleshooting guides

Featured Infrastructure PartnerUp to 75% OFF + 3 Months Free

Supercharge Your Linux & Cloud Workloads with Hostinger

Deploy high-speed NVMe KVM VPS, Docker containers, and LiteSpeed-accelerated WordPress servers in under 60 seconds with full root access, dedicated IPv4, and 24/7 technical support.

Pure NVMe SSD StorageFull Root Access & SSHFree Domain & Unlimited SSL99.9% Uptime SLA
Plans from$2.99/mo
Claim Hostinger DealAffiliate link • Special discount applied at checkout