I just uploaded my current project on a free PHP hosting website and when i and my friend visited the website i got from PDO (MySQL) the error SQLSTATE[08004] [1040] Too many connections.

I use a Singleton class to manage the PDO (so i create only 1 PDO instance to do the queries). I do not close the PDO (set the PDO = null) since i read that the PHP does it by itself at the end of script execution.

I checked the mysql max_connections and it is set to 150. Isn't it a little weird to show this message with only 2 visitors???

Are the max_connections being shared/affected from other websites hosted on the same server (I can't imagine any other cause)?

Any ideas what causes this...

Recommended Answers

All 4 Replies

If you don't close the connection, it will time out after 180 seconds by default (because PHP is basically stateless, a connection is never re-used). Then it is re-added to the connection pool. If you close the connection as you should, this does not happen, and there will be no dangling connections causing this error.

Ok. Lets say this happens. Each page (which has no AJAX) will generate 1 connection (since i use a Singleton class i mentioned in the previous post). I had 150 max_connections according to MySQL variables. After 5-6 pages (i just had uploaded the website and we were only 2 persons viewing it) i got the error. That doesn't make sense. It would be 20 (10 connections from me and 10 from the other person) connections the maximum and still i got the error!!!

Is the number of max_connection individual for each database or is it shared throught the other databases from from other websites hosted on the same server?

It's a per (MySQL) server setting IIRC.

Thx a lot :)

Now i only have to check max_user_connections. Something tells me that they don't give a "good" number of connections per user

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.