MySQL Connection Pooling with ProxySQL for WordPress

“Too many connections”: PHP-FPM, MySQL and when ProxySQL helps

PHP has no connection pool in the usual sense: every request that WordPress handles opens its own MySQL connection and closes it at the end. Under load, the number of connections equals the number of busy PHP-FPM workers, from every web server. When that number exceeds max_connections, MySQL refuses new connections and WordPress shows “Error establishing a database connection”. Often the right fix is in the PHP-FPM settings; with several web servers or a read replica, a proxy like ProxySQL is the next step.

Problem: Three web servers with 60 PHP-FPM workers each share one MySQL server with max_connections = 151. During a campaign, the error log fills with “Too many connections”, although the database itself is not busy.

Solution: First align the numbers: the sum of pm.max_children over all web servers must stay below max_connections with room for admin and cron. If more workers are really needed, or reads should go to a replica, put ProxySQL on each web server: WordPress connects to it locally, and ProxySQL multiplexes the requests over a smaller, persistent set of connections to MySQL.

Check the numbers

SHOW VARIABLES LIKE 'max_connections';
SHOW GLOBAL STATUS LIKE 'Max_used_connections';
SHOW GLOBAL STATUS LIKE 'Threads_connected';
SHOW PROCESSLIST;     -- what the connections are doing right now
grep -E '^pm\.(max_children|start_servers)' /etc/php/8.3/fpm/pool.d/www.conf

If Max_used_connections reaches max_connections and the process list shows many connections in state “Sleep”, the problem is not the database but the number of workers that hold connections. 3 × 60 = 180 workers can never fit into 151 connections. Either lower pm.max_children (requests then wait in the FPM queue instead of failing) or raise max_connections, which costs memory per connection on the MySQL server. How to size the FPM pool: tune the PHP-FPM pool for WordPress.

Persistent connections in PHP (mysqli with p: host prefix) are not a fix: each worker keeps its own connection open even while idle, so the count stays the same and stale session state can leak between requests.

ProxySQL in front of MySQL

# /etc/proxysql.cnf (excerpt), on each web server
mysql_servers =
(
    { address = "10.0.0.10", port = 3306, hostgroup = 10, max_connections = 40 },  # primary
    { address = "10.0.0.11", port = 3306, hostgroup = 20, max_connections = 40 }   # replica
)

mysql_users =
(
    { username = "wp", password = "secret", default_hostgroup = 10 }
)

mysql_query_rules =
(
    { rule_id = 1, active = 1, match_digest = "^SELECT .* FOR UPDATE", destination_hostgroup = 10, apply = 1 },
    { rule_id = 2, active = 1, match_digest = "^SELECT", destination_hostgroup = 20, apply = 1 }
)
// wp-config.php
define( 'DB_HOST', '127.0.0.1:6033' ); // ProxySQL's MySQL interface

ProxySQL accepts one client connection per PHP worker, but sends their queries over at most 40 backend connections per server, reusing a connection as soon as a query is done (multiplexing). The query rules send plain SELECT queries to the replica and everything else to the primary.

The catch with read/write splitting

WordPress reads data right after writing it: save a post, then load it for the redirect. If the replica lags behind by a second, the read returns old data. For WordPress, it is safer to start with all queries on the primary (no rule 2) and only use ProxySQL’s multiplexing, then route specific heavy reads (reports, search) to the replica deliberately. Multiplexing is also disabled for a connection that uses session variables, temporary tables or LAST_INSERT_ID() patterns, which WordPress does; ProxySQL handles that automatically, but the reduction of backend connections is then smaller than the numbers suggest.

Watch it

mysql -u admin -padmin -h 127.0.0.1 -P 6032 -e "SELECT hostgroup, srv_host, status, ConnUsed, ConnFree, Queries FROM stats_mysql_connection_pool;"

The admin interface on port 6032 shows how many backend connections are used and free per server.

NOTE: A persistent object cache reduces the number of queries per request so much that connection problems often disappear without any proxy: object caching with Redis. ProxySQL is worth its complexity on multi-server setups; a single server with a sensible pm.max_children rarely needs it.