The database refuses connections with “Too many connections”
MariaDB refuses site and client connections with ERROR 1040 (08004): Too many connections when it reaches max_connections, which defaults to 151. To fix it, find what holds the connections and raise the limit if the server has enough memory.
Symptoms
Clients receive this error when they try to connect.
ERROR 1040 (08004): Too many connectionsWordPress and other applications show their database connection errors at the same time.
Cause
MariaDB accepts up to max_connections connections at once. The distribution sets it to 151, and UrbeStack leaves that value to you.
A slow query, a site under heavy load, or connections an application leaves open can fill the available slots.
Resolution
Run these commands as root to see the peak number of simultaneous connections since MariaDB started and what holds the connections now. MariaDB allows one additional connection for an administrator.
mariadb -N -e "SHOW GLOBAL STATUS LIKE 'Max_used_connections'" mariadb -e "SHOW PROCESSLIST"In the process list, the
Useranddbcolumns show which site holds each connection. TheTimecolumn shows how long each has run.Each connection uses memory. Raise the limit only as far as the server's memory allows. This command raises it to
300immediately, until MariaDB restarts.mariadb -e "SET GLOBAL max_connections=300"To keep the setting after a restart, add
max_connections = 300under[mysqld]in/etc/my.cnf.d/mariadb-server.cnfon AlmaLinux or/etc/mysql/mariadb.conf.d/50-server.cnfon Ubuntu. Then runurbestack-service-restart --name mariadb.