I am trying to set up multiple databases in order to split up the read and write queries between two databases, a Read-Only Database and a Write Database.
I set up the multiple databases in settings.php:
$db_url['default'] = 'mysql://username:password@localhost/databasename';
$db_url['readonly'] = 'mysql://username:password@localhost/databasename';
And the only way I could figure out how to do this without having to go through every module and manually set the database with 'db_set_active()' is to go into the core file '/includes/database.mysql-common.inc' and modify the 'db_query' function:
function db_query($query) {
$args = func_get_args();
array_shift($args);
$query = db_prefix_tables($query);
if (isset($args[0]) and is_array($args[0])) { // 'All arguments in one array' syntax
$args = $args[0];
}
_db_query_callback($args, TRUE);
$query = preg_replace_callback(DB_QUERY_REGEXP, '_db_query_callback', $query);
/* Added by apersaud
Integrating multiple read/write databases
*/
if(strpos(strtolower($query),"select") === 0){
db_set_active('readonly'); //this will not contain any data from the master (write) database untill replication happens
}
else {
db_set_active('default');
}
return _db_query($query);
db_set_active('default');
}
However, it's not working correctly. Whenever I try to add new content (e.g. Story), I get an error message "The post could not be saved". The post is still created, but it's corrupted and I can't open it, and also the only row in the 'node_access' table is deleted somehow (which I think is causing some of the issues). But, I can't figure out what's causing that row to be deleted.
Also, in the log entries I get this error:
Duplicate entry '0' for key 2 query: INSERT INTO node (vid, type, language, title, uid, status, created, changed, comment, promote, moderate, sticky, tnid, translate) VALUES (0, 'story', '', 'Test 9', 6, 0, 1242671928, 1242671928, 0, 0, 0, 0, 0, 0) in .../includes/common.inc on line 3422.
Any suggestions?
Comments
Comment #1
apersaud commentedOk, I found a patch that Drupal is using to split read and write queries in a MySQL master/slave replication environment (for Drupal 6.x):
http://sourceforge.net/projects/msrwd/
Will it do what I'm trying to achieve?
Comment #2
apersaud commentedI applied the patch, and I am still having the same issues (except the row in node_access table is not being deleted anymore).
I still get an error message "Post could not be saved" whenever I create new content and the post is not saved. Nor can I edit any content - the changes aren't saved.
Any ideas anyone?
Comment #3
sezer commentedhello apersaud,
could you manage to do it?
Comment #4
kars-t commentedHi
I am closing this issue to clean up the issue queue. Feel free to reopen the issue if there is new information and the problem still resides. If not please make sure you close your issues that you don't need any more.
Maybe you can get support from the local user group. Please take a look at this list at groups.drupal.org.