In this tuto, I'll show you how to use multiple databases in Drupal 8.
The database settings and connection has 3 levels.
- KEY
- TARGET
- DATA
$databases[KEY][TARGET] = array (
DATA
);
Example:
$databases['default']['default'] = array (
'database' => 'drupal_db',
'username' => 'drupal_db',
'password' => 'drupal_db',
'prefix' => '',
'host' => 'localhost',
'port' => '3306',
'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',
'driver' => 'mysql',
);
Methode 1: Multiple connections Using Different KEY
This is the recommended way.
1. Configure your drupal instance for multiple databases in 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['second']['default'] = array (
'database' => 'drupal8_second',
'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 key "default"
$con = \Drupal\Core\Database\Database::getConnection();
//To get another database (here : 'second')
$con = \Drupal\Core\Database\Database::getConnection('default','second');
Methode 2. Multiple connections Using Different TARGET
This way is not recommended.
1. Configure your drupal instance for multiple databases in 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']['second'] = array (
'database' => 'drupal8_second',
'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 : 'second')
$con = \Drupal\Core\Database\Database::getConnection('second');
also to set the active connection, you can use this:
$conn = \Drupal\Core\Database\Database::setActiveConnection('second');
How to use multiple databases to move some tables from main database into another database.
// use prefix to move cache tables into another database
$databases['default']['default'] = array (
'database' => 'drupal_db',
'username' => 'drupal_db',
'password' => 'drupal_db',
'prefix' => array(
'cache_bootstrap' => 'drupal_db_cache.',
'cache_config' => 'drupal_db_cache.',
'cache_container' => 'drupal_db_cache.',
'cache_data' => 'drupal_db_cache.',
'cache_default' => 'drupal_db_cache.',
'cache_discovery' => 'drupal_db_cache.',
'cache_dynamic_page_cache' => 'drupal_db_cache.',
'cache_entity' => 'drupal_db_cache.',
'cache_menu' => 'drupal_db_cache.',
'cache_render' => 'drupal_db_cache.',
'cache_toolbar' => 'drupal_db_cache.',
),
'host' => 'localhost',
'port' => '3306',
'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',
'driver' => 'mysql',
);
//Configure cache database
$databases['cache']['default'] = array(
'database' => 'drupal_db_cache',
'username' => 'drupal_db_cache',
'password' => 'drupal_db_cache',
'host' => 'localhost',
'port' => '3306',
'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',
'driver' => 'mysql',
);