MySQL optimization: number of connections

Source: Internet
Author: User

Sometimes we meet the "Mysql:error 1040:too many connections" anomaly. One reason is that the amount of access is too high for mysqlserver to withstand. This time you will want to consider adding decentralized read pressure from the server. Another reason is that the max_connections value in the MySQL configuration file is too small.

First of all. Let's look at the maximum number of connections for MySQL:

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

Secondly. To view the maximum number of connections for 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)

The maximum number of connections that can be seen for the server response is 2, which is much lower than the maximum agreed value for MySQLServer.

The range of settings for the MySQLServer maximum connection value is ideal: The maximum connection value for the server response is more than 10% of the server upper connection value, assuming under 10%, the MySQLServer maximum connection upper limit value is set too high.

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 large for reference, we 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:

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 |    |+----------------------+-------+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 connection value of MySQLServer. and know how to infer whether the value is reasonable. Let's look at how to set this maximum connection value below.

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 |   |+-----------------+-------+1 row in Set (0.00 sec)

Method 2:

Change the MySQL configuration file my.cnf. Add or change the max_connections value in the [Mysqld] segment:

max_connections=128

Restarting the MySQL service is possible.



MySQL optimization: number of connections

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.