Mysql 1040 error Too connector connections

Source: Internet
Author: User

 

Is there a warning like this on your server?

The information is as follows:

 

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:

[Mysqld]

Port = 3306

Socket =/tmp/mysql. sock

Skip-locking

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.

Description:

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:

: MYSQL_CLIENT_COMPRESS, MYSQL_CLIENT_IGNORE_SPACE, or MYSQL_CLIENT_INTERACTIVE.

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.

Mysql_pconnect:

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.

Description:

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,

MYSQL_CLIENT_IGNORE_SPACE or MYSQL_CLIENT_INTERACTIVE.

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

 

From song lixing's column

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.