Using an Additional Database in Drupal 7

Aug
16
7

One of the nice, lesser-known features of Drupal 7 is the ability to use additional databases and switch at ease. This might be useful for external databases, or if you have another database in an alternative format – perhaps you have an SQLite database that for performance reasons, you don’t wish to migrate. (Yes, Drupal 7 now supports SQLite!)

The configuration can be a little confusing at first, so let’s look at a settings.php file set up to use two databases: drupal on Localhost, and db2 situated at db.example.com.

$databases = array (
  'default' => 
  array (
    'default' => 
    array (
      'database' => 'drupal',
      'username' => 'username',
      'password' => 'password',
      'host' => 'localhost',
      'port' => '',
      'driver' => 'mysql',
      'prefix' => '',
    ),
  ),
  'external' => 
  array (
    'default' => 
    array (
      'database' => 'db1',
      'username' => 'username2',
      'password' => 'password2',
      'host' => 'db.example.com',
      'port' => '',
      'driver' => 'mysql',
      'prefix' => '',
    ),
  ),
);

The file is fairly self-explanatory – the default database is called drupal and the second database is keyed external (i.e., the connection key). The confusion might lie in the nested default.

In addition to allowing additional, separate databases, Drupal now allows master/slave configurations. In the example above the second level default is the target, and refers to the master server – as does the second. If you examine the code it can become quite easy to confuse whether default refers to the default database, the master database or the additional, master database. (perhaps master would have been a better term?) just remember, connection key followed by target.

Switching databases in your code is simple:

db_set_active('external');

Any subsequent database operations (for example, db_select) will now take place on your second database.

There’s a caveat – you must switch back to the default database when you’ve done. Otherwise you’ll get tell-tale error; a “Base table or view not found”, usually something like one of Drupal’s cache tables, or commonly the block table – either way, the error message will tell you it’s your database as the table names will be prefixed with the schema in the error message.

Switching back to the default database is easy; no key is required as the default is assumed, thus:

db_set_active();

Hope this helps.

Posted

August 16th, 2011

Category

Drupal

Comments

7 comments

 

7 comments

#1
tabernario wrote 6 days 21 hours ago:

Thank you. I've spent a lot of time trying to conect to a civicrm database.

Perfect explained.

Thank you.

#2
Anonymous wrote 1 week 5 days ago:

Hi
I have small issue coming when i was migrate drupal 6 to drupal 7 site..
My drupal 6 site in one server and drupal 7 site in other server...
Now how can i migrate sites .....................
plzzzzzzzzzzzzzzzzzzzzzz replay me
If any possible to do the issue...

#3
Mark wrote 39 weeks 20 hours ago:

There's a pretty big gotcha with switching databases though - you need to make sure you don't call any functions which rely on your default database while you're switched to the external database. t() is a particularly easy one to miss, and theme functions can give all kinds of strange errors. I wrote more about this at http://www.sparrowtail.com/dangers-multiple-drupal-database-connections.

#4
Cesar wrote 40 weeks 4 days ago:

Thak you so much, you are my hero.

#5
kc6ape wrote 1 year 3 weeks ago:

Lukas,
Thanks so much for posting this, could you possibly help a newbie out and show exactly how this whole string would look when added to my settings.php file? I am confused as to where the connection key goes and the statement to switch back to default would go.
Thank You Again

#6
Allison wrote 1 year 17 weeks ago:

Thank you. I am new to drupal 7 (used drupal 6 at my last job, but never set up anything from scratch). This solved the problem I was having.

#7
Anonymous wrote 1 year 19 weeks ago:

may is know how to use multiple database to share user table ?

Add your comment

The content of this field is kept private and will not be shown publicly.

About Me

Lukas White, LAMP Developer Manchester

I'm a web developer based in Manchester, England. I also design websites.  I generally favour open-source technologies such as PHP, MySQL and JQuery, and I have extensive experience using Drupal.

Read more »

Recent Comments

1 day 3 hours ago Anonymous commented on Gift Certificates in Drupal Commerce:

I am using the steps which you are given...
1) created a line item type "Gift Certificate...

Read more...
6 days 21 hours ago tabernario commented on Using an Additional Database in Drupal 7:

Thank you. I've spent a lot of time trying to conect to a civicrm database.

Perfect...

Read more...
1 week 5 days ago Anonymous commented on Using an Additional Database in Drupal 7:

Hi
I have small issue coming when i was migrate drupal 6 to drupal 7 site..
My...

Read more...