Home  >  Article  >  Database  >  MySQL database error: Too many connections solution

MySQL database error: Too many connections solution

黄舟
黄舟Original
2017-09-11 11:29:222833browse

This kind of error is obviously caused by forgetting mysql_close after mysql_connect;
When a large number of connections are made, the error of Too many connections will occur. The default connection number of mysql is 100. Under what circumstances will this error occur? ?
After normal mysql_connect, call mysql_close() to close the connection
But when there is a connection error, when the mysql_real_query() exits with an error, you may forget mysql_close();
So you must determine whether before the program returns close(), the safest way is to have only one exit when writing any function!
You can also increase the number of allowed connections by modifying the mysql configuration file!
Sometimes your server often encounters errors like this:
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.

From the official documentation, we know that the default connection number of mysql compiled and installed on Linux is 100
Document:

http://dev.mysql.com/doc/refman/5.0/en/too-many-connections.html

Mysql official tells us that we need to modify the value of max_connections, so how do we modify it? There are two methods
1. Modify the configuration file
Modify the file /etc/my.cnf and add max_connections=N in [mysqld]. If you do not have this file, please compile the source code from support- Copy the *.cnf file you need in the files folder to /etc/my.cnf. I am using my-medium.cnf, medium server configuration. For example, the content of my [mysqld] is 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

Since I am not very familiar with mysql, many parameters have not been modified. Ha ha. .
2. Users who do not use the mysqld script to automatically start.
Modify the $MYSQL_HOME/bin/mysqld_safe file
For example:

/usr/local/mysql/bin/mysqld_safe这个文件
grep -n ‘max_connection’ $MYSQL_HOME/bin/mysqld_safe

Modify the max_connections parameter value of the corresponding line number

The above is the detailed content of MySQL database error: Too many connections solution. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn