Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to setup a connection timeout depending of the user login in MySQL

Tags:

mysql

timeout

I have currently more than 100 connections in Sleep state.

Some connection must stay in Sleep state (and don't close) because it's permanent connection but some others (with a different user name) are from some php script and I want them to timeout very fast.

Is it possible to setup a wait_timeout per user? and if yes, How?

like image 510
acemtp Avatar asked Oct 28 '08 15:10

acemtp


People also ask

What is MySQL default timeout?

MySQL has its wait_timeout variable default value set to 28800 seconds (8 hours). Therefore, if both sides of the connection still keep the defaults, the problem will never happen, as MySQL will never timeout a connection before Stash does it.

How do I change the connection timeout in MySQL workbench?

Can I adjust the timeout? Yes, go to Preferences, SQL Editor, and adjust the DBMS connection read time out option that defaults to 600 seconds. This sets the maximum amount of time (in seconds) that a query can take before MySQL Workbench disconnects from the MySQL server.


2 Answers

There's no per-user timeout configuration, but you can set the wait_timeout value dynamically. That is, after you make a connection as a given user, you can issue a statement to change the timeout value to what you want it to be for that user's session.

Try the following experiment in the mysql command-line client:

mysql> SHOW VARIABLES LIKE 'wait_timeout';

...shows 28800 (i.e. 8 hours), which is the default wait_timout.

mysql> SET SESSION wait_timeout = 60;
mysql> SHOW VARIABLES LIKE 'wait_timeout';

...shows 60.

Then you can quit the session, reconnect, and again the default wait_timeout is 28800. So it's limited to the scope of the current session.

You can also open a second window and start a separate mysql client session, to prove that changing the wait_timeout in one session does not affect other concurrent sessions.

like image 140
Bill Karwin Avatar answered Sep 25 '22 08:09

Bill Karwin


You should set the following variables in your my.conf:

[mysqld]
interactive_timeout=180
wait_timeout=180

wait_timeout is a timeout for automated connections (in my opinion more than 30 on a web server is too much).
interactive_timeout is a console interaction timeout for idle session.

like image 42
Sych Avatar answered Sep 24 '22 08:09

Sych