MySQL replication keeps a replica server in sync with a primary, providing redundancy and read scaling. This guide configures MySQL 8.0 primary-replica (master-slave) replication between two Ubuntu 24.04 LTS servers.
Tested and valid on:
- Ubuntu 24.04 LTS
Prerequisites
- Two Ubuntu 24.04 LTS servers (primary and replica) with MySQL 8.0 installed
- Network connectivity between the two servers
- A user with sudo privileges on both
Step 1 – Configure the Primary Server
On the primary, edit MySQL config:
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
Set a unique server ID and enable binary logging:
[mysqld]
server-id = 1
log_bin = /var/log/mysql/mysql-bin.log
binlog_do_db = mydb
Restart MySQL:
sudo systemctl restart mysql
Step 2 – Create a Replication User on the Primary
Log in and create the user:
sudo mysql
CREATE USER 'replica_user'@'replica_ip' IDENTIFIED WITH mysql_native_password BY 'ReplicaPass123!';
GRANT REPLICATION SLAVE ON *.* TO 'replica_user'@'replica_ip';
FLUSH PRIVILEGES;
Step 3 – Get the Primary Binary Log Coordinates
Lock tables and record the log file and position:
FLUSH TABLES WITH READ LOCK;
SHOW MASTER STATUS;
Note the File and Position values. Keep this session open.
Step 4 – Export and Transfer the Database
In a new terminal on the primary, dump the database:
sudo mysqldump -u root -p --databases mydb > /tmp/mydb.sql
Copy to the replica:
scp /tmp/mydb.sql user@replica_ip:/tmp/
Unlock tables on the primary:
UNLOCK TABLES;
Step 5 – Configure the Replica Server
On the replica, edit MySQL config:
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
Set a different server ID and disable binary logging:
[mysqld]
server-id = 2
relay-log = /var/log/mysql/mysql-relay-bin.log
Restart MySQL and import the dump:
sudo systemctl restart mysql
sudo mysql < /tmp/mydb.sql
Step 6 – Connect the Replica to the Primary
On the replica, configure replication with the coordinates from Step 3:
sudo mysql
CHANGE REPLICATION SOURCE TO
SOURCE_HOST='primary_ip',
SOURCE_USER='replica_user',
SOURCE_PASSWORD='ReplicaPass123!',
SOURCE_LOG_FILE='mysql-bin.000001',
SOURCE_LOG_POS=154;
START REPLICA;
Step 7 – Verify Replication
Check replica status:
SHOW REPLICA STATUSG
Look for Replica_IO_Running: Yes and Replica_SQL_Running: Yes.
Conclusion
MySQL 8.0 primary-replica replication is now active on Ubuntu 24.04 LTS. Changes on the primary are automatically propagated to the replica. Use the replica for read-heavy queries to scale performance.