Question: Can anyone help me to setup connection with multiple databases connection in Drupal 8? I have database on the same server and I want to access it along with default Drupal 8 database.

Answer: 
This is done in the same way as in Drupal 7: You can add the database credentials in your settings.php file.

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

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

```

You will now have two connection options, default and external. You can tell Drupal to use the *external* connection using `\Drupal\Core\Database\Database::setActiveConnection('external')` and switch back to the default one with `\Drupal\Core\Database\Database::setActiveConnection()` once you don't need anymore the *external* connection.
Adding the settings for a new connection is possible by editing the settings.php file used for the site. The successive step is letting Drupal know when it needs to use the new connection, and this depends on which module needs to use it and for what data.

In the case of a custom module which uses procedural code to connect to the new connection (which I assume doesn't contain data accessed from Drupal core or Drupal core modules) for accessing its own database tables, I would use code similar to the following one.

```php
$db = Database::getConnection('default', 'external');
$data = $db->->select('table_name','t')->fields('t')->execute();

```

I use [`Database::getConnection()`](https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Database%21Database.php/function/Database%3A%3AgetConnection/8.9.x) because it allows to use any function/method that could (directly or indirectly) access a database table without first setting the default connection back with [`Database::setActiveConnection()`](https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Database%21Database.php/function/Database%3A%3AsetActiveConnection/8.9.x).  

There is still the risk that Drupal core invokes a hook when the database is queried (for example, [`hook_query_TAG_alter()`](https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Database%21database.api.php/function/hook_query_alter/8.9.x)), and that hook implementation assumes the database used for the connection contains all the Drupal data.

Instead of relying on the connection being defined in the settings.php file, the module could add the connection information with code similar to the following one.

```php
$connection = [
  'database' => 'database',
  'username' => 'user',
  'password' => 'password',
  'host' => 'localhost',
  'port' => '3306',
  'driver' => 'mysql',
  'prefix' => '',
  'collation' => 'utf8mb4_general_ci'
];

Database::addConnectionInfo('external', 'default', $connection);

```

Note that the order of the first two parameters used by `Database::getConnection()` is different from the order of the first two parameters used by [`Database::addConnectionInfo()`](https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Database%21Database.php/function/Database%3A%3AaddConnectionInfo/8.9.x): For `Database::addConnectionInfo()`, `$key` is the first parameter.

For a module that uses the external database to store its own entities, the module should define its own storage handler (extending [`ContentEntityStorageBase`](https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Entity%21ContentEntityStorageBase.php/class/ContentEntityStorageBase/8.9.x)), possibly passing to its constructor the key and the target to use for the database connection. In this way, it can use a different connection for its entity data and the field data associated to its entity.