Use multiple / external databases in Drupal 8 | Drupal 8

Use multiple / external databases in Drupal 8

Submitted by editor on Wed, 06/29/2016 - 11:40
Question

How to connect to multiple databases within Drupal 8 ?

How to use multiple databases with Drupal 8 ?

The database settings (and connection) has 3 levels.

1. KEY
2. TARGET
3. DATA

Example:

$databases[KEY][TARGET] = array (
 DATA
);

Like:

$databases['default']['default'] = array (
  'database' => 'drupal8_default_db',
  ....
);

 

Methode 1. Multiple connections Using Different "KEY" (Recommended)

1. Configure your drupal instance for multiple databases (setting.php)

'key' is the first level of the database connection;

Example :

$databases['default']['default'] = array (
  'database' => 'drupal8_default',
  'username' => 'root',
  'password' => 'root',
  'prefix' => '',
  'host' => 'localhost',
  'port' => '3306',
  'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',
  'driver' => 'mysql',
);

$databases['myseconddb']['default'] = array (
  'database' => 'mysql',
  'username' => 'root',
  'password' => 'root',
  'prefix' => '',
  'host' => 'localhost',
  'port' => '3306',
  'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',
  'driver' => 'mysql',
);

2. Connect to the correct data base:

Example:

//To get the default database
$con = \Drupal\Core\Database\Database::getConnection();
//To get another database (here : 'myseconddb')
$con = \Drupal\Core\Database\Database::getConnection('default','myseconddb');

 

Methode 2. Multiple connections Using Different "TARGETS" (Not recommended, Use for master/slave configuration)

1. Configure your drupal instance for multiple databases (setting.php)

'target' is the second level of the database connection;

Example :

$databases['default']['default'] = array (
  'database' => 'drupal8_default',
  'username' => 'root',
  'password' => 'root',
  'prefix' => '',
  'host' => 'localhost',
  'port' => '3306',
  'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',
  'driver' => 'mysql',
);

$databases['default']['myseconddb'] = array (
  'database' => 'mysql',
  'username' => 'root',
  'password' => 'root',
  'prefix' => '',
  'host' => 'localhost',
  'port' => '3306',
  'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',
  'driver' => 'mysql',
);

2. Connect to the correct data base:

Example:

//To get the default database
$con = \Drupal\Core\Database\Database::getConnection();
//To get another database (here : 'myseconddb')
$con = \Drupal\Core\Database\Database::getConnection('myseconddb');

 

And See also How to use database programmatically

 

Database configuration :
https://www.drupal.org/docs/7/api/database-api/database-configuration
Master / Slave Configuration :
https://www.drupal.org/node/1952990

Comments

Angelo (not verified)

Tue, 02/21/2017 - 10:57

i already configured my drupal instance for multiple databases (settings.php), i want to show contents from my external database in a view but i don't know how to do it? could you please give me a hand?

im using the last version of drupal 8.26. i know is not to much but i could offer you a 10 euros gift card.

Blessings!

Hello,

I was able to get data from external database, refer to http://www.drupal8.ovh/en/comment/144#comment-144.
But it's not inside of view, just top of header, I mind echo the result inside of page. How to passed the result of external db to inside of view. How about view_hooks_data?

Thanks

ugy (not verified)

Wed, 03/22/2017 - 07:08

Hello, thank's for the article.

Is this way possible for different host? I want to get some data from online database then show them into my site on localhost . On item host I change with "localhost" with IP from my online database.

Thank's.

Priya (not verified)

Fri, 04/21/2017 - 12:00

Hi,

I have setup a multisite in Drupal 8  having one master site and 2 child sites, one child site have same db with prefix and 2nd child site having external db. How can I use only users and users_role tables from db of master site in slave site with external db.

Arlie Tosi (not verified)

Fri, 06/16/2017 - 03:14

There are not any memory issues regarding gadget what so
ever. It has an internal 256 MB which are extended via microSD,
as many as 32GB. Camera is of 2 MP in addition to a photo of resolution as high as 1600x1200 pixels can be captured. http://tunaniece11.beep.com/secrets-your-parents-never-told-you-about-mobile-insurance-2017-06-13.htm?nocache=1497321231

Anonymous (not verified)

Thu, 10/12/2017 - 13:01

Hello
In fact I use drupal 8 with the view database connector but I did the connection of my second database. Here is my configuration
$ databases ['default'] ['default'] = array (
   'database' => 'bd_drupal8',
   'username' => 'root',
   'password' => '',
   'prefix' => '',
   'host' => 'localhost',
   'port' => '3306',
   'namespace' => 'Drupal \\ Core \\ Database \\ Driver \\ mysql',
   'driver' => 'mysql',
);
$ databases ['external'] ['default'] = array (
   'database' => 'student',
   'username' => 'root',
   'password' => '',
   'prefix' => 'etu_',
   'host' => 'localhost',
   'port' => '3306',
   'namespace' => 'Drupal \\ Core \\ Database \\ Driver \\ mysql',
   'driver' => 'mysql',
);
But my problem is that I can see my tables from my second database 'ma_base' when I want to create a view. Thank you for pointing out the problem if there is either to help me to make a good configuration.

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.