Skip to content

Recovering a Galera cluster from split-brain

Detection, bootstrap selection by seqno, the safe_to_bootstrap flag, and the safest path back to a Primary cluster after a partition or full outage.

A network partition takes out the leaf switch in front of one rack at 03:14. Your three Galera nodes are spread across two racks. Two on the surviving side keep going; the lone one on the isolated side trips its wsrep_provider_quorum_lost check and switches to non-Primary. Then power comes back at 03:32, the lone node rejoins, and wsrep_cluster_size reports 3 but wsrep_cluster_status is non-Primary on all three. The application is timing out on writes and your monitoring lit up two minutes ago. Now you have to pick a node to bootstrap — and if you pick the wrong one you discard the writes that happened on the other side of the partition. This is the runbook for that hour.

What’s happening

Galera’s quorum rule is “a node must see a majority of the last-known cluster to keep accepting writes.” When the cluster fragments into two sides that can no longer see each other, each side asks the same question and only one side can answer “yes I have majority.” The minority side flips to non-Primary and stops accepting writes — that is the design working correctly. The dangerous failure mode is not the partition; it’s the recovery after a full outage where nothing was alive to vote. In that case every node comes up holding only its on-disk state, and the cluster has to be told which on-disk state is canonical.

The grastate.dat file in the MariaDB datadir holds two fields that matter for recovery:

  • seqno: <number> — the last transaction this node certified before it stopped cleanly. -1 means the node went down uncleanly and the on-disk seqno is unreliable.
  • safe_to_bootstrap: 0|1 — Galera’s own flag indicating that this node was the last one running when the cluster shut down cleanly, so its state is the most up-to-date one available.

The node with safe_to_bootstrap: 1 is the one Galera wants you to bootstrap from. If no node has that flag, you fall back to comparing seqnos. If all nodes have seqno: -1, you have to recover the seqno from the InnoDB redo log with mysqld --wsrep-recover and pick the highest.

How to verify

First, find out what state each node believes it is in. Run on each node:

# Is the cluster Primary, non-Primary, or down?
mysql -uroot -p -e "SHOW STATUS LIKE 'wsrep_cluster_status'; SHOW STATUS LIKE 'wsrep_cluster_size'; SHOW STATUS LIKE 'wsrep_local_state_comment';"

If MariaDB won’t start at all — that’s the full-outage case — read the on-disk state directly:

cat /var/lib/mysql/grastate.dat

You’ll see something like:

# GALERA saved state
version: 2.1
uuid:    e4f3a0b8-4ab2-11ee-9c2a-1b2c3d4e5f60
seqno:   -1
safe_to_bootstrap: 0

seqno: -1 and safe_to_bootstrap: 0 everywhere means no node knows it was last alive. You will need to ask the InnoDB recovery to tell you the seqno:

mysqld --wsrep-recover --user=mysql 2>&1 | grep "Recovered position"
# Recovered position: e4f3a0b8-4ab2-11ee-9c2a-1b2c3d4e5f60:184372091

The number after the colon is the seqno this node would have if it could finish recovery. Run the same on all three nodes and record the results.

The fix

The runbook is a strict decision tree. Pick the right node first; everything downstream follows.

Step 1 — Decide which node has the highest committed state

In order of preference:

  1. A node with safe_to_bootstrap: 1 in /var/lib/mysql/grastate.dat. Galera already chose this one for you on the last clean shutdown. Use it.
  2. The node with the highest valid seqno in grastate.dat. Valid means a positive integer, not -1.
  3. The node with the highest “Recovered position” seqno from mysqld --wsrep-recover. Use this when all grastate.dat files show seqno: -1.

Document the seqnos from all three nodes before doing anything. A small whiteboard table:

Nodegrastate seqnoRecovered positionChosen
db01-1184372091
db02-1184372104X
db03-1184370882

In this example db02 is the bootstrap candidate.

Step 2 — Mark the chosen node safe to bootstrap

On db02:

systemctl stop mariadb     # make absolutely sure mysqld isn't running

# Edit /var/lib/mysql/grastate.dat — flip the flag to 1
sed -i 's/^safe_to_bootstrap:.*/safe_to_bootstrap: 1/' /var/lib/mysql/grastate.dat
cat /var/lib/mysql/grastate.dat

Step 3 — Bootstrap a new Primary cluster from the chosen node

MariaDB:

galera_new_cluster
# OR equivalently:
# systemctl start mariadb@bootstrap.service

PXC:

systemctl start mysql@bootstrap.service

Verify on the bootstrap node:

mysql -uroot -p -e "
  SHOW STATUS LIKE 'wsrep_cluster_status';   -- should be Primary
  SHOW STATUS LIKE 'wsrep_cluster_size';     -- should be 1 for now
  SHOW STATUS LIKE 'wsrep_local_state_comment'; -- should be Synced"

Step 4 — Bring the other nodes back as joiners

On each of the other two nodes, in sequence (not in parallel):

systemctl start mariadb        # MariaDB
# OR
systemctl start mysql          # PXC

# Watch the join
journalctl -u mariadb -f

You’ll see one of two recovery flows:

  • IST (Incremental State Transfer). The joiner’s seqno is recent enough that the donor can stream just the missing writesets from its gcache. This finishes in seconds. The log will say IST sender starting on <ip>:4568.
  • SST (Snapshot State Transfer). The joiner’s seqno is too old, so the donor takes a full backup (xtrabackup / mariabackup) and streams the dataset. The log will say WSREP_SST: Streaming the backup to joiner. For a 200 GB dataset on a 10 Gbps private network, expect 20 to 60 minutes.

After both joiners are Synced, the cluster is back. From any node:

mysql -uroot -p -e "SHOW STATUS LIKE 'wsrep_cluster_size';"
# 3
mysql -uroot -p -e "SHOW STATUS LIKE 'wsrep_cluster_status';"
# Primary
mysql -uroot -p -e "SHOW STATUS LIKE 'wsrep_local_state_comment';"
# Synced  (run on each node)

Step 5 — What you have lost

This is the part of the runbook that nobody likes. If the partition happened during writes and the side you discarded had different transactions than the side you bootstrapped from, those transactions are gone. There is no automatic merge. The minority-side writes are simply absent from the rebuilt cluster.

Two ways to mitigate the loss:

  • Compare binary logs. Before you stop the minority-side node, dump its binary log from the last known good seqno and apply the diff manually if it’s small and unambiguous:

    mysqlbinlog --start-position=<pos> --base64-output=DECODE-ROWS -v \
      /var/lib/mysql/binlog.000XXX > minority-side-writes.sql

    Inspect by hand. Apply via mysql -uroot -p on the new Primary if the writes are non-conflicting (typically appends, not updates).

  • Restore from a recent snapshot for the lost rows. If the missing data is bounded — a single table, a single hour — and you have a snapshot, restore that table from the snapshot into a temp database, diff against current, and replay the missing inserts.

Step 6 — Postmortem inputs

You want all of these recorded in writing while it’s fresh, not three weeks later when the action items get prioritized:

  • Time of partition start and time of recovery.
  • Which node was bootstrap-elected and why (seqno table).
  • Whether any minority-side writes were lost; if so, the count.
  • Whether gcache.size was big enough to allow IST instead of SST. If you saw SST on a node that was down for less than the gcache window, the gcache is sized wrong.
  • Whether the garbd arbitrator (or third datacenter node) prevented or mitigated the split-brain. If you don’t have one, the action item writes itself.

Galera split-brain recovery is one of the procedures Stack Harbor’s on-call runs for managed-database customers under environment management — if you’d rather have us pick up the pager for this, book a scoping call.