Hi.

This is what appears on my site when there:

Warning: mysql_connect(): Too many connections in /home/public_html/ca/sjsu/includes/settings.inc.php(2) : eval()'d code on line 12

Warning: mysql_select_db(): Too many connections in/home/public_html/ca/sjsu/includes/settings.inc.php(2) : eval()'d code on line 16

Warning: mysql_select_db(): A link to the server could not be established in /home/public_html/ca/sjsu/includes/settings.inc.php(2) : eval()'d code on line 16

What's going on? Thanks.

Dominic

    Sounds like you are not closing your MySQL connection after executing all of your queries. The server only allows X amount of simutanious connections and is now refusing until those previous connections are killed by the server, by timeout or administration.

    Always close your connections when your done, didn't your mom tell you to clean up after yourself. 🙂

      4 months later

      Hi,

      I had the same problem and realised that I also did not close the connections 🙂 After a while the problem disappeared and I (thought) have added mysql_close to the code but today I have the same problem again. I checked the code and saw that I typed "mysql_close ($db);" with a space after "close". I have corrected but want to be sure (while waiting the db server comes up again) if that would be the problem of the second db shot down as the code was working as intended.

      Thanks

      As I'm a real rookie at php please find some parts of my code below. It might be that I could not even close the db 😃

      Main php file:

      include("functions.php");

      $db = mysql_connect("localhost", "username", "password");
      mysql_select_db("database", $db);

      ...other code here...

      mysql_close($db); //Now corrected the space error (if it was)

      Functions.php file:
      ...
      function Ucus_Saati ($pilot_adi, $onay){
      $table = "nuke_logbook";
      $result = mysql_query( "SELECT * FROM $table where pn_uname = '$pilot_adi' and pn_approve = '$onay'" );
      while ($row = mysql_fetch_object($result)){
      $ucus_saati = $ucus_saati + $row->pn_flighttime;
      }
      return $ucus_saati;
      }
      ...

        Any comments? Is it possible that "mysql_close ($db);" (space between "close" and "(" ) would not effect the run of the code but not close the connection?

          pekdemir wrote:

          Is it possible that "mysql_close ($db);" (space between "close" and "(" ) would not effect the run of the code but not close the connection?

          No it makes no difference if there's 1 space or 100 spaces or even if the two parts are on seperate lines or even like this

          mysql_close //a comment
          ( $db);

            Thanks for the answer. Can you also confirm that the connection is good to close in the main php file? (I mean I use "mysql_connect" and "mysql_close" in the main php but query the database in "functions.php" using "mysql_query")

              OK, Just in case you guys didn't know, PHP closes all database connections opened by plain connects when the script finishes running.

              HOWEVER, if the script / apache process running the script dies abnormally, then the connection can be left open, in a kind of zombie state. The database thinks there's a client on the other end, while the actual client on the other end went bye bye and the connection won't get reused.

              If this is happening enough to cause you to run out of connections, then something on your apache / php server needs attention. A common cause for this kind of problem is when apache is compiled against one version of a library, and php against another version of the same lib. For instance, it was a common problem back when OpenLDAP sdk and and Netscape's LDAP sdk were fighting it out for superiority for a machine to get set up this way.

              IF this is happening, your apache error / access logs should show some clues.

              Now, if you're using pconnects (which I recommend you do NOT use, generally) then a close_connect will have no effect.

              Note that closing a connection as soon as your done with may help a bit in this situation, as it might be that whatever is causing your php scripts to crash isn't hitting them before they close the connect, but the real problem is that something is making your apache / php child processes die.

              But wait, there's more!

              You can adjust your tcp keep alive parameters to "harvest" those dead connections more quickly. The default tcp keep alive settings are to wait 2 hours to check, then tries to ping it (technically, it's not really a ping, but it's the tcp keep alive equivalent...) 9 times every 75 seconds (in most linux and unix environments.)

              If you're running linux, you can see them as root by typing in :

              sysctl -a|grep keep

              which will show you something like:
              net.ipv4.tcp_keepalive_intvl = 75
              net.ipv4.tcp_keepalive_probes = 9
              net.ipv4.tcp_keepalive_time = 600

              The 600 is the time to wait (so on this machine, it's been reduced from the default of 7200 i.e. 2 hours, to 10 minutes.)
              The 75 is the interval between rechecks once the connection is thought to be possibly dead, and the 9 is the retries.

              To set them on a linux box, edit the /etc/sysctl.conf file and put those lines above we got from the sysctl -a|grep boogie in there and edit them. Then, to put them into force, enter 'sysctl -p' as root.

              So, in your environment, you can drop the keepalive_time to 300 or so, and drop the probes to 3 or 4, and the intvl to 20 or 30. With settings of 300/3/20 for those, you'd have to wait 5 minutes, plus 3*20 seconds, or 6 minutes for dead connections to be reported by the tcp stack back to the database, at which point the database should kill off the connection and it should be available for use again.

              whew. That was longer than I wanted. Hope it helps.

                Don't databases have their own timeout values which can be configured?

                  Yes, they do / can. But quite often they're not set.

                  PostgreSQL has a statement timeout, but no connection timeout (it might now, it didn't used to. I need to keep up with all these new fangle changes...) So, if the connection just poof disappears, and TCP doesn't kick it out of the tree house, the database never notices.

                  OTOH, TCP timeout should kill the children off quite handily, unless the database was written by idiots. And given the recent problem with 5.0.16/17 and php... arg... another story.

                    Sxooter,

                    Thanks for the post. As I mentioned before I'm a real rookie 🙂 so your post is very informative for me. I thought you have to close every connection manually. I'm hosting my site at a shared server so I can't change the settings but I'm and will stay away from pconnect and still closing the connections just in case.

                      Write a Reply...