You picked Percona XtraDB Cluster instead of vanilla Galera because the application team needs MySQL 8 syntax and the existing tooling is already Percona-flavored — Percona Toolkit, pt-online-schema-change, xtrabackup. Now you need it in front of an app that opens five hundred connections, expects writes to land somewhere consistent, and reads to spread out. A naked PXC with mysql -h db01 will work until the day db01 becomes a donor for a joining node and stops serving traffic. ProxySQL is the piece that fixes that — it knows which member is wsrep_local_state=4, routes writes to a single writer, and shifts reads across the healthy followers.
What’s happening
PXC is Galera under the hood: certification-based, multi-primary, three nodes minimum. ProxySQL sits in front as a MySQL-protocol-aware proxy that maintains pools per “hostgroup”. The PXC scheduler (a ProxySQL scheduler job running proxysql_galera_checker) polls the mysql_servers table and adjusts hostgroup membership based on wsrep_local_state. Hostgroup 10 is the writer pool — by convention only one node lives there at a time. Hostgroup 20 is the reader pool — all Synced nodes go there. Hostgroup 30 is the “donor/desync” parking lot for nodes temporarily out of the rotation. Your application connects only to ProxySQL, and ProxySQL routes by query rule: ^SELECT lands on hostgroup 20, everything else on hostgroup 10. The application has no opinion on which node it’s talking to. Failure of the active writer is two seconds of dropped connections while the scheduler promotes the next eligible node into hostgroup 10.
How to verify
PXC cluster state from any node:
mysql -uroot -p -e "SHOW STATUS LIKE 'wsrep_cluster_size';"
mysql -uroot -p -e "SHOW STATUS LIKE 'wsrep_cluster_status';"
mysql -uroot -p -e "SHOW STATUS LIKE 'wsrep_local_state_comment';"
ProxySQL hostgroup view (run on the ProxySQL host):
mysql -uadmin -padmin -h127.0.0.1 -P6032 -e "
SELECT hostgroup_id, hostname, port, status, weight
FROM runtime_mysql_servers
ORDER BY hostgroup_id, hostname;"
You should see one entry in hostgroup 10 (the writer), three or four in hostgroup 20 (readers — depending on whether the writer is also in the reader pool), and zero in hostgroup 30 unless a node is desync’d.
Stats:
mysql -uadmin -padmin -h127.0.0.1 -P6032 -e "
SELECT hostgroup, srv_host, Queries, Bytes_data_sent, Bytes_data_recv, Latency_us
FROM stats.stats_mysql_connection_pool;"
If Queries is climbing on hostgroup 10 across multiple servers simultaneously, the writer policy isn’t single-writer — fix it before two nodes start certifying conflicting writes.
The fix
Save as /root/setup-pxc-node.sh and run on each of three data nodes. First node bootstraps the cluster; the other two join.
#!/usr/bin/env bash
set -euo pipefail
### Ubuntu 22.04 — Percona XtraDB Cluster 8.0 — 3 data nodes
### Run on each node; BOOTSTRAP=yes on the first one only
if [[ "$(id -u)" -ne 0 ]]; then
echo "Run as root."
exit 1
fi
# === EDIT PER NODE ===
NODE_NAME="pxc01"
NODE_IP="10.30.0.11"
PEER_IPS=("10.30.0.11" "10.30.0.12" "10.30.0.13")
CLUSTER_NAME="pxc_prod"
ROOT_PASSWORD="CHANGE_ME_ROOT"
SST_PASSWORD="CHANGE_ME_SST"
MONITOR_PASSWORD="CHANGE_ME_MONITOR"
BOOTSTRAP="no"
# =====================
export DEBIAN_FRONTEND=noninteractive
echo
echo "========================================"
echo "PXC node ${NODE_NAME} (${NODE_IP})"
echo "Bootstrap=${BOOTSTRAP}"
echo "========================================"
echo
echo "==> Base apt update"
apt-get update
apt-get -y upgrade
apt-get -y install curl wget gnupg lsb-release ca-certificates \
chrony fail2ban unattended-upgrades \
vim net-tools tcpdump bind9-dnsutils tar gzip socat jq \
rsync openssl ufw
systemctl enable --now chrony
echo "==> Sysctl tuning"
cat > /etc/sysctl.d/99-pxc.conf <<'SYSCTL'
vm.swappiness = 1
vm.overcommit_memory = 2
vm.dirty_background_ratio = 3
vm.dirty_ratio = 10
fs.file-max = 2097152
net.core.somaxconn = 4096
net.ipv4.tcp_max_syn_backlog = 8192
net.ipv4.tcp_syncookies = 1
SYSCTL
sysctl --system
cat > /etc/security/limits.d/99-mysql.conf <<'LIMITS'
mysql soft nofile 200000
mysql hard nofile 200000
LIMITS
echo "==> SSH lockdown"
sed -i 's|^[#[:space:]]*PermitRootLogin.*|PermitRootLogin prohibit-password|' /etc/ssh/sshd_config
sed -i 's|^[#[:space:]]*PasswordAuthentication.*|PasswordAuthentication no|' /etc/ssh/sshd_config
sshd -t && systemctl restart ssh
cat > /etc/fail2ban/jail.d/sshd.local <<'F2B'
[sshd]
enabled = true
port = ssh
backend = systemd
maxretry = 4
findtime = 10m
bantime = 1h
F2B
systemctl enable --now fail2ban
dpkg-reconfigure -fnoninteractive unattended-upgrades
echo "==> Percona repo + PXC 8.0"
wget -qO percona-release.deb https://repo.percona.com/apt/percona-release_latest.$(lsb_release -sc)_all.deb
dpkg -i percona-release.deb
percona-release setup pxc-80
apt-get update
# Pre-seed root password before install
debconf-set-selections <<DEB
percona-xtradb-cluster-server-8.0 percona-server-server/root-pass password ${ROOT_PASSWORD}
percona-xtradb-cluster-server-8.0 percona-server-server/re-root-pass password ${ROOT_PASSWORD}
DEB
apt-get -y install percona-xtradb-cluster percona-xtradb-cluster-client
systemctl stop mysql || true
echo "==> Writing wsrep.cnf"
cat > /etc/mysql/mysql.conf.d/wsrep.cnf <<EOF
[mysqld]
bind-address = 0.0.0.0
default-storage-engine = innodb
binlog_format = ROW
default_authentication_plugin = mysql_native_password
innodb_autoinc_lock_mode = 2
innodb_doublewrite = 1
innodb_flush_log_at_trx_commit = 2
innodb_buffer_pool_size = 4G
innodb_log_file_size = 512M
innodb_file_per_table = 1
max_connections = 1500
table_open_cache = 4000
performance_schema = ON
# wsrep
wsrep_provider = /usr/lib/galera4/libgalera_smm.so
wsrep_cluster_name = ${CLUSTER_NAME}
wsrep_cluster_address = gcomm://${PEER_IPS[0]},${PEER_IPS[1]},${PEER_IPS[2]}
wsrep_node_name = ${NODE_NAME}
wsrep_node_address = ${NODE_IP}
wsrep_sst_method = xtrabackup-v2
wsrep_sst_auth = sstuser:${SST_PASSWORD}
wsrep_slave_threads = 8
pxc_strict_mode = ENFORCING
wsrep_provider_options = "gcache.size=2G;gcs.fc_limit=1000"
EOF
# Make sure default datadir is initialized when PXC first runs unbootstrapped
mkdir -p /var/lib/mysql
chown -R mysql:mysql /var/lib/mysql
echo "==> Starting mysql"
if [[ "${BOOTSTRAP}" == "yes" ]]; then
echo "==> Bootstrap mode"
systemctl start mysql@bootstrap.service
else
systemctl start mysql
fi
systemctl enable mysql
echo "==> Securing root + creating SST + monitor users"
mysql -uroot -p"${ROOT_PASSWORD}" <<SQL
CREATE USER IF NOT EXISTS 'sstuser'@'localhost' IDENTIFIED BY '${SST_PASSWORD}';
GRANT RELOAD, PROCESS, LOCK TABLES, BACKUP_ADMIN, REPLICATION CLIENT ON *.* TO 'sstuser'@'localhost';
CREATE USER IF NOT EXISTS 'monitor'@'%' IDENTIFIED BY '${MONITOR_PASSWORD}';
GRANT USAGE, REPLICATION CLIENT, PROCESS ON *.* TO 'monitor'@'%';
CREATE USER IF NOT EXISTS 'proxysql'@'%' IDENTIFIED BY '${MONITOR_PASSWORD}';
GRANT SELECT ON sys.* TO 'proxysql'@'%';
GRANT USAGE, REPLICATION CLIENT, PROCESS ON *.* TO 'proxysql'@'%';
FLUSH PRIVILEGES;
SQL
echo "==> Firewall"
ufw allow 22/tcp
ufw allow 3306/tcp
ufw allow 4444/tcp
ufw allow 4567/tcp
ufw allow 4568/tcp
ufw --force enable
echo
echo "========================================"
echo "DONE — ${NODE_NAME}"
echo "========================================"
mysql -uroot -p"${ROOT_PASSWORD}" -e "
SHOW STATUS LIKE 'wsrep_cluster_size';
SHOW STATUS LIKE 'wsrep_cluster_status';
SHOW STATUS LIKE 'wsrep_local_state_comment';" || true
echo
echo "Listeners:"
ss -lntp | grep -E ':(3306|4444|4567|4568)\b' || true
Now stand up the ProxySQL front. Save as /root/setup-proxysql.sh on a dedicated small host:
#!/usr/bin/env bash
set -euo pipefail
export DEBIAN_FRONTEND=noninteractive
# === EDIT ===
PXC_NODES=("10.30.0.11" "10.30.0.12" "10.30.0.13")
WRITER_HOSTGROUP=10
READER_HOSTGROUP=20
BACKUP_WRITER_HOSTGROUP=11
OFFLINE_HOSTGROUP=30
MONITOR_USER="monitor"
MONITOR_PASSWORD="CHANGE_ME_MONITOR"
APP_USER="app"
APP_PASSWORD="CHANGE_ME_APP"
PROXYSQL_ADMIN_PW="CHANGE_ME_ADMIN"
# ============
apt-get update
apt-get -y install curl wget gnupg lsb-release ca-certificates
wget -qO percona-release.deb https://repo.percona.com/apt/percona-release_latest.$(lsb_release -sc)_all.deb
dpkg -i percona-release.deb
percona-release enable proxysql release
apt-get update
apt-get -y install proxysql2 mysql-client
systemctl enable --now proxysql
# Wait for admin port
for i in {1..30}; do
if nc -z 127.0.0.1 6032; then break; fi
sleep 1
done
mysql -uadmin -padmin -h127.0.0.1 -P6032 <<SQL
-- 1) admin credentials
UPDATE global_variables SET variable_value='admin:${PROXYSQL_ADMIN_PW}' WHERE variable_name='admin-admin_credentials';
LOAD ADMIN VARIABLES TO RUNTIME;
SAVE ADMIN VARIABLES TO DISK;
-- 2) monitor user (ProxySQL polls the backend with this)
UPDATE global_variables SET variable_value='${MONITOR_USER}' WHERE variable_name='mysql-monitor_username';
UPDATE global_variables SET variable_value='${MONITOR_PASSWORD}' WHERE variable_name='mysql-monitor_password';
UPDATE global_variables SET variable_value='2000' WHERE variable_name='mysql-monitor_connect_interval';
UPDATE global_variables SET variable_value='2000' WHERE variable_name='mysql-monitor_ping_interval';
LOAD MYSQL VARIABLES TO RUNTIME;
SAVE MYSQL VARIABLES TO DISK;
-- 3) backend servers — all start in the writer hostgroup; the scheduler will re-arrange
DELETE FROM mysql_servers;
SQL
for ip in "${PXC_NODES[@]}"; do
mysql -uadmin -padmin -h127.0.0.1 -P6032 -e "
INSERT INTO mysql_servers (hostgroup_id, hostname, port, weight)
VALUES (${WRITER_HOSTGROUP}, '${ip}', 3306, 1000);
INSERT INTO mysql_servers (hostgroup_id, hostname, port, weight)
VALUES (${READER_HOSTGROUP}, '${ip}', 3306, 1000);"
done
mysql -uadmin -padmin -h127.0.0.1 -P6032 <<SQL
LOAD MYSQL SERVERS TO RUNTIME;
SAVE MYSQL SERVERS TO DISK;
-- 4) Galera awareness table
DELETE FROM mysql_galera_hostgroups;
INSERT INTO mysql_galera_hostgroups
(writer_hostgroup, backup_writer_hostgroup, reader_hostgroup, offline_hostgroup,
active, max_writers, writer_is_also_reader, max_transactions_behind, comment)
VALUES
(${WRITER_HOSTGROUP}, ${BACKUP_WRITER_HOSTGROUP}, ${READER_HOSTGROUP}, ${OFFLINE_HOSTGROUP},
1, 1, 1, 100, 'pxc-prod');
LOAD MYSQL SERVERS TO RUNTIME;
SAVE MYSQL SERVERS TO DISK;
-- 5) Application user (must also exist on PXC with same password)
DELETE FROM mysql_users WHERE username='${APP_USER}';
INSERT INTO mysql_users (username, password, default_hostgroup, max_connections)
VALUES ('${APP_USER}', '${APP_PASSWORD}', ${WRITER_HOSTGROUP}, 1000);
LOAD MYSQL USERS TO RUNTIME;
SAVE MYSQL USERS TO DISK;
-- 6) Query rules — reads to reader hostgroup, writes to writer
DELETE FROM mysql_query_rules;
INSERT INTO mysql_query_rules (rule_id, active, match_digest, destination_hostgroup, apply)
VALUES (10, 1, '^SELECT.*FOR UPDATE', ${WRITER_HOSTGROUP}, 1),
(20, 1, '^SELECT', ${READER_HOSTGROUP}, 1);
LOAD MYSQL QUERY RULES TO RUNTIME;
SAVE MYSQL QUERY RULES TO DISK;
SQL
echo
echo "========================================"
echo "ProxySQL configured"
echo "========================================"
mysql -uadmin -p"${PROXYSQL_ADMIN_PW}" -h127.0.0.1 -P6032 -e "
SELECT hostgroup_id, hostname, status FROM runtime_mysql_servers ORDER BY hostgroup_id, hostname;"
You must also create the app user on PXC with the same password before the application can connect through ProxySQL:
CREATE USER 'app'@'%' IDENTIFIED BY 'CHANGE_ME_APP';
GRANT ALL PRIVILEGES ON appdb.* TO 'app'@'%';
FLUSH PRIVILEGES;
The application connects to ProxySQL on :6033 (the data port) using the app user — no awareness of which physical node is the current writer. Run a failover drill: systemctl stop mysql on the active writer, watch runtime_mysql_servers move that host from hostgroup 10 to hostgroup 30, and confirm a new connection from the application lands on the new writer. The whole transition should be under five seconds.
For an HA MySQL tier you don’t have to run yourself, Stack Harbor operates PXC + ProxySQL clusters on Cloud Compute under environment management — or book a scoping call if you’d like us to take over an existing deployment.