2

I have recently had a problem with our website. Users/visitors were being denied a connection to the database. When I tried to log on via PHPMyAdmin and it came back with this message: "#1203 - User already has more than 'max_user_connections' active connections."

It is a shared host, and the maximum connections allowed is 20, apparently. I cant change this, but the site doesnt usually have more than 20 on at any one time, and the error showed up for about an hour or two.

When I asked the host what the problem is, all the sent back was this: "We are afraid this has something to do with poor scripts and was filling all of your allotted connections to the database (20). You need to check the scripts and data assets you are trying to return".

What does this mean exactly?

This is a typical piece of code I am using:

session_start();
require 'include.php';
mysql_connect($DBhost,$DBuser,$DBpass);
mysql_select_db("$DBName");

I know the information here doesnt give much in the way of specific scripts, but I am wondering if there is a problem with the connection not being closed? Any one with any ideas would be of great help and I could look at this. Is there any coding errors that can cause this? As shouldnt the connection automatically close after the page has loaded?

Thank you in advance

SW_Cali
  • 383
  • 2
  • 17
  • What is most likely happening is that the connection is not being freed when the PHP code has run. Could you give an example of a full page script? – James Culshaw Mar 06 '13 at 10:13
  • Reading the accepted answer to this will give you some background info: [MySQL Error “Too many connections”](http://stackoverflow.com/questions/1202322/mysql-error-too-many-connections) – nickhar Mar 06 '13 at 10:17
  • Hi James, thanks for your reply. I have been running through the pages most likely to have been accessed when the error came up. The log in page was one, and on checking this, the script connects at the beginning of the page, then half way down it calls "mysql_connect($DBhost,$DBuser,$DBpass); mysql_select_db("$DBName");" again, for some reason. This is the only page I have found that does this. Could this have been the problem? – SW_Cali Mar 06 '13 at 10:23
  • Hi nickhar. Thanks, I had seen that page when looking last night. Very good & detailed answer. This bit was helpful: "If I remember correctly, max_connections does not limit the number of connections you can open to the MySQL Server, but the total number of connections that can be opened to that server, by anyone connecting to it." as it gave me some insight as to what the problem could be. – SW_Cali Mar 06 '13 at 10:26
  • Just an update, I have removed the double-ups of the mysql_connect(), but this issue has just happened again. I cant seem to find any way around it. At the time there was just one person viewing the website. – SW_Cali Mar 06 '13 at 12:32

1 Answers1

0

To find out how many current connections there are, you can click on STATUS and then the SERVER tab under Runtime Information in PHPMYADMIN. Or run:

mysqladmin -uroot -ppassword processlist (from command line)

Tell your host you want to limit connections from localhost only. I suspect you are getting probed from the Internet. The processlist command will tell you if that's the case.

micah94
  • 414
  • 4
  • 9
  • If you can't find STATUS, you might have a link called 'Processes', that should do the same thing. You can also run 'SHOW PROCESSLIST' in an Sql Query Window. – Bjorn Mar 16 '14 at 22:11