Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Terminating idle mysql connections

I see a lot of connections are open and remain idle for a long time, say 5 minutes.

Is there any solution to terminate / close it from server without restarting the mysql service?

I am maintaining a legacy PHP system and can not close the connections those are established to execute the query.

Should I reduce the timeout values in my.cnf file those defaults to 8 hours?

# default 28800 seconds  interactive_timeout=60 wait_timeout=60 
like image 646
shantanuo Avatar asked Nov 26 '10 09:11

shantanuo


People also ask

How do I stop all MySQL connections?

The quickest way to kill all MySQL connections would be to simply restart the MySQL service. This can be done via "Restart Services" in WHM, or via the command-line. It is also possible to kill individual connections for the MySQL client. You can access this client by issuing the MySQL command from a root SSH session.

How do I delete a MySQL connection?

To modify or delete a connection, start MySQL Connections Manager and select an existing connection. You can modify any of the settings by overwriting the existing values with new ones. The connection may be modified or deleted only if no active editor for its objects is opened; otherwise, you may lose your data.

Should you close MySQL connection after every query?

Explicitly closing open connections and freeing result sets is optional. However, it's a good idea to close the connection as soon as the script finishes performing all of its database operations, if it still has a lot of processing to do after getting the results.

How do I keep MySQL connection alive?

To prevent these connections being automatically closed, the connector can be configured to keep the connection alive by submitting a simple SELECT statement (actually SELECT 'KEEP_ALIVE';) periodically to ensure that the MySQL timeout is not reached and the connection closed.


1 Answers

Manual cleanup:

You can KILL the processid.

mysql> show full processlist; +---------+------------+-------------------+------+---------+-------+-------+-----------------------+ | Id      | User       | Host              | db   | Command | Time  | State | Info                  | +---------+------------+-------------------+------+---------+-------+-------+-----------------------+ | 1193777 | TestUser12 | 192.168.1.11:3775 | www  | Sleep   | 25946 |       | NULL                  | +---------+------------+-------------------+------+---------+-------+-------+-----------------------+  mysql> kill 1193777; 

But:

  • the php application might report errors (or the webserver, check the error logs)
  • don't fix what is not broken - if you're not short on connections, just leave them be.

Automatic cleaner service ;)

Or you configure your mysql-server by setting a shorter timeout on wait_timeout and interactive_timeout

mysql> show variables like "%timeout%"; +--------------------------+-------+ | Variable_name            | Value | +--------------------------+-------+ | connect_timeout          | 5     | | delayed_insert_timeout   | 300   | | innodb_lock_wait_timeout | 50    | | interactive_timeout      | 28800 | | net_read_timeout         | 30    | | net_write_timeout        | 60    | | slave_net_timeout        | 3600  | | table_lock_wait_timeout  | 50    | | wait_timeout             | 28800 | +--------------------------+-------+ 9 rows in set (0.00 sec) 

Set with:

set global wait_timeout=3; set global interactive_timeout=3; 

(and also set in your configuration file, for when your server restarts)

But you're treating the symptoms instead of the underlying cause - why are the connections open? If the PHP script finished, shouldn't they close? Make sure your webserver is not using connection pooling...

like image 109
Konerak Avatar answered Oct 02 '22 10:10

Konerak