Mysql – Which MySQL queries are causing this error message

MySQL

I found this Stack Overflow Q & A:

mysql: see all open connections to a given database?

But by the moments that is the limit of connections, I have not access to my server, which displays error messages:

ERROR: Zend_Db_Adapter_Exception: SQLSTATE[08004] [1040] 
 Too many connections (Abstract.php:144)

ERROR: PleskDBException: Unable to connect to database: 
 mysql_connect(): Too many connections (Error code: 1040) (Abstract.php:69)

How to know which queries were being made at the time that my server was unavailable?

I have a single site on the server and does not have much movement. There is something causing numerous unnecessary processes and I need to figure out what it is.

Best Answer

This issue, Too Many Connection error occurs when the max_connections variable reaches its maximum threshold value.

Usually, the value for max_connections is defined at the configuration file. By increasing this variable value, this error will resolve.

To see the current value, try the following command:

mysql> show global variables like '%max_connections%';
+-----------------+-------+
| Variable_name   | Value |
+-----------------+-------+
| max_connections | 300   |
+-----------------+-------+
1 row in set (0.02 sec)

When the defined limit reaches, all users will get the error, this is not user specific. However, MySQL allows you to define the user specific values.

After login as root or user with SUPER privilege, you can run the below command to identify the currently processing queries:

mysql> show full processlist;

Note: even if you get Too Many Connections error, you can still login to server, using the root or user with SUPER privilege. MySQL by default will consider maximum allowed connection as max_connection + 1 for SUPER user to fix this issue.

By increasing this variable value, this issue will be resolved.

mysql> SET global max_connections = 1000;

If you wish this changes to be permanent, edit the configuration file (my.ini), and change the value for max_connections variable under [mysqld] section. This approach requires a server restart.

[mysqld]
max_connections = 1000;

Changing this variable value to a higher value in a system with limited resource may lead to performance issues related to memory. Check this page for how to fix MySQL Too Many Connections and other workarounds. I hope this answer will help you.