Knowledgebase

Find frequently asked questions and setup guides by searching or by browsing the categories.

What This Guide Covers

This is an overview with the key commands for one source and one replica on MySQL 8.4, the version Ubuntu 26.04 ships. On MySQL 8.0 (Ubuntu 24.04), two statements have other names, given where they appear. It is not a full production runbook: test the whole procedure on two spare servers first.

  • Source: the server that takes the writes and records them in its binary log.
  • Replica: a server that reads that log and applies the same changes.

Older documentation calls them master and slave. For MySQL itself, see what is MySQL.

Before You Start

  • Same version: install the same MySQL version on both, as in how to install MySQL on Ubuntu. Debian's default MySQL-compatible server is MariaDB, whose replication commands differ from the ones below.
  • Private network: replication traffic goes between the servers over a private network or TLS, never in the clear across the internet. The examples use 10.0.0.1 for the source and 10.0.0.2 for the replica.
  • Backup: take a backup of the source before you change its configuration.

Step 1: Configure the Source

On Ubuntu, set these lines under [mysqld] in /etc/mysql/mysql.conf.d/mysqld.cnf, changing the existing bind-address line rather than adding a second one:

server_id = 1
bind-address = 127.0.0.1,10.0.0.1
gtid_mode = ON
enforce_gtid_consistency = ON

Several addresses in bind-address are separated by commas. The binary log is on by default in MySQL 8.0 and later, in row format. GTIDs (global transaction identifiers) let the replica find by itself where to continue. Restart MySQL, then allow port 3306 from the replica only: sudo ufw allow from 10.0.0.2 to any port 3306 proto tcp.

Step 2: Create the Replication Account

In sudo mysql on the source, replacing replica_private_ip with the replica's address, 10.0.0.2 in this example:

CREATE USER 'repl'@'replica_private_ip' IDENTIFIED BY 'STRONG_PASSWORD' REQUIRE SSL;
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'replica_private_ip';

The privilege still carries its old name in 8.4. The account can connect from that one address only, over TLS, and can do nothing but read the binary log. MySQL creates its own self-signed TLS certificates at startup when none are configured, so the connection is encrypted without further setup, although the replica does not verify the source's identity that way. How to list users in MySQL shows how to check the account.

Step 3: Take a Consistent Copy

The replica must start from exactly the state the source had at one moment. For a small or medium dataset, a dump in a single transaction does that for InnoDB tables:

sudo mysqldump --all-databases --single-transaction --triggers --routines --events > source.sql

With GTIDs on, the dump records which transactions it contains. Copy it to the replica with scp, and delete it afterwards: it holds all your data. For large datasets, the clone plugin or a physical backup tool is faster.

Step 4: Configure the Replica

On the replica, set server_id = 2 and the same two GTID lines, restart, and load the dump with sudo mysql < source.sql. If the load complains about GTIDs already in use, the replica has its own history. The reset that clears it deletes all binary logs and the GTID history of the server it runs on, so run it only on the new replica, never on the source: RESET BINARY LOGS AND GTIDS; on 8.4 (RESET MASTER; on 8.0), then load again. Then connect it to the source:

CHANGE REPLICATION SOURCE TO
  SOURCE_HOST = '10.0.0.1',
  SOURCE_USER = 'repl',
  SOURCE_PASSWORD = 'STRONG_PASSWORD',
  SOURCE_AUTO_POSITION = 1,
  SOURCE_SSL = 1;
START REPLICA;

Finally add read_only = ON and super_read_only = ON to the replica's configuration, so nothing but replication writes to it.

Step 5: Check SHOW REPLICA STATUS

SHOW REPLICA STATUS\G
  • Replica_IO_Running and Replica_SQL_Running: both must say Yes.
  • Seconds_Behind_Source: how far the replica lags; 0 or close to it on a quiet server.
  • Last_IO_Error and Last_SQL_Error: the reason when something stops.

Connection errors usually come from the firewall, bind-address, or the account's host. On the source, SHOW BINARY LOG STATUS (8.4) or SHOW MASTER STATUS (8.0) shows its current position.

Replication Is Not a Backup

A replica copies mistakes as faithfully as data: a dropped table or a bad DELETE on the source reaches the replica within seconds. Keep regular dumps or physical backups, stored on another machine, alongside any replica; see snapshot vs backup. A replica is useful for read scaling, for taking backups without loading the source, and as a standby you can promote by hand.

Was this answer helpful?
Loading...