Appearance
MariaDB primary and replica
This recipe runs a MariaDB primary on one Server and a replica on another, with Roles. It is a recipe, not a Yoho feature. Yoho does not fail over. You promote the replica by hand, with the steps below. CI runs this exact example and every step on this page (test/integration/mysql-replication.sh).
The files are in examples/mysql-replication.
It uses MariaDB 11.4 LTS. MariaDB always writes GTIDs, so there is no gtid_mode to switch on, and a replica resumes from its own gtid_slave_pos with MASTER_USE_GTID=slave_pos. mariadb-dump --gtid records that position in the copy it makes. MySQL 8.4 needs gtid_mode=ON and enforce_gtid_consistency=ON, SOURCE_AUTO_POSITION=1, super_read_only, and its own replica commands (STOP REPLICA, RESET REPLICA ALL, WAIT_FOR_EXECUTED_GTID_SET). The structure of this recipe carries over, but CI tests only MariaDB.
How it works
db(Servera) anddb-replica(Serverb) are two Stateful Services. Each has one Server, as ADR 0005 requires. They use the same image, entrypoint andreplication.cnf. The entrypoint gives each one its ownserver_id, from the Service name.The Connection
primarydecides which one is the primary:yamlconnections: primary: {service: db, port: 3306, consumers: [web, db-replica]}A consumer of
primarywith an empty data directory initializes, copies the primary'sappdatabase withmariadb-dump --single-transaction --gtid, and replicates from the position in that copy. It runs read-only. A Service that is not a consumer and has an empty data directory initializes as the primary.webconnects toYOHO_CONNECTION_PRIMARY_HOSTandYOHO_CONNECTION_PRIMARY_PORT, not to the namedb. After a promotion the same variables point at the new primary.Replication is asynchronous. A write the primary acknowledged can be lost if the primary's Server dies before the replica received it. For fewer lost writes, enable semi-synchronous replication (
rpl_semi_sync_master_enabledon the primary,rpl_semi_sync_slave_enabledon the replica); then each commit waits until the replica has received it.
Set it up
Copy the example directory. In
yoho.yml, set each Server'ssshandprivate_address(or use the WireGuard mesh). Incompose.yaml, set the Proxy host ofweb.Create
.yoho/secretsfrom.yoho/secrets.examplewith long random values:shMARIADB_ROOT_PASSWORD=... MARIADB_PASSWORD=... REPLICATOR_PASSWORD=... DEMO_WRITE_TOKEN=...These are user secrets, not
x-yoho.generate. A generated secret has one owning Service (ADR 0019). After a promotion that owner would be a read-only replica, and rotation would fail. Both database Services receive all three passwords as files.MARIADB_PASSWORDis the password of theappuser thatwebuses. Each password must be one line: the entrypoint writes them into SQL, one statement per line. Trailing newlines in a secret file are ignored (the shell and the image's*_FILEreading strip them the same way); any other newline or a carriage return makes the entrypoint refuse to start, naming the secret but never its value.Run
yoho deploy. Yoho startsdbfirst, thendb-replica(provider before consumer). The replica copies the primary and starts replicating.
Check replication on the replica:
sh
yoho app exec db-replica -- sh /etc/yoho-mariadb/entrypoint.sh mariadb -e "SHOW REPLICA STATUS\G"Slave_IO_Running and Slave_SQL_Running should both be Yes. The entrypoint's mariadb command runs the client as root on the local socket and reads the root password from its secret file.
The web Service is a demo. It reads a row count at /cgi-bin/read. Its write endpoint accepts only a POST whose body is DEMO_WRITE_TOKEN:
sh
curl --data-binary @token-file https://mariadb.example.com/cgi-bin/write?helloReplace it with your App. The app user has privileges on the app database only, so read_only stops its writes.
What protects the database
- The Connection firewall admits only consumer Servers to port 3306 on the primary's private address. Other hosts are refused, even when another rule accepts all forwarded traffic.
rootexists only asroot@localhost. Thereplicatoruser can replicate and read theappdatabase, for the copy; nothing else. Every network login needs a password.- The replica runs with
read_only. Replication still applies changes; theappuser cannot write. - A replica copy keeps a marker in the data directory until it is complete and configured. If the container stops mid-copy, the next start wipes the partial data and copies again.
- MariaDB stores the replication password in
master.infoin the replica's data directory, readable only by themysqluser. The entrypoint updates it from the secret file on every start. No password is in a command line. A failed statement that carries a password (creating thereplicatoruser,CHANGE MASTER TO,set-passwords) prints only its name and exit code, never the client's error, which can quote the statement. - The copy holds the
appdatabase only. Users are created on each node from the secrets at initialization. If your App uses stored routines, grantreplicatorSHOW CREATE ROUTINEonappand add--routinesto themariadb-dumpline ininitdb/10-replication.sh.
Promote the replica
Do this for a planned switchover, with both Servers up. Do the steps in order. If Server a is lost, read Server a is lost first.
Fence the old primary. Stop new writes:
shyoho app exec db -- sh /etc/yoho-mariadb/entrypoint.sh fenceThis ends the App's sessions, sets
read_only, and leaves a marker sodbalso starts read-only after a restart. Then read the primary's last GTID:shyoho app exec db -- sh /etc/yoho-mariadb/entrypoint.sh gtidPromote. Pass that GTID position:
shyoho app exec db-replica -- sh /etc/yoho-mariadb/entrypoint.sh promote 0-2100691912-42It waits up to 120 seconds until the replica has applied that position (
MASTER_GTID_WAIT) and refuses to promote if it has not. Then it runsSTOP SLAVE,RESET SLAVE ALLandSET GLOBAL read_only = OFF. Do steps 3 and 4 right away: until thendb-replicais still a consumer, and a restart brings it back read-only.Switch the Connection. In
yoho.yml:yamlprimary: {service: db-replica, port: 3306, consumers: [web, db]}Apply. Run
yoho apply.webnow connects todb-replica. The firewall now protects port 3306 on Serverb. The olddbrestarts read-only: it is fenced, and its entrypoint sees a consumer with old primary data.Rebuild the old primary as a replica. Yoho never deletes volumes, so remove the old data by hand on Server
a:shid=$(docker ps -q --filter label=com.docker.compose.project=yoho-mariadb-production --filter label=com.docker.compose.service=db) docker rm -f "$id" docker volume rm yoho-mariadb-production_mariadbCheck the volume name with
docker volume lsfirst. Then runyoho applyagain.dbstarts with an empty data directory as a consumer, and copiesdb-replica.
The roles are now reversed. To switch back later, repeat the steps with the names swapped.
Server a is lost
A Server you cannot reach over SSH is not fenced. It may still run and accept writes from clients you cannot see. Before you promote, make sure the old primary cannot accept writes: power Server a off in your provider's console and confirm that it stays off. Detaching only its external network is not enough, because web and the old primary may still write to each other on Server a.
Then promote db-replica without a GTID (entrypoint.sh promote --no-wait; there is no primary to compare with, and a plain promote needs a GTID) and move everything to Server b. In yoho.yml, remove a from the top-level servers (this also removes it from the mesh) and from the Destination, drop the old Role, place web on b, and leave web as the only consumer:
yaml
servers:
b: {ssh: root@203.0.113.11, private_address: 10.0.0.11}
destinations:
production:
servers: [b]
roles:
app: {services: [web], servers: [b]}
replica: {services: [db-replica], servers: [b]}
connections:
primary: {service: db-replica, port: 3306, consumers: [web]}Delete the db Service from compose.yaml, then run yoho apply (add --version if the App directory is not a Git checkout). If DNS, a Cloudflare Tunnel or a load balancer still sends traffic to Server a, point it at Server b. The App now runs on one Server with no replica. To add a replica again, add a new Server, a Service for it, and make it a consumer of primary. Never start the old Server a with its data again; wipe its disk first.
Rotate the passwords
REPLICATOR_PASSWORD (tested in CI):
Put the new value in
.yoho/secrets.Run
yoho deploy. Both database Services restart with the new secret files, and the replica's entrypoint gives the new password toCHANGE MASTER TO.Set the new password on the primary:
shyoho app exec db -- sh /etc/yoho-mariadb/entrypoint.sh set-passwordsIt sets the
replicatorandapppasswords from the secret files, over stdin. The change is replicated, so the replica has the same users. The replica retries and reconnects with the new password.
Between steps 2 and 3, the replica cannot log in and replication waits. Run step 3 right after the deploy. After a promotion, run it on db-replica.
MARIADB_PASSWORD (web uses it, tested in CI) and MARIADB_ROOT_PASSWORD: put the new value in .yoho/secrets and run yoho deploy. The image reads the secret files only when it initializes an empty data directory, so on every start the entrypoint passes the server an --init-file that sets the app and root@localhost passwords from the secret files before it serves clients. The init file runs as the server itself, so a changed root secret cannot lock the entrypoint out. It is not binary logged: each node sets its own users when it restarts, and the deploy restarts both, before web, because their secrets changed. The init file is in the container's /run/yoho-mariadb, readable only by mysql. Root rotation is not covered by CI.
Backups
Replication copies mistakes too: a dropped table is dropped on the replica. Keep off-Server Backups of the primary. After a promotion, move the Backup job to the Service that is now the primary.
Limits
- Yoho does not detect a failed primary and does not promote anything. Automatic failover needs a resident leader election, which ADR 0003 rules out.
- One replica. More replicas work the same way: add a Service, a Role and a consumer.
- Binary logs are kept for a week (
replication.cnf). A replica that was down longer cannot catch up; rebuild it (step 5). - A planned helper (
yoho db promote) is in the backlog.