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

salomon shoes
huarache shoes
salomon speedcross 4
adidas originals nmd
nike shoes
moncler jacket
yeezy boost 350 v2
pandora jewelry
pandora
yeezy shoes
fitflop
nike air max 2018
polo
adidas
adidas
louboutin
supreme shirt
Salomon Shoes
ed hardy
ecco outlet
mcm backpack
polo ralph lauren
adidas nmd r1
ray ban prescription sunglasses
kate spade
uggs for women
pandora jewelry
nike air max 90
Hermes Birkin Bags
air jordan
oakley glasses
balenciaga shoes
skechers uk
asics mens running shoes
mont blanc outlet
kd 10
nike outlet
chrome hearts jewelry
true religion jeans for women
prada
burberry scarf
nike air max 270 men
cartier love ring
ralph lauren
under armour outlet
yeezy boost 350 v2
mcm wallet
moncler jacket mens
ray ban eyeglasses
jimmy choo trainers
yeezy boost 350
Pandora Jewelry
burberry bags
burberry shirt
ultra boost mens
fitflops clearance
pandora charms
stephen curry shoes
nike vapor max
pandora bracelet
ugg slippers
north face coats
true religion jeans
salvatore
adidas nmd r1
pandora rings
kd shoes
chrome hearts bracelet
huaraches
yeezy boost
skechers women
pandora
kyire 3
kyrie irving
longchamp
nike air max
nike kd
jordan shoes
goyard
rihanna fenty puma
Yeezy Boost 350
chrome hearts ring
air max 95 mens
kate spade handbags
kate spade bags
jordan 11
adidas nmd r1
mulberry sale
basketball shoes
new balance golf shoes
moncler coat
north face jacket
swarovski rings
coach purses
harden shoes
lacoste sale
oakley eyeglasses
christian louboutin
fitflop shoes
air max 97 womens
adidas yeezy
air max 95
curry shoes
ray ban sunglasses
nike air max
pandora uk
nike air max
uggs for women
nike air max 95
nike air max 97
celine wallet
swarovski jewelry
ugg slippers
louboutin
michael kors handbags
mbt womens shoes
nike air max 2018
christian louboutin shoes
moncler jacket
jordan shoes
pandora uk
vapormax
coach purses
goyard tote
nike air max 270
lebron
pandora jewelry
michael kors
adidas ultra boost
jordan shoes
valentino sandals
moncler
supreme shirt
nike air max
nike air max 97
fitflop
ray ban sunglasses
birkenstock
jordan
barbour outlet
asics womens running shoes
birkenstock shoes
mulberry
nike air max 90
skechers women
yeezy boost 350
salomon skis
balenciaga bags
prada bags
polo ralph lauren
ferragamo shoes men
bvlgari
longchamp bag
moncler,moncler jacket
nike air max
pandora jewelry
hermes belt women
barbour outlet

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.

pandora charms
coach bags
salomon
uggs for men
kate spade outlet
moncler coat
kate spade wallet
lebron 14
jordan retro 11
nike vapormax
chrome hearts ring
oakley eyeglasses
air jordan
yeezy shoes
air max 90
mk handbags
fitflop sale
nike air max
pandora jewelry
burberry wallet
adidas originals nmd
asics shoes
nike air vapormax flyknit
jordan 11
pandora jewelry
nike air max 90
moncler jacket womens
air max 97
ultra boost mens
nike air huarache
uggs on sale
moncler
Hermes Handbags
bvlgari ring
curry 4 shoes
ferragamo shoes
salvatore ferragamo
skechers shoes
nike air max 270
ray ban eyeglasses
jordan retro 11
lebron shoes
north face jacket
kyrie irving shoes
adidas yeezy
burberry bags
asics
oakley sunglasses
coach outlet
louboutin shoes
mont blanc outlet
pandora jewelry
curry shoes
longchamp bag
pandora bracelet
rihanna puma shoes
pandora charms
celine purse
skechers women
moncler jacket
moncler jacket
barbour coats
yeezy boost 350
prada purse
goyard
nike air force 1
true religion jeans men
nike air max 90
balenciaga sneakers
barbour jacket
kd shoes
nike air max 270 men
nike air max 95
longchamp bag
christian louboutin
ray ban sunglasses
salomon
air max
Salomon Speedcross 3
fitflop
ultra boost
Pandora
ugg slippers
yeezy boost 350 v2
air max 95
uggs for women
adidas yeezy
new balance golf shoes
ralph lauren polo shirts
nike air vapormax
jimmy choo heels
moncler coat
swarovski pen
adidas shoes
ecco boots
christian louboutin shoes
salomon xa pro 3d
burberry bags
adidas
supreme shirt
ray ban eyeglasses
prada shoes
skechers
fitflop
supreme jacket
polo
pandora earrings
chrome hearts ring
birkenstock
kd shoes
valentino sneakers
air jordan
james harden
nmd
ultra boost
cartier sunglasses
mbt
jordan retro
pandora charm
pandora,pandora charms
mulberry purse
air max
fitflop sale
true religion jeans men
mulberry bayswater
christian louboutin
under armour outlet
air max 95
swarovski
michael kors bags
mcm
polo ralph lauren
chrome hearts clothing
air max 95
ed hardy clothing
hermes belt men
pandora rings
kd 9
kate spade bags
birkenstock sale
goyard
balenciaga shoes
adidas nmd r1
mcm
nike air max 95
huarache
womens north face jacket
air max 97
ralph lauren shirts
yeezy boost 350
Adidas Yeezy
lacoste polo shirts
yeezy shoes
ray ban
kyrie irving
air max 90
pandora
air max 97

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.

Anonymous (not verified)

Mon, 10/22/2018 - 09:41

Add new comment