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: 
In addition to retrieving a database connection to the external database using [`Database::getConnection()`](https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Database%21Database.php/function/Database%3A%3AgetConnection/8.1.x), you can also use [dependency injection](https://www.drupal.org/node/2133171) in your code to retrieve the connection *as a dependency* and declare your connection in a module's services YAML file:

```
database.external:
  class: Drupal\Core\Database\Connection
  factory: 'Drupal\Core\Database\Database::getConnection'
  arguments: ['external']

external_database_dependent_service:
  class: Drupal\MODULE\Some\Class\For\A\ServiceUsingTheExternalDatabase
  arguments: ['@database.external']

```
**Update on this topic for the most recent version of Drupal 8 at this time (8.9.13)**:

As mentioned by @acrosman in a comment, you do need to provide two parameters, and not in the order that you might expect. In settings.php, the format is like:

```
$databases['default']['default'] = array(
  'database' => 'database',
  'username' => 'user',
  'password' => 'pass',
  'host' => 'localhost',
  'port' => '3306',
  'driver' => 'mysql',
  'prefix' => '',
  'collation' => 'utf8mb4_general_ci'
);

```

So if you've added a second database like:

```
$databases['external']['default'] = array(
  'database' => 'database',
  'username' => 'user',
  'password' => 'pass',
  'host' => 'localhost',
  'port' => '3306',
  'driver' => 'mysql',
  'prefix' => '',
  'collation' => 'utf8mb4_general_ci'
);

```

And you want to get a connection to the second database:

```
$connExternal = \Drupal\Core\Database\Database::getConnection('default', 'external');
$results = $connExternal->select('users', 'u')

```

... and so on until ->execute() and fetching the data.

Note that the keys are in reverse order. This is also not set globally with ::getConnection(), so subsequent DB queries will revert to the previous default database.