MySQL primary-replica replication asynchronously copies data from a primary server to one or more replica servers. This improves read scalability, provides a hot standby for disaster recovery, and enables backups from the replica without impacting the primary. This guide sets up replication on Ubuntu 26.04 LTS.
Tested and valid on:
- Ubuntu 26.04 LTS
Prerequisites
- Two Ubuntu 26.04 LTS servers with MySQL 9 installed
- Network connectivity between the servers
- Unique server IDs for each node
Step 1 – Configure the Primary Server
On the primary, edit /etc/mysql/mysql.conf.d/mysqld.cnf:
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
Add:
[mysqld]
server-id = 1
log_bin = /var/log/mysql/mysql-bin.log
binlog_expire_logs_seconds = 604800
max_binlog_size = 100M
sudo systemctl restart mysql
Step 2 – Create a Replication User on Primary
sudo mysql
CREATE USER 'replicator'@'replica_ip' IDENTIFIED WITH mysql_native_password BY 'ReplicaPass2026!';
GRANT REPLICATION SLAVE ON *.* TO 'replicator'@'replica_ip';
FLUSH PRIVILEGES;
SHOW MASTER STATUS;
exit
Note the File and Position values.
Step 3 – Configure the Replica Server
On the replica, edit mysqld.cnf:
server-id = 2
relay-log = /var/log/mysql/mysql-relay-bin.log
read_only = 1
sudo systemctl restart mysql
Step 4 – Connect the Replica to the Primary
On the replica:
sudo mysql
CHANGE REPLICATION SOURCE TO
SOURCE_HOST='primary_ip',
SOURCE_USER='replicator',
SOURCE_PASSWORD='ReplicaPass2026!',
SOURCE_LOG_FILE='mysql-bin.000001',
SOURCE_LOG_POS=123;
START REPLICA;
SHOW REPLICA STATUSG
exit
Step 5 – Verify Replication
On the primary, create a test table:
sudo mysql
CREATE DATABASE reptest;
USE reptest;
CREATE TABLE test (id INT AUTO_INCREMENT PRIMARY KEY, val VARCHAR(50));
INSERT INTO test VALUES (NULL, 'hello');
exit
On the replica, confirm it appeared:
sudo mysql
USE reptest;
SELECT * FROM test;
exit
Step 6 – Open Firewall on Primary
sudo ufw allow from replica_ip to any port 3306
Conclusion
MySQL primary-replica replication is now running on Ubuntu 26.04 LTS. All writes on the primary automatically propagate to the replica. Direct read queries to the replica to offload the primary and improve overall throughput.