Solution to the Too connector connections error in mysql 1040

Source: Internet
Author: User

From the official documentation, we know that the default mysql connections compiled and installed on linux are 100, which is far from enough for website requirements.
Mysql officially told us that we need to modify the value of max_connections. How can we modify it? There are two methods

1. modify the configuration file

Modify/etc/my. the cnf file is added to max_connections = N in [mysqld]. If you do not have this file, copy the file you need from the support-files folder in the compilation source code *. the cnf file is/etc/my. cnf. I'm using a my-medium.cnf with medium-sized server configuration. For example, the content of my [mysqld] is as follows:Copy codeThe Code is as follows: [mysqld]
Port = 3306
Socket =/tmp/mysql. sock
Key_buffer = 160 M
Max_allowed_packet = 1 M
Table_cache = 64
Sort_buffer_size = 512 K
Net_buffer_length = 8 K
Read_buffer_size = 256 K
Read_rnd_buffer_size = 512 K
Myisam_sort_buffer_size = 8 M
Max_connections = 1000

Because I am not familiar with mysql, many parameters have not been modified. Haha ..

2. users who do not use the mysqld script to start automatically.

Modify the $ MYSQL_HOME/bin/mysqld_safe File
Example:/usr/local/mysql/bin/mysqld_safe File
Grep-n 'max _ connection' $ MYSQL_HOME/bin/mysqld_safe
Modify the max_connections parameter value of the corresponding row number.
The above method is written based on the practices on the Internet.

Method 2:

Find information about mysql_connect and mysql_pconnect in the PHP Manual. The following describes the two functions in the php manual:
Mysql_connect function prototype:
Resource mysql_connect ([string server [, string username [, string password [, bool new_link [, int client_flags])
Return Value:
If the connection succeeds, a MySQL connection ID is returned. If the connection fails, FALSE is returned.
Mysql_connect () establishes a connection to the MySQL server. If no optional parameter is provided, use the following default values: server = 'localhost: 100', username =
Username of the server process owner, password = empty password.
If the same parameter is used to call mysql_connect () for the second time, a new connection is not established, but an opened connection ID is returned. The new_link parameter changes this behavior and causes
Mysql_connect () always opens new connections, even when mysql_connect () was previously called with the same parameters. The client_flags parameter can be a combination of the following constants:
Note: The new_link parameter is available in PHP 4.2.0.
The client_flags parameter is available in PHP 4.3.0.
Once the script ends, the connection to the server will be closed. Unless mysql_close () has been called before to close it.
Function prototype:
Resource mysql_pconnect ([string server [, string username [, string password [, int client_flags])
Return Value:
If the connection succeeds, a positive MySQL persistent connection identifier is returned. If an error occurs, FALSE is returned.
Mysql_pconnect () establishes a connection to the MySQL server. If no optional parameter is provided, the following default value is used: server = 'localhost: 100 ',
Username = username of the server process owner, password = empty password. The client_flags parameter can be a combination of the following constants: MYSQL_CLIENT_COMPRESS,
The server parameter can also include the port number, for example, "hostname: port", or the path of the local socket, for example, ":/path/to/socket ".
Note: The support for ": port" is added for version 3.0B4.
The support for ":/path/to/socket" is added in version 3.0.10. Differences between the two:
Mysql_pconnect () and mysql_connect () are very similar, but there are two main differences.
First, when connecting, this function will first try to find a (persistent) connection that has been opened with the same user name and password on the same host. If it is found, the connection ID is returned without a new connection.
Second, after the script is executed, the connection to the SQL server will not be closed. This connection will be enabled for future use (mysql_close () will not close the connection established by mysql_pconnect ).
The client_flags parameter is available in PHP 4.3.0 and later versions. This connection is called "persistent ".

To make sure that your system does not encounter the Too connector connections error, note the following two points:
1. Ensure that the maximum number of processes in apache does not exceed the maximum number of connections in mysql;
2. Do not use too many mysql_pconnect connections to the same database server in the Program (one is enough). This requires good coding habits and specifications. In particular, it is constantly added to the system.
If the new functions do not focus on the system architecture and coding specifications, the system becomes unable to maintain when the complexity of the system reaches a certain level. it is very troublesome to solve the problem.
The solution is to modify/etc/mysql/my. cnf and add the following line:
Set-variable = max_connections = 500
Or add the parameter max_connections = 500 to the startup command.
Is to modify the maximum number of connections, and then restart mysql. The default number of connections is 100, which is too small, so it is prone to errors such as the question

The above is a supplement from Song lixing's column below:

1. It may be a problem with mysql's max connections settings.
2. It may be multiple insert operations. The update operation does not close the session. You need to configure transaction support in spring.

1. Reduce the time-out time of the session in tomcat to 20 (not required)
2. Provides transaction support for database insert or update operations with large processing capacity.

The following is a solution:
Com. mysql. jdbc. exceptions. jdbc4.MySQLNonTransientConnectionException: Data source rejected establishment of connection, message from server: "Too has connections"


The number of connections is full because the number of concurrent connections set in my. ini in your mysql installation directory is too small or the system is busy.


Open the MYSQL installation directory, open MY. INI, and find max_connections (in approximately 93rd rows). The default value is 100, which is generally set to 500 ~ 1000 is more suitable. Restart mysql and solve the 1040 error.
Max_connections = 1000

MYSQL must be restarted to take effect.


Net stop mysql

Net start mysql

Mysql cannot be started after innodb_log_file_size is changed.

Innodb_buffer_pool_size = 768 M
Innodb_log_file_size = 256 M
Innodb_log_buffer_size = 8 M
Innodb_additional_mem_pool_size = 4 M
Innodb_flush_log_at_trx_commit = 0
Innodb_thread_concurrency = 20
The above is a preliminary optimization of the innodb engine. It was found that there was a problem when innodb_log_file_size = 256M was updated. If this is added, the engine cannot be started,

Later, I realized that I had to STOP the service first, and then deleted the original file .........
Open/MySQL Server 5.5/data

Delete ib_logfile0, ib_logfile1 ...... ib_logfilen
Enable the option again and start successfully.

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: 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.