MySQL Optimized connection count

Source: Internet
Author: User

Many developers will meet the "Mysql:error 1040:too many connections" anomalies, one of the reasons for this situation is that the traffic is too high , the MySQL server can not resist, this time to consider increasing the decentralized reading pressure from the server 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 connections for MySQL:

?
1234567 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 that the server responds to:

?
1234567 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, which is much lower than the maximum allowable connection value for the MySQL server.

For MySQL server maximum connection value of the set range is ideal: The server response to the maximum connection value of the server is higher than the number of connection values above 10%, if under 10%, the MySQL server maximum connection limit value is set too high.

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

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

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

?
1234567891011121314 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‘;+----------------------+-------+| Variable_name    | Value |+----------------------+-------+| Max_used_connections | 44  |+----------------------+-------+1 row in set (0.17 sec)

The maximum number of connections here is about 30% of the total number of connections.

Above we know how to view the maximum number of MySQL server connection, and know how to determine whether the value is reasonable, let us explain how to set the maximum value of the connection.

Method 1:

?
123456789 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, add or modify the Max_connections value in the [Mysqld] segment:

max_connections=128
Restart the MySQL service.

View the number of current connections
Show status;
threads_connected Current number of connections
threads_created the number of connections created

MySQL Optimized connection count

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.