MySQL Persistent Connections with WordPress

I received an inquiry from another WordPress blogger regarding a WordPress support inquiry I had opened in August. He wrote:

I was searching for answers to a very pesky “Cannot establish connection to database!” WordPress error, and read your thread about database persistent connections. I was wondering if you could give me a hand optimizing the database for WP, I am not a programmer by any extent of the imagination, and know nothing of servers, just got one by chance. (Edited slightly by Ryan.)


Normally PHP opens a database connection when a dynamic page request comes in, uses that connection to build the webpage, and then closes the database connection once the page is finished. This is a lot of extra opening and closing which doesn’t serve any real useful purpose.

The alternative is called persistent connections. Using persistent connections, PHP keeps a pool of a database connections available for the application to use. Those connections are always open, but only in use when a page request comes in. When your request comes in, it grabs a connection from the connection pool, uses it, and then returns the connection when it’s finished. By eliminating the steps of opening and closing the connection, you save a lot of time, and a lot of processing power on the database. The drawback is that a persistent connection takes up a little bit of memory on the database server, even if no one’s ever viewing your website. There are certain situations where this may be a problem.

It’s very easy to enable persistent connections with WordPress. To do so, all you need to do is make a tiny change to the file wp-db.php inside the wp-includes directory. Open that file and search for the function “mysql_connect”. (It’s only used once.) Just change that to “mysql_pconnect” and save your changes. Presto, you’re using persistent connections!

If your site is so busy that it’s overloading the database, you may want to look into page caching instead. Caching lets WordPress build a page once a minute, or once an hour, instead of constantly rebuilding the page for every visitor. This will make your site extremely fast and dramatically cut down on the amount of memory and CPU power you need. The drawback is that new posts and comments won’t appear immediately; it will take a few minutes, depending on the setting for your cache timeout. There’s a great caching plugin for WordPress called WP-Cache which I strongly recommend.

Is anyone else out there using MySQL persistent connections with WordPress? Any tips or tricks?

  • I'm about to give this one a shot, can't see it doing any damage :)
  • Bernd
    I had the same error message. Checking the access with phpMyAdmin I got the more verbose message:
    "User 'wordpress' has exceeded the 'max_connections' resource (current value: 30)."

    First I had suspected the wordpress code to leave out some "db_close()" or similar function. But finally it turned out to be my mistake: I (or some gnome) had really typed "30" into the field "max. connections per hour" for the DB user "wordpress" when he was generated (with phpMyAdmin).
    Changing this to "0" (=> unlimited) solved the problem.
  • which uses the least amount of memory, persistant connections or caching.
  • Hi,

    I have WP Super Cache and Widget Cache enabled on my WordPress site so the site is loading really fast, but my database is overloading and Media Temple has moved me to the burst container twice in the past 4 days.

    Do you know if I can use the persistent connections at along with my page caching? Would that help or make a difference? Or would it not work?

    Thanks!
    P
  • Persistent connections can certainly be used in conjunction with plugins like WP Super Cache. You might want to check with Media Temple first and get their opinion on whether this is a good idea in your situation. But if your site gets a lot of traffic, then I do think it would reduce your database load and improve your database performance. Good luck!
  • Dieter
    Well, maybe a bit late, but i use WP 2.7 and MySQL 5.0XX on a Athlon 1000 mhz with 256 MB RAM and persistent connection BUT without super-cache or another php caching system together with lighttpd as the server. Working in the admin section and listening with Amarok to some nice music, it uses only 175 to 190 MB RAM. With Apache and with or without persistent connection the system would run into permanent swapping, also without persistent connection lighttpd would take swap after some time. I only use it localhost.

    This is a short umlaut test for your discussion system: Über den Wolken grüßen die Engel öfter als äöäßÜÖÄ. Thanks.
blog comments powered by Disqus