Lab 10: ProxySQL Routing

Time: 45 minutes | Level: Advanced | DB: MySQL 8.0 + ProxySQL

Overview

ProxySQL sits between your application and MySQL servers, transparently routing queries based on rules. Write queries go to the primary; read queries are distributed to replicas. This enables read scaling without changing application code.


Step 1: Launch MySQL Primary and Replica

docker network create proxysql-net

# Start primary
docker run -d \
  --name mysql-primary \
  --network proxysql-net \
  --hostname mysql-primary \
  -e MYSQL_ROOT_PASSWORD=rootpass \
  -e MYSQL_DATABASE=appdb \
  mysql:8.0 \
  --server-id=1 \
  --log-bin=mysql-bin \
  --binlog-format=ROW \
  --gtid-mode=ON \
  --enforce-gtid-consistency=ON

# Start replica
docker run -d \
  --name mysql-replica \
  --network proxysql-net \
  --hostname mysql-replica \
  -e MYSQL_ROOT_PASSWORD=rootpass \
  mysql:8.0 \
  --server-id=2 \
  --log-bin=mysql-bin \
  --binlog-format=ROW \
  --gtid-mode=ON \
  --enforce-gtid-consistency=ON \
  --read-only=ON

# Wait for both
for srv in mysql-primary mysql-replica; do
  for i in $(seq 1 30); do
    docker exec $srv mysql -uroot -prootpass -e "SELECT 1" 2>/dev/null && break || sleep 2
  done
  echo "$srv ready"
done

📸 Verified Output:


Step 2: Configure Replication Between MySQL Nodes

📸 Verified Output:


Step 3: Launch ProxySQL

📸 Verified Output:


Step 4: Configure MySQL Servers in ProxySQL

📸 Verified Output:

💡 Including the primary in the reader hostgroup (with low weight) provides a fallback if all replicas fail.


Step 5: Configure Application Users and Query Rules

📸 Verified Output:


Step 6: Test Query Routing Through ProxySQL

📸 Verified Output:


Step 7: Monitor Connection Pool

📸 Verified Output:


Step 8: Capstone — Simulate Replica Failure and Failover

📸 Verified Output:


Summary

ProxySQL Component
Location
Purpose

Admin interface

Port 6032

Configuration and monitoring

MySQL interface

Port 6033

Application connections

mysql_servers

Table

Backend MySQL servers per hostgroup

mysql_users

Table

Application credentials + default hostgroup

mysql_query_rules

Table

Regex-based query routing rules

LOAD ... TO RUNTIME

Command

Activate configuration changes

SAVE ... TO DISK

Command

Persist configuration across restarts

stats_mysql_connection_pool

View

Real-time connection monitoring

stats_mysql_query_digest

View

Per-query routing statistics

Key Takeaways

  • Two-step configuration: LOAD TO RUNTIME (activate) + SAVE TO DISK (persist)

  • Query rules are regex-based — order matters (lower rule_id = higher priority)

  • ProxySQL auto-detects failures via monitoring user ping — SHUNNED status = server removed

  • Hostgroup 10 = writes, 20 = reads by convention; you define the routing rules

  • Use SELECT @@hostname to verify which backend a query hit — essential for debugging

Last updated