notes on configuring hyperdb

HyperDB is a plugin for WordPress that enhances the shipping database connection class. My main interest is its support for connections to MySQL master and slave servers.

The scenario here involves a pair of web servers that both point at a common NAS share that WordPress has been installed onto. To establish some semblance of high availability we created a master/slave MySQL database infrastructure that WordPress could reach from either web server.

HyperDB ships with 2 files that are not automatically installed upon upload. In fact this is one plugin that can probably be deployed more easily by downloading (or uploading) via a terminal session on the web server.

db-config.php contains the configuration for the plugin and belongs in the root of the directory WordPress is installed in (e.g. /nas_p/html/wordpress).

db.php is the actual database management script that needs to be installed to the wp-content directory (e.g. /nas_p/html/wordpress/wp-content).

Here is a really simple master and slave setup that starts at line 217 of the shipping db-config.php file (all other parameters are set to the defaults):

$wpdb->add_database(array(
	'host'     => 'host1.example.com',
	'user'     => DB_USER,
	'password' => DB_PASSWORD,
	'name'     => DB_NAME,
));

/**
 * This adds the same server again, only this time it is configured as a slave.
 * The last three parameters are set to the defaults but are shown for clarity.
 */
$wpdb->add_database(array(
	'host'     => 'localhost',
	'user'     => DB_USER,
	'password' => DB_PASSWORD,
	'name'     => DB_NAME,
	'write'    => 0,
	'read'     => 1,
	'dataset'  => 'global',
	'timeout'  => 0.2,
));

Note that my master and slave MySQL databases each have a user named “wpadmin” with the same password. This user has been granted full rights over the WordPress database. There are three entries and sets of privileges for the user: one for “wpadmin@localhost”, and the others as “wpadmin@host1.example.com” and “wpadmin@host2.example.com”*. This allows the wpadmin user from either server to connect to the database on the other. Also note that I have specified the FDQN for the master server and ‘localhost’ for the slave (both servers are mapped in each others /etc/hosts just in case DNS goes down/becomes unavailable). In each case the host name is enclosed in single quotes.

Many thanks to alfred for his most excellent article on this subject, How to: Deploying WordPress over multiple servers for load balancing. It really cut through all the noise in the official (and unofficial) doc out there and set me on the right path to getting it done. God, I love the Internet!

*You could use “wpadmin@%.example.com”, allowing connections from any host in the .example.com domain, but unless you’re limiting network access to the servers (using firewalls), it would be a less secure choice.

This entry was posted in System Administration on by .

About phil

My name is Phil Lembo. In my day job I’m an enterprise IT architect for a leading distribution and services company. The rest of my time I try to maintain a semi-normal family life in the suburbs of Raleigh, NC. E-mail me at philipATlembobrothersDOTcom. The opinions expressed here are entirely my own and not those of my employers, past, present or future (except where I quote others, who will need to accept responsibility for their own rants).