MySQL primary-replica replication with GTID for WordPress
When one database server cannot keep up with the reads of a busy WordPress site, a replica takes part of the load: it receives every change from the primary and can answer SELECT queries. With GTID (global transaction identifiers) the replica tracks its position by transaction, not by binary log file and offset, which makes setup and failover simpler. WordPress itself knows only one database, so the read/write split needs a database drop-in.
Problem: The database server of a large WordPress site runs at full CPU during traffic peaks, mostly with reads, and a bigger server is no longer an option.
Solution: Enable GTID and row-based binary logging on the primary, create a replication user, copy a consistent snapshot to the replica with mysqldump --single-transaction --source-data, start replication with CHANGE REPLICATION SOURCE TO ... SOURCE_AUTO_POSITION = 1, and send WordPress reads to the replica with the HyperDB or LudicrousDB drop-in.
The commands use the MySQL 8.0.23+ syntax (SOURCE/REPLICA). Older versions and MariaDB use MASTER/SLAVE and MariaDB has its own GTID implementation.
The primary
# /etc/mysql/mysql.conf.d/replication.cnf on the primary [mysqld] server_id = 1 log_bin = mysql-bin binlog_format = ROW gtid_mode = ON enforce_gtid_consistency = ON binlog_expire_logs_seconds = 604800 bind-address = 10.0.0.1
CREATE USER 'repl'@'10.0.0.2' IDENTIFIED BY 'a-long-random-password'; GRANT REPLICATION SLAVE ON *.* TO 'repl'@'10.0.0.2';
Restart MySQL after changing the configuration. Allow port 3306 only from the replica’s private address in the firewall.
The snapshot
mysqldump --single-transaction --source-data=2 --set-gtid-purged=ON \ --routines --triggers --databases wordpress | gzip > snapshot.sql.gz
--single-transaction reads a consistent state of the InnoDB tables without locking the site. --set-gtid-purged=ON writes the set of transactions contained in the dump into it, so the replica knows where to continue.
The replica
# /etc/mysql/mysql.conf.d/replication.cnf on the replica [mysqld] server_id = 2 gtid_mode = ON enforce_gtid_consistency = ON read_only = ON super_read_only = ON
gunzip < snapshot.sql.gz | mysql -u root -p
CHANGE REPLICATION SOURCE TO
SOURCE_HOST = '10.0.0.1',
SOURCE_USER = 'repl',
SOURCE_PASSWORD = 'a-long-random-password',
SOURCE_AUTO_POSITION = 1,
SOURCE_SSL = 1;
START REPLICA;
SHOW REPLICA STATUS\G
In the status, Replica_IO_Running and Replica_SQL_Running must both be Yes, and Seconds_Behind_Source should be close to 0. super_read_only stops even administrators from writing to the replica by mistake.
WordPress reads from the replica
HyperDB (by Automattic) and its maintained fork LudicrousDB replace $wpdb through wp-content/db.php and a db-config.php with the servers:
// db-config.php (HyperDB / LudicrousDB) $wpdb->add_database( [ 'host' => '10.0.0.1', 'user' => DB_USER, 'password' => DB_PASSWORD, 'name' => DB_NAME, 'write' => 1, 'read' => 1 ] ); $wpdb->add_database( [ 'host' => '10.0.0.2', 'user' => DB_USER, 'password' => DB_PASSWORD, 'name' => DB_NAME, 'write' => 0, 'read' => 1 ] );
After a write in the same request, the drop-in keeps reading from the primary, so a page sees its own changes. Between requests, a replica that lags a few seconds can still show old data, for example right after saving a post; a lag alert and a page cache with purge on save keep that unnoticed.
NOTE: A replica is not a backup: a DELETE on the primary is copied to the replica within a second. Keep the dumps (automated backups with a Bash script), and before adding servers, check that a persistent object cache and a page cache have already taken the reads they can take.