Mysql Error: Solution to Too many connections

  • 2021-12-05 07:42:16
  • OfStack

MySQL database Too many connections

This error obviously means forgetting mysql_close after mysql_connect;

After a large number of connect, the error of Too many connections will occur, and the default connection of mysql is 100. Under what circumstances will this error occur?

Call mysql_close () after normal mysql_connect to close the connection

However, when the connection is wrong, mysql_close () may be forgotten when the participant mysql_real_query () exits with an error;

So before the program return 1 must judge whether close (), the safest way is to write any function when there is only 1 exit!

You can also increase the number of connections allowed by modifying the mysql configuration file!

Sometimes your server often has such errors:

The error message is as follows:

Can not connect to MySQL server

Error: Too many connections

Errno.: 1040

Similar error report has beed dispatched to administrator before.

According to the official documents, the default connection of mysql compiled and installed on Linux is 100

Document: http://dev.mysql.com/doc/refman/5. 0/en/too-many-connections. html

mysql officially told us that we need to modify the value of max_connections, so how can we modify it? There are two ways

1. Modify the configuration file

Modify the file /etc/my. cnf to add max_connections=N in [mysqld]. If you do not have this file, please copy the required *. cnf file to/etc/my. cnf from the support-files folder in the compile source code. I'm using my-medium. cnf, a medium server configuration. For example, my [mysqld] reads as follows


[mysqld]

port = 3306

socket = /tmp/mysql.sock

skip-locking

key_buffer = 160M

max_allowed_packet = 1M

table_cache = 64

sort_buffer_size = 512K

net_buffer_length = 8K

read_buffer_size = 256K

read_rnd_buffer_size = 512K

myisam_sort_buffer_size = 8M

max_connections=1000

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

2. Users who do not use mysqld script to start automatically.

Modify the MYSQLHOME/bin/mysqldsafe file for example:/usr/local/mysql/bin/mysqldsafe This file grep n 'maxconnection'

MYSQLHOME/bin/mysqldsafe files such as:/usr/local/mysql/bin/mysqldsafe This file grep n 'maxconnection' MYSQL_HOME/bin/mysqld_safe

Modify the max_connections parameter value for the corresponding line number

3. Server login mysql: mysql-u root-p

910% can't get in, if you can't get in, execute the restart command:/etc/init. d/mysql restart (centos system)

At this time, you can connect mysql by restarting mysql. If there is still time, you can continue to the next step to cure the disease

Open the configuration file and add 1 to configure vi/etc/my. cnf


wait_timeout = 600

interactive_timeout = 600

Restart mysql again

Principle solution

The default number of mysql is 100. If it exceeds, it can't be connected. The actual number of connections is far from 100, and most of them are in sleep

Therefore, either increase the number of connections or kill useless connections, and the latter is recommended.

Summarize


Related articles: