MySQL replication with GTID for WordPress: set up, monitor, fail over
A replica of the WordPress database gives a second copy that is seconds behind, a place to run heavy reports and backups without touching the primary, and a server to switch to when the primary fails. GTID-based replication (global transaction identifiers) makes the last part much easier than the old file-and-position method: each transaction has a unique ID, and a replica knows exactly which ones it has applied.
Problem: A shop’s database runs on one server. Backups with mysqldump lock up the shop for a minute at night, and when the server’s disk failed last year, the shop was down for five hours while the backup was restored.
Solution: Enable GTID mode on the primary and a replica, clone the data to the replica, start replication with SOURCE_AUTO_POSITION = 1, monitor lag and errors with SHOW REPLICA STATUS, take backups from the replica, and practice a manual failover: stop writes, check that the replica caught up, promote it, point DB_HOST to it.
Configuration on both servers
# /etc/mysql/mysql.conf.d/zz-replication.cnf [mysqld] server_id = 1 # 2 on the replica, unique per server gtid_mode = ON enforce_gtid_consistency = ON log_bin = mysql-bin binlog_format = ROW # replica only: # read_only = ON # super_read_only = ON
enforce_gtid_consistency rejects statements that cannot be logged safely with GTIDs, such as CREATE TABLE ... SELECT. WordPress core and common plugins do not use them, but test on staging. super_read_only on the replica prevents accidental writes, even from admin accounts.
Start the replica
-- on the primary CREATE USER 'repl'@'10.0.0.%' IDENTIFIED BY 'long-random-password'; GRANT REPLICATION SLAVE ON *.* TO 'repl'@'10.0.0.%';
# copy the data with GTID information (or use the CLONE plugin / a physical backup for large databases) mysqldump --single-transaction --set-gtid-purged=ON --all-databases --triggers --routines --events -h primary | mysql -h replica
-- on the replica (MySQL 8.0.23+ syntax)
CHANGE REPLICATION SOURCE TO
SOURCE_HOST = '10.0.0.10',
SOURCE_USER = 'repl',
SOURCE_PASSWORD = 'long-random-password',
SOURCE_AUTO_POSITION = 1,
SOURCE_SSL = 1;
START REPLICA;
SOURCE_AUTO_POSITION = 1 lets the replica tell the primary which GTIDs it already has; no binlog file names or positions are needed. --set-gtid-purged=ON writes the dump’s GTID set into it, so the replica starts exactly after the dumped state.
Monitor
SHOW REPLICA STATUS\G -- Replica_IO_Running: Yes -- Replica_SQL_Running: Yes -- Seconds_Behind_Source: 0 -- Last_SQL_Error: -- Retrieved_Gtid_Set / Executed_Gtid_Set
Both threads must be Yes, and the lag should be near zero outside of bulk operations. A small cron check that alerts when a thread stops or the lag exceeds 60 seconds catches problems early; the same pattern as the site monitor in monitoring with shell scripts and cron. A stopped SQL thread usually means a write happened on the replica or a statement failed there; Last_SQL_Error says which.
Backups from the replica
Running mysqldump or a physical backup tool on the replica takes the load off the shop. With --source-data=2 or the GTID set in the dump, the backup also works as a base for point-in-time recovery: the binary log and point-in-time recovery.
Manual failover
-- 1. on the primary, if it is still reachable: stop writes SET GLOBAL super_read_only = ON; -- 2. on the replica: wait until all received transactions are applied, then promote SHOW REPLICA STATUS\G -- Retrieved_Gtid_Set fully contained in Executed_Gtid_Set STOP REPLICA; RESET REPLICA ALL; SET GLOBAL super_read_only = OFF; SET GLOBAL read_only = OFF;
Then set DB_HOST in wp-config.php to the new primary (or switch a DNS name or a ProxySQL hostgroup), flush the object cache, and later rebuild the old primary as a replica of the new one. Automatic failover tools (Orchestrator, MySQL InnoDB Cluster) exist, but a practiced manual procedure is enough for most single shops.
NOTE: A replica is not a backup: a DELETE on the primary is replicated within a second. Keep regular backups in addition: mysqldump and cron. MariaDB has its own GTID format and uses CHANGE MASTER TO ... MASTER_USE_GTID = slave_pos instead of the syntax above.