Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

My website has been showing an error related to database.

The error says- Warning:mysql_connect()[function.mysql-connect]:Too many connections in /home/host1/public_html/ on line 2.Could not connect error.Too many connections.

I get the error occasionally, but when i refresh the page say after 20 seconds the error disappears and page appear normally. I tried to rectify this by putting a code to close the sql connection after the page finishes loading. But even after that i got this error. Can you tell we what could possibly be wrong. Is it a server issue or is it related to my code?

share|improve this question
What is your max connections set to? – Brad Mar 4 '11 at 15:51

Couple of things to check:

Are you using mysql_pconnect to connect to the database? This will make a persistent connection that remains open once it is established and will re-use that connection instead of creating a new db connection each time.

Also check max_connections and max_user_connections in the my.cnf file as you may be reaching a limit set in those two variables.

share|improve this answer
Thanks Shaun for the reply..I am not using the persistent connection. So there is no chance of an error through the first case. And i am using shared hosting account and not a dedicated server.So do i have the privilege to access the my.cnf file in shared hosting service... – user73092 Mar 8 '11 at 14:24

If you run this query:


You will see what all connections are currently up, and what they are doing. This should give you a better idea as to what is holding things up. Over time, connections can get stuck, and it is possible they can sit there for a long time. More likely though, you have a hacked script (or poorly written script causing a lot of locks).

share|improve this answer
Thanks for the reply Brad...I tried the command just after opening multiple tabs.Result was time:0 state:null. Also i am using connection close command at end of page...Hope that that works and the error dont appear again...About the script i don't think i am using too complex scripts on my page. My Site is – user73092 Mar 8 '11 at 14:42
You ran SHOW PROCESSLIST;? You should have been given a list of running queries... – Brad Mar 8 '11 at 15:02

You are probably not closing the connection in your php script. Using mysql_close(), which explicitly closes the connections. The use of mysql_close() in conjunction with mysql_connect() would ensure that the connection is closed.

share|improve this answer
The connection is always closed at the end of the script, automatically. While I agree, it is good practice to explicitly call mysql_close(), I doubt that is the issue here. – Brad Mar 4 '11 at 15:49
Thanks Jonathan for the reply.I closed the connections in all pages.I have read that, by default the connection closes when the page load is complete. But still i gave a connection close statement at the end...of all pages....The problem is that the error occurance is for only few seconds 20-30 seconds.after that i refresh the page...its back to normal... – user73092 Mar 8 '11 at 14:34

If you successfully close connections after querys are finished then try upping max_connections in my.cnf.

MySQL administrator in their GUI-tools package might give you an idea as to what is happening.

share|improve this answer
I dont think i have the privilege to access the my.cnf file with the shared hosting account i am using.I get about 200 hits/day. But i dont think that isn't something a server from hostgator cant handle...I contacted hostgator live chat. they say that they cant trouble shoot the error unless they see it live.My prob error goes after 20-40 sec after it appear...By the time i contact them and gets a analyst the error disappears... :-(. I show them screen shot and error text....Still no help.... :-( – user73092 Mar 8 '11 at 14:38

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.