Using PHP-MySQL Persistent Connections to Run WordPress Blog

When connecting to MySQL database, WordPress weblog based on PHP scripting will use non-persistent connection by default. That is, WordPress will trigger PHP to generate a new connection to MySQL server for each dynamic page request, and subsequently close the database connection once the web page building is completed. Repetitive opening and closing of MySQL connections causes overheads and uses up precious CPU, memory and system resources on the web host.

Using persistent connection to connect to MySQL database server is not always a good option. Each connections takes up resources, including memory, buffers, table or thread caches internally in MySQL and sockets, open files or IO activity at OS level. Thus when a lot of persistent connections are opened but not closed after transactions are completed, the resources on system may drain up and degrade performance of web server, or in worst case, cause HTTP failure. Furthermore, if a persistent connection is stalled, inactive or became defunct or no longer in used, it is not terminated, and MySQL server will only close it to release the resource occupied by the connection after about 28800 seconds (8 hours) by default unless changed the timeout parameters in my.cnf configuration files. This few factors may bring troubles such as MySQL refusing new DB connections or slow server if the applications do not properly handle persistent connections. Thus if you’re not experience webmasters, please do not attempt to change Wordpress to use persistent connections.

However, for bloggers who have busy and large blog, and facing constraints in upgrading web host system specifications, and do not want to use static page caching plugin such as WP-Cache, persistent connection is a available option to tune and optimize the web server to see if the webpage serving can be faster. Persistent connection allows connection to be open once and will not be closed when the execution of the script ends. Instead, the link is kept in pool and will remain open for future use. Thus, using persistent connections will eliminate the process of opening and closing the connection. This will indirectly reduce MySQL connection related server overheads such as authentication to establish connection where only once is needed, saving extra network traffic, and resources used to handle extra connection. Beside, there could be some connection level caches, which need to be filled up for proper performance, and potentially reduce the number of connections to the database.

By default, WordPress is using mysql_connect() function that establishes a new database connection on every page. The persistent connection feature will require mysql_pconnect() function which uses the exact same API, but has built-in connection pooling to keep connections alive between requests. So to switch to use persistent connection to run WordPress, the mysql_connect() function has to be replaced with mysql_pconnect().

To enable and use persistent connections with WordPress, simply login to your web host with SSH (or insecure Telnet), and change to the webroot home directory where WordPress blog is installed. Then change directory into wp-includes directory. Inside the wp-includes directory, there is wp-db.php file. Use any text editor such as vi to edit wp-db.php. Search or locate the function of mysql_connect (or @mysql_connect). There is only one instance of mysql_connect. Just change the mysql_connect to mysql_pconnect, then save and exit the wp-db.php. WordPress and PHP (as long as you don’t turn off the mysql.allow_persistent in PHP.INI) will immediately use persistent connections to connect to MySQL database. It’s worth to try out which setting is best for your environment.


2 Responses to “Using PHP-MySQL Persistent Connections to Run WordPress Blog”

  1. raja
    November 22nd, 2008 10:20
    2

    THANK YOU SO MUCH!
    WordPress is running so much faster now!

    I am running all on Windows Server 2008 with:
    - MySQL Server v5.0 x64 (on dedicated server)
    - IIS7 with FastCGI z32 (on dedicated server)

    Regards,
    raja

  2. Edward
    September 27th, 2007 22:14
    1

    This seemed to do the trick for. Thanks so much for the info!

Leave a Reply

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Subscribe to comments feature has been disabled. To receive notification of latest comments posted, subscribe to My Digital Life Comments RSS feed or register to receive new comments in daily email digest.
Custom Search

New Articles

Incoming Search Terms for the Article

mysql persistent connection - php mysql persistent connection - php mysql wordpress - php mysql persistent connections - persistent connections mysql - php persistent mysql connection - php persistent connections - php mysql persistent - mysql php persistent connection - mysql allow persistent - mysql persistent - Wordpress connection to MySQL - persistent connection php - wordpress mysql timeout - mysql_pconnect wordpress - MySQL persistent connections - php mysql persistant connection - persistent mysql - php mysql keep connection alive - php persistent connection - mysql persistent connect - wordpress persistent mysql - wordpress mysql conn - mysql persistent links - steps to connect on mysql through wordpress - wordpress mysql connection sleep - Reduce or eliminate persistent connections to reduce connection usage - persistent connections - wordpress mysql connection - persistent connection - wp mysql_connect - max connection mysql wordpress - wordpress mysql connections - mysql connection persist php.ini - mysql timeout configuration wordpress - mysql persistent connection performance - mysql connections php - mysql.allow_persistent wordpress - persistent connection mysql - WORDPRESS PHP MYSQL - auth mysql persistant - do I keep my db connect alive php - persistent connections mysql php - wordpress persistant mysql - wordpress my sql - how to enable persistent connect in mysql configuration - persistent connections php mysql - php Article mysql - php 5 persistent mysql connection - what are persistent connections mysql -