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.
Related Articles
- How to Move WordPress Blog to New Domain or Location
- WordPress MySQL SQL Query Error in WPDB Class
- Moderate WordPress Blog Comments Via Moderator with Desktop Client
- How to Customize, Modify or Change WordPress Database Connection Error Page
- Replace Google Blog Search with Technorati for Incoming Links in Dashboard of WordPress
- WLWManifest Disabler Plugin to Remove XLM Link in WordPress Blog Header
- AdSense and Display Banner Ads Rotation in WordPress Blog or Websites
- Integrate and Display Google AdSense for Search and Co-Op Custom Search Engine Results in WordPress Blog Page Template
- Change and Reset MySQL root Password
- Paginating or Split WordPress Post or Page with NextPage in WordPress Not Working










































November 22nd, 2008 10:20
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
September 27th, 2007 22:14
This seemed to do the trick for. Thanks so much for the info!