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 directory to the webroot home directory where WordPress blog is installed.
Locate and edit the wp-config.php file, and add in the following lines (or change to the following values if the constants already existed):
define('USE_PCONNECT', true); define('WP_CACHE', false);
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.
The following steps are also workable, but involves hacking the WordPress core file, and thus not recommended, and left in this article for reference only.
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.