Mysql optimize the number of connections to prevent excessive access to the method _mysql

Source: Internet
Author: User

Many developers will meet the "Mysql:error 1040:too many Connections" exception, which is caused by a high volume of traffic, MySQL server can not withstand, this time to consider increasing the spread from the server read pressure Another reason is that the max_connections value in the MySQL configuration file is too small .

First, let's look at the maximum number of MySQL connections:

Mysql> Show variables like '%max_connections% ';
+-----------------+-------+
| Variable_name  | Value |
+-----------------+-------+
| max_connections | 151 |
+-----------------+-------+
1 row in Set (0.00 sec)

Second, view the maximum number of connections to the server response:

Mysql> show global status like ' Max_used_connections ';
+----------------------+-------+
| Variable_name    | Value |
+----------------------+-------+
| max_used_connections | 2   |
+----------------------+-------+
1 row in Set (0.00 sec)

You can see that the maximum number of connections to the server response is 2, much lower than the maximum number of connections allowed by the MySQL server.

For the MySQL server maximum connection value setting range is ideal: The maximum connection value of the server response is over 10% of the server upper connection value, if below 10%, the maximum connection of MySQL server is set too high.

Max_used_connections/max_connections * 100% = 2/151 *100%≈1%

We can see that the ratio is far below 10% (because this is a local test server, the result value is not too much reference, you can set the number of connections according to the actual value of the maximum).

Take a look at yourself. Linode VPS Now (time: 2013-11-13 23:40:11) result value:

Mysql> Show variables like '%max_connections% ';
+-----------------+-------+
| Variable_name  | Value |
+-----------------+-------+
| max_connections | 151 |
+-----------------+-------+
1 row in Set (0.19 sec) 
mysql> show global status like ' Max_used_connections '; c12/>+----------------------+-------+
| Variable_name    | Value |
+----------------------+-------+
| max_used_connections |  |
+----------------------+-------+
1 row in Set (0.17 sec)

The maximum number of connections here accounts for about 30% of the upper bound.

We know how to view the maximum number of connections to the MySQL server, and we know how to determine whether the value is reasonable, let's explain how to set the maximum connection value.

Method 1:

Mysql> set GLOBAL max_connections=256; 
Query OK, 0 rows Affected (0.00 sec)
mysql> show variables like '%max_connections% ';
+-----------------+-------+
| Variable_name  | Value |
+-----------------+-------+
| max_connections | 256 |
+-----------------+-------+
1 row in Set (0.00 sec)

Method 2:

Modify the MySQL configuration file my.cnf to add or modify max_connections values in the [mysqld] section:

max_connections=128
Restart the MySQL service.

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.