Dual DB Schema Setup with Drupal
How to configure a second database schema in Drupal for separating transactional data from content.
Anyone that has worked on an enterprise level Drupal application knows that having the transactional consumer data separate from your content will save you time and headaches during a release.
The first thing that you will need to do is edit your “settings.php” file for your Drupal installation and add the following array block to configure your second schema:
$databases['transactional_db'] = array('default' => array(
'driver' => 'mysql',
'database' => 'Name_of_your_Database',
'username' => 'Database_username',
'password' => 'some-password',
'host' => 'mysql-server-host-name',
'port' => 3360
));
Next step, is to copy the Drupal Database API functions that you need into your own custom module and prefix the function names with a prefix of your liking. You will also need to add two additional lines to each function to switch between databases:
function transactional_db_select($table, $alias = NULL, array $options = array()) {
db_set_active(variable_get('secondary_db_schema', 'default'));
$result = Database::getConnection($options['target'])->select($table, $alias, $options);
db_set_active('default');
return $result;
}
You might also consider looking at “Master” and “Slave” configuration if this does not fit your needs.