I'm having trouble getting Drupal to switch between multiple databases. I have a system that involves connecting to an external database to retrieve data. I am using the following code inside of a form at build time to get some information for displaying in the form.

$connExternal = \Drupal\Core\Database\Database::getConnection('default', 'atavism');
$results = $connExternal->select('world');
$data = $results->query();

$result = $data->fetchAll();


However, when I visit the form, Drupal redirects me back to the install page, where it tells me that drupal is already installed. I have tried using setActiveConnection() as well, but the same thing keeps happening. I am not sure how to resolve it, and access the secondary database for retrieving the information I need.

Inside of my settings.php, I have this:

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

Am I missing something?

  • You have to set it back after using. drupal.stackexchange.com/questions/167569/…
    – Kevin
    May 10, 2021 at 15:58
  • According to that article, using getConnection allows you to get the database without changing the global state. With that being said, I also call getConnection() at the bottom to the same effect.
    – Ex0r
    May 10, 2021 at 22:50
  • Still trying to resolve this. No matter what I do, Drupal continues to redirect me to the install complete page.
    – Ex0r
    May 13, 2021 at 16:14


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.