Skip to main content
Drupal 8

Main navigation

  • Home
    • Question And Answer
    • NEWS
    • Projects
  • Tutorials - Drupal
    • Android
    • Other
User account menu
  • Log in

Breadcrumb

  1. Home

Use multiple / external databases in Drupal 8

By editor , 29 June, 2016
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

Tags

  • Drupal 8
  • Code
  • Database
  • Add new comment
Database Basic Examples (Select, Merge, Insert ...)
Database LIKE condition
Database Merge, Insert or Update if exist.
Drupal Mysql / Database Data types

Comments9

Plain text

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

Angelo (not verified)

9 years 9 months ago

i already configured my drupal instance for multiple databases..

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!

  • Reply

editor

9 years 9 months ago

In reply to i already configured my drupal instance for multiple databases.. by Angelo (not verified)

Use hook_views_data

Hello, Fore that, you can use hook_views_data. you have a tutorial here.

  • Reply

ugy (not verified)

9 years 8 months ago

In reply to Use hook_views_data by editor

Passing result of external db to view

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

  • Reply

ugy (not verified)

9 years 8 months ago

Possible with different host?

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.

  • Reply

editor

9 years 8 months ago

In reply to Possible with different host? by ugy (not verified)

Yes, Just set the host

Hi,

You can use this method with different host, table, database (mysql, mariadb, sqlite ....)

Thanks

  • Reply

ugy (not verified)

9 years 8 months ago

In reply to Yes, Just set the host by editor

Wow, its great!.. thanks.

Wow, its great!.. thanks.

  • Reply

Priya (not verified)

9 years 7 months ago

How can I share user table of master site into child sites

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.

  • Reply

Arlie Tosi (not verified)

9 years 5 months ago

Use multiple / external databases in Drupal 8 | Drupal 8

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
  • Reply

Anonymous (not verified)

9 years 1 month ago

Hello…

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.

  • Reply

Links

  • Drupal.org
  • Drupal core release cycle
  • Drupal events with ease
  • Unit test with Drupal using PHPUnit (Drupal 10)
  • Tokens for file upload url
  • Tips for Docker
  • CKEditor inline image files usage for custom forms.
  • Install Docker on Linux Mint
  • Install PHP 8.1
  • Drupal Security Best Practices
  • Order and sort items with tabledrag
  • Add a custom service YAML container
  • Apply a patch with composer