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.


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

  1. Out
    September 23rd, 2009 14:37
    3

    Thanks a lot.I really do not know about the function mysql_pconnect().Will going to implement it practically soon.

  2. 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

  3. 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

wordpress persistent connections - mysql persistent connections - wordpress php mysql - wordpress mysql connection - mysql persistent connection - wordpress persistent connection - wordpress connection pool - how to php to mysql connection login to open the nextpage - php persistent connection - mysql persistent links - php persistent mysql connection - wordpress change database connection - mysql connection pooling wordpress - php mysql persistent connection or not - wordpress connect mysql - db connection in wordpress - mysql.allow persistent, is it needed - php disable local persistent connect - wordpress mu persistent connection - fastcgi persistent mysql php - php connect MySQL - php wordpress connexion mysql - run triggers php - wordpress mysql connectionstring - persistent connections wordpress - persistent connection in mysql - php persistent connections how long open? - php mysql persist example - php connection persistence - php connection to mysql fast - reduce mysql requests using php - how to run wordpress on my pc - keep connection on IIS 7 persistent - mysql what is a persistent connection - MySql Pooling in php - mysql persistent connections - mysql_connect slow on "windows 7" - persistent connection - php mysql connection pooling - wordpress mysql database php - wordpress mysql connection info php file - CLOSING CONNECTIONS WORDPRESS MYSQL - connect wordpress to mysql - objectif connection persistent mysql - speed mysql_connect - wordpress mysql connection conflict - wordpres php connect - wordpress mysql connections - wordpress persistent mysql - wordpress mysql_pconnect() -