Processing of MySQL database connection timeout (wait_timeout) issues

Source: Internet
Author: User
Tags connection pooling set time

  Presumably everyone will encounter the connection timeout problem when using MySQL, as shown in:  
 
    is this exception ( Com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:Communications link failure Last packet sent to the server is X Ms ago), because the MySQL service was disconnected after a long period of disconnection, the first request after disconnecting throws the exception. So since it is the connection timeout problem, it is necessary to go to MySQL to explore how the connection time is controlled. Open MySQL console, run: Show variables like '%timeout% ', view and connect time related MySQL system variables, get the following result:  
 
    Where Wait_timeout is responsible for time-out control of the variable, its length is 28800s, that is, 8 hours, then the MySQL service will be 8 hours after the operation interval disconnects, need to re-connect again. There are also users in the URL using Jdbc.url=jdbc:mysql://localhost:3306/nd?autoreconnect=true to make the connection automatically recover, of course, this is possible, but MySQL4 and the following version of the applicable. The MySQL5 has been invalidated and the system variable must be adjusted to control it. MySQL5 Manual for two variables as follows:  
    Interactive_timeout: The number of seconds that the server waits for activity before closing an interactive connection. The interactive client is defined as a client that uses the Client_interactive option in Mysql_real_connect (). See also wait_timeout 
    Wait_timeout: The number of seconds that the server waits for activity before closing a non-interactive connection. When a thread starts, the session Wait_timeout value is initialized based on the global Wait_timeout value or global interactive_timeout value, depending on the client type (the connection option for the Mysql_real_connect () Client_ Interactive definition), see also interactive_timeout 

So it seems that two variables are in common control, so they must be modified. Continue to drill down into these two variables Wait_timeout range is 1-2147483 (Windows), 1-31536000 (Linux), interactive_time values with wait_timeout changes, Their default values are 28800.
The MySQL system variable is controlled by the configuration file, and when not configured in the configuration file, the system uses the default value, which is the default value of 28800. To be modified, it can only be modified in the configuration file. Under Windows under%mysql home%/bin there is a Mysql.ini configuration file, open after adding two variables in the following location, assign values. (modified here to 388000)
Save exit, restart the MySQL service, must restart the system service. You will see the result of the modification:

The configuration file under the Linux system is/etc/my.cnf. The more you need to say is: The configuration file under Windows specifically which needs to find MySQL from Windows system Services, open the properties, see "Executable path" inside the parameter value, Because it may be my.cnf instead of My.ini, this is due to setup at the time of installation, we may ignore it.

At this point, the modification is complete, then the connection timeout problem is resolved from the database. Of course, you can also use connection pooling to set time-outs during programming, and the configuration is relatively straightforward. But modifying the database is a bit more essential.

Reference: http://sarin.iteye.com/blog/580311/

Processing of MySQL database connection timeout (wait_timeout) issues

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.