I'm trying to make my WordPress database a bit more resilient, with MySQL master/slave replication. To go along with that, I've installed HyperDB, with the master as a read/write database, and the slave as read-only.
As long as both master and slave database servers are running, everything is alright. The Web server sends queries to both master and slave databases. But as soon as I shut down the master database server, my sites become non-responsive. I expected the sites to still function, just not to be able to post new articles and such.
Do I have unrealistic expectations of the HyperDB plugin, or is there something I might have misconfigured?
Below is the relevant section of db-config.php. It's not just straight $wpdb->add_database() calls because a little bit of logic is needed to work out the "correct" database server. For testing, I tried replacing all of it with simple add_database() calls and had the same issue.
// Returns wpdb-style array for use with add_database() copying settings from wp-config
function wshdb_get_template_db($host, $readpref = 1, $writepref = 1) {
$db_tmpl = array(
'host' => $host,
'server' => '', // only here to suppress a PHP error
'dataset' => 'global',
'lag_threshold' => NULL,
'user' => DB_USER,
'password' => DB_PASSWORD,
'name' => DB_NAME,
'timeout' => 0.2,
'read' => $readpref,
'write' => $writepref
);
return $db_tmpl;
}
// Populate the wpdb array based on where we're running
$tmp_dbhost = explode(".", strtolower(gethostname()));
$host = $tmp_dbhost[0];
switch($host) {
// WP4 stage
case 'webserver1':
case 'webserver2':
$wpdb->add_database(wshdb_get_template_db('masterdb'));
$wpdb->add_database(wshdb_get_template_db('slavedb', 1, 0));
break;
// a bunch of other cases omitted
default:
$wpdb->add_database(wshdb_get_template_db('localhost'));
break;
}