Easy solution: too many mysql database connections _ MySQL

Source: Internet
Author: User
Q: How can I solve the problem of too many mysql connections? A: The system cannot connect to the database. The key is to have two data items: 1. maximum number of connections allowed by the database system max_connections. This parameter can be set. If this parameter is not set, the default value is 100. The maximum value is 16384. 2. the current number of connection threads in the database is threads_connected. This is a dynamic change. Query

Q:How can I solve the problem of too many mysql connections?

A:The system cannot connect to the database. The key lies in two data points:

1. maximum number of connections allowed by the database system max_connections. This parameter can be set. If this parameter is not set, the default value is 100. The maximum value is 16384.

2. the current number of connection threads in the database is threads_connected. This is a dynamic change.

For more information about how to view max_connections and max_connections, see.

If threads_connected = max_connections, the database system cannot provide more connections. in this case, if the program wants to create a new connection thread, the database system will reject the connection, if the program does not handle too many errors, an error message similar to the strong altar will appear.

Because the creation and destruction of database connections consume system resources. To avoid opening too many connection threads at the same time, the so-called database connection pool technology is usually used for programming.

However, the database connection pool technology does not prevent connection resources from being exhausted due to program errors.

This usually happens when the program fails to release the database connection resources in time or other reasons cause the database connection resources to be unable to be released. However, it is estimated that this low-level programming error will not occur in the strong altar system.

The easy way to check this error is to constantly monitor the changes of threads_connected when refreshing the strong altar page. If max_connections is large enough, and the value of threads_connected is constantly increasing to reach max_connections, check the program. Of course, if you use the database connection pool technology, threads_connected will not grow to the maximum number of connection threads in the database connection pool.

From the perspective of a strong altar error, it is more likely that the database system fails to be properly configured. The following are some suggestions. For reference

Ask your engineers to change the maximum number of connections allowed for MySQL from 100 to 32000 by default. This will not cause too many connections.

View max_connections

Go to MySQL and run the following command:

     show variables

View the variable value of the maximum number of connections in the database:

     max_connections

View threads_connected

Go to MySQL and run the following command:

     show status

View the value of the currently active connection thread variable:

     threads_connected

Set max_connections

The setting method is to add the last red line below in the my. cnf file:

     [mysqld] port=3306 #socket=MySQL skip-locking set-variable = key_buffer=16K set-variable = max_allowed_packet=1M set-variable = thread_stack=64K set-variable = table_cache=4 set-variable = sort_buffer=64K set-variable = net_buffer_length=2K set-variable = max_connections=32000

After modification, restart MySQL. Of course, to ensure correct settings, check max_connections.

Note:

1. although 32000 is written here. However, the actual MySQL server allows a maximum of 16384 connections;

2. in addition to max_connections, the above configurations should be configured according to your system's own needs;

3. added the maximum number of allowed connections, which does not increase the system consumption much.

4. if your mysql instance uses my. ini as the configuration file, the settings are similar, but the settings must be slightly changed.

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.