We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 6699
    • 11 Posts
    Hi everyone,

    On trying to log in to the manager, I get what appears to be the following error:

    Warning: mysql_connect(): Too many open links in /site-folder/dbapi.mysql.class.inc.php on line 85.

    This comes in the ununsual format of the largest pop-up dialog I’ve ever seen (attached).

    Unfortunately this means I’m unable to log into the Manager at all!

    Any ideas?

    This is a recently completed an installation of MODx 0.9.5 on my production server (shared host). Installation was smooth apart from having to make some source-code modifications to allow sessions to be saved in a sub-directory of the site folder.

    Many thanks,

    David
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      that means that your host’s mysql database engine is overloaded. It is configured to allow a certain number of connections at any one time, and if too many sites are hitting the database at one time you’ll get left out in the cold with that error message.
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 6699
        • 11 Posts
        Thanks for the reply, but any idea why it’s only happening upon trying to log in to the Manager? Should it not happen on accessing any part of the site (i.e. whenever data is retrieved from the database)?
          • 4041
          • 788 Posts
          Since the front site is most likely cached, it doesnt require calls to the database under most circumstances as the parser first checks the cache and then the database if necessary. (I’m sure someone will correct me if i am wrong wink )
            xforum
            http://frsbuilders.net (under construction) forum for evolution
            • 6699
            • 11 Posts
            Thanks Breezer. That does make sense, but I assume there must be some database activity when a page is loaded. Also, if I use an incorrect username/password combination to log in to the manager the site behaves as expected and generates the "Incorrect username/password..." dialog. This would imply that the database has been successfully read to retrieve the credentials wouldn’t it?

            Update: I’ve spoken with my host, and they suggest that because they limit the number of connections to "about 20" per site I need to ensure my code uses [tt]mysql_close();[/tt] to close any connections I make to the database. I assume that this would be happening anyway, but does anyone know of a reason why there would be a large number of connections open prior to line 85 of [tt]dbapi.mysql.class.inc.php[/tt] being called?

            I believe this is the code that’s falling over:

               /**
                * @name:  connect
                *	 
                */
               function connect($host = '', $dbase = '', $uid = '', $pwd = '', $persist = 0) {
                  global $modx;
                  $uid = $uid ? $uid : $this->config['user'];
                  $pwd = $pwd ? $pwd : $this->config['pass'];
                  $host = $host ? $host : $this->config['host'];
                  $dbase = $dbase ? $dbase : $this->config['dbase'];
                  $tstart = $modx->getMicroTime();
                  if (!$this->conn = ($persist ? mysql_pconnect($host, $uid, $pwd) : mysql_connect($host, $uid, $pwd, true))) {
                     $modx->messageQuit("Failed to create the database connection!");
                     exit;
                  } else {
                     $dbase = str_replace('`', '', $dbase); // remove the `` chars
                     if (!@ mysql_select_db($dbase)) {
                        $modx->messageQuit("Failed to select the database '" . $dbase . "'!");
                        exit;
                     }
                     $tend = $modx->getMicroTime();
                     $totaltime = $tend - $tstart;
                     if ($modx->dumpSQL) {
                        $modx->queryCode .= "<fieldset style='text-align:left'><legend>Database connection</legend>" . sprintf("Database connection was created in %2.4f s", $totaltime) . "</fieldset><br />";
                     }
                     $this->isConnected = true;
                     // FIXME (Fixed by line below):
                     // this->queryTime = this->queryTime + $totaltime;
                     $modx->queryTime += $totaltime;
                  }
               }
            


            Line 85 reads:

            [tt]if (!$this->conn = ($persist ? mysql_pconnect($host, $uid, $pwd) : mysql_connect($host, $uid, $pwd, true))) {[/tt]

            Any help in resolving this would be greatly appreciated as I’m totally stuck! huh
              • 6699
              • 11 Posts
              Umm...

              OK.

              So I’ve modified line 78 in \manager\includes\extenders\dbapi.mysql.class.inc.php from:

              [tt]function connect($host = ’’, $dbase = ’’, $uid = ’’, $pwd = ’’, $persist = 0) {[/tt]

              to

              [tt]function connect($host = ’’, $dbase = ’’, $uid = ’’, $pwd = ’’, $persist = 1) {[/tt]

              and this has finally allowed me to log in to the Manager.

              Does anyone know of any negative impact this modification may have had on site functionality?

              Cheers,

              David
                • 22303 MODX Staff
                • 10,725 Posts
                Read here: http://www.php.net/manual/en/features.persistent-connections.php

                In general, persistent connections create and leave open a lot more connections, because non-persistent connections are closed after the script executing them stops executing, but this will completely depend on the configuration of your environment, especially how the database is setup in relation to PHP.

                The advice about calling mysql_close(), from my understanding, would not have any effect, because those connections are supposed to be closed when the PHP process is done with it, regardless if it is closed explicitly.

                Also, from the notes on the persistent connections page; might be useful to you or your provider, as I bet this is the problem:
                To those using MySQL and finding a lot of leftover sleeping processes, take a look at MySQL’s wait_timeout directive. By default it is set to 8 hours, but almost any decent production server will have been lowered to the 60 second range. Even on my testing server, I was having problems with too many connections from leftover persistent connections.
                  • 6699
                  • 11 Posts
                  Thanks for the advice Jason - much appreciated.

                  I’ll discuss that with my host - for the moment I don’t seem to be running into any problems using persistent connections (my understanding is that PHP will attempt to re-use any open connection before creating a new one, so perhaps this helps).

                  In the meantime I’m enjoying delving into MODx!

                  smiley