Reputation: 972
I want to deploy openpublish on about 5 drupal multisite by high traffic on website and want to use multiple database server but some tables should be shared. How to connect to multiple databases within Drupal by making share on 'users', 'sessions' and 'role' tables.
Upvotes: 0
Views: 4245
Reputation: 1226
Sum times i wanted to retrieve data from other database in drupal then, i created a function in selected theme folder inside template.php file
connect_to_database($un, $pass, $db, $insert)
it takes four parameters of the database username, password, databasename, and last is query
function connect_to_database($username,$password,$database,$query){
$database_info = array(
'host' => 'hosname',
'database' => $database,
'username' => $username,
'password' => $password,
'driver' => 'mysql'
);
Database::addConnectionInfo('coreapp', 'default', $database_info);
db_set_active('newcon');
$q = db_query($query);
db_set_active('default');
return $q;
}
Upvotes: 0
Reputation: 2007
The documentation on sharing tables says that you have to use a single database to share tables, but I've done it with multiple databases on the same server. I've done this by putting the database name before the prefix then adding a dot. So if your default database is called 'drupal', and your second database is called 'second_drupal', the prefixes would look like this in settings.php:
$db_prefix = array(
"default" => "slave1_", // the prefix for tables that are not shared.
"users" => "second_drupal.master_",
...
(Note that it doesn't matter what you call your default database, since it's the default database, you don't need to refer to it by name in the $db_prefix
variable, as long as it's setup correctly in the settings.php file.)
http://thedrupalblog.com/setting-multi-site-drupal-6-installation-shared-databases-and-single-sign has some instructions, but I think that still assumes a single database server.
Upvotes: 1
Reputation: 14568
To allow multiple database connections, convert $db_url
to an array.
<?php
$db_url['default'] = 'mysql://drupal:drupal@localhost/drupal';
$db_url['mydb'] = 'mysql://user:pwd@localhost/anotherdb';
$db_url['db3'] = 'mysql://user:pwd@localhost/yetanotherdb';
?>
To query a different database, simply set it as active by referencing the key name.
<?php
db_set_active('mydb');
db_query('SELECT * FROM table_in_anotherdb');
//Switch back to the default connection when finished.
db_set_active('default');
?>
But make sure all databases are of same kind.
Upvotes: 3