MySQL timeout mechanism

Source: Internet
Author: User

Each time MySQL creates a socket connection (connect), the socket takes up a certain amount of memory. Even when you close the (close) connection, it is not really closed, but it is in the sleep state.

The next time you connect, you can quickly start a socket that is currently asleep. But too many sockets take up a lot of memory, and to solve this problem, MySQL has a timeout mechanism.

You can use this statement to see the time-out length of the current setting:

Show global variables like ' wait_timeout ';

The results are as follows:

+---------------+-------+
| variable_name | Value |
+---------------+-------+
| Wait_timeout | 28800 |
+---------------+-------+
1 row in Set (0.00 sec)

The default is 28,800 seconds, which means 8 hours.

The function of the Wait_timeout parameter: When an idle connection exceeds 8 hours, the connection is disconnected.

But sometimes it takes a long time to open the MySQL connection, and you need to change the value of this parameter. Select the appropriate parameter values according to the machine's hardware.

There are two ways to change the value of this parameter:

1. Change the value of this parameter through the statement. This method does not require a restart of MySQL, but will disconnect all current connections and then reconnect.

Statement: Set Global wait_timeout = 100; Execution Result:

ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect ...
Connection Id:3
Current database: * * * NONE * * *

Query OK, 0 rows affected (1.13 sec)

The last line indicates that the change was successful.

Note: This method is local in nature. The next time you restart the MySQL server, execute show global variables like ' wait_timeout ', when it is still the value before the modification.

2. By modifying the configuration file

In the MySQL installation directory, there is a Mysql.ini file

Add the following settings:

[MYSQLLD]

Wait_timeout = 100

Interactive_timeout = 100

Then restart the MySQL service and the changes will take effect.

Note: The meaning of this parameter:

Wait_timeout:mysql number of seconds to wait for activity before the server closes a non-interactive connection

The number of seconds the Interactive_timeout:mysql server waits for activity before closing an interactive connection

MySQL timeout mechanism

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.