Updated: 29 September 2026 · Applies to: MariaDB 10.6 to 11.8 and MySQL 8.0 and 8.4 on Linux servers, including cPanel
In SHOW PROCESSLIST, idle client connections appear with the command Sleep. An older version of this article suggested a script that kills them every half second. We do not recommend that: sleeping connections use almost no CPU and only a little memory, killing them can break applications in the middle of their work, and a script that loops with a password on the command line is a security risk. The right fix is a timeout in the database server, and finding the application that leaves connections open. We tested the timeout on MariaDB 11.8.
1. Look at the connections
mysql -e "SELECT user, host, db, command, time FROM information_schema.PROCESSLIST WHERE command = 'Sleep' ORDER BY time DESC LIMIT 20" mysql -e "SHOW GLOBAL STATUS LIKE 'Threads_connected'; SHOW GLOBAL VARIABLES LIKE 'max_connections'"
A few sleeping connections are normal. They become a problem when there are hundreds of them, when Threads_connected gets close to max_connections, or when websites report "Too many connections".
2. Close idle connections with wait_timeout
The server closes a connection that has been idle longer than wait_timeout (for interactive clients such as the mysql shell, interactive_timeout). The default is 28800 seconds, eight hours. For web applications, a few minutes is usually enough. Try it at runtime:
mysql -e "SET GLOBAL wait_timeout = 300; SET GLOBAL interactive_timeout = 300"
In our test with wait_timeout set to 5 seconds, an idle connection disappeared from the process list within a few seconds after the limit. To keep the setting after a restart, add it to the [mysqld] section of the configuration file (/etc/my.cnf or a file in /etc/my.cnf.d/; on Ubuntu and Debian a file in /etc/mysql/mariadb.conf.d/ or /etc/mysql/mysql.conf.d/):
[mysqld] wait_timeout = 300 interactive_timeout = 300
On cPanel servers, add the lines to /etc/my.cnf and restart with /usr/local/cpanel/scripts/restartsrv_mysql (the same script restarts MariaDB).
Do not go below about 60 seconds: long-running jobs, such as imports or cron scripts that pause between queries, may lose their connection.
3. Find the cause
- Persistent connections in PHP (
mysqliwith ap:host prefix, or PDO withPDO::ATTR_PERSISTENT) keep one connection per PHP process open. With many PHP-FPM workers, that adds up. Turn persistent connections off unless you need them. - Too many PHP-FPM workers: each worker may hold a connection. Match
pm.max_childrento what the server can handle. - Connection pools in Java, Node.js or Python applications: set their idle timeout lower than
wait_timeout, so the application closes connections before the server does.
Group the sleeping connections by user to see which application they belong to:
mysql -e "SELECT user, COUNT(*) FROM information_schema.PROCESSLIST WHERE command = 'Sleep' GROUP BY user ORDER BY 2 DESC"
If you must end one connection
Kill a single connection by its ID from the process list: mysql -e "KILL 12345". Do this by hand, for a specific stuck connection, not in a loop.
Official documentation: MariaDB server system variables and MySQL 8.4 server system variables.
Ucartz services for this topic
- KVM VPS hosting: NVMe SSD and full root access, with Free Basic Managed Support.
- Unmanaged dedicated servers: full root access and free IPMI KVM, with Free Basic Managed Support.
- Linux server management: hardening, patching, monitoring and automation for Ubuntu, Debian, RHEL and AlmaLinux.
