Modifying the database connections within Drupal

The Acquia include statement (sometimes called the require statement) sets up configuration details for all of the databases within the environment, such as development or production.

Common use cases for modifying the database connection info include adding a third-party (non-Acquia) database, or editing the MySQL init conditions.

Configured databases

You can view the details of all configured databases using Drush:

Drupal 8

drush ev "print_r(\Drupal\Core\Database\Database::getConnectionInfo());"

Drupal 7

drush ev 'print_r($GLOBALS["databases"]);'

Drupal 6

drush ev 'print_r($GLOBALS["db_url"]);'

Adding a third-party database

If the database you want to connect to is not present in the environment, then it is not configured by default. You can add the connection details in your settings.php file.

In the following example, you'll see a conf variable, which is set before the require line to tell it not to autoconnect to the database. The function that makes the connection is called after the extra database details have been configured. As an alternative, it's possible to force the database API to reparse the database connection information after the $databases array has been amended, but it's typically more efficient to follow the example provided here.

The syntax for creating this connection is a little different depending on the Drupal version.

Drupal 8

if (file_exists('/var/www/site-php')) {
  global $conf;
  global $databases;
  $conf['acquia_hosting_settings_autoconnect'] = FALSE;
  require '/var/www/site-php/[sitename]/[database]-settings.inc';
  $databases['extra_db'] = array (
    'default' => array (
      'driver' => 'mysql',
      'database' => 'dbname',
      'username' => 'username',
      'password' => 'password',
      'host' => 'host',
      'prefix' => '',
      'port' => 3306,
    ),); 
  acquia_hosting_db_choose_active();
  unset($GLOBALS['conf']);
  unset($GLOBALS['databases']);
}

 

Drupal 7 


if (file_exists('/var/www/site-php')) {
  $conf['acquia_hosting_settings_autoconnect'] = FALSE;
  require('/var/www/site-php/[sitename]/[database]-settings.inc');
  $databases['extra_db'] = array (
    'default' => array (
      'driver' => 'mysql',
      'database' => 'dbname',
      'username' => 'username',
      'password' => 'password',
      'host' => 'host',
      'prefix' => '',
      'port' => 3306,
    ),);
  acquia_hosting_db_choose_active();
}

Drupal 6


if (file_exists('/var/www/site-php')) {
  require('/var/www/site-php/[sitename]/[sitename]-settings.inc');
  $db_url['extra_db'] = 'mysql://username:[email protected]:port/databasename';
}

You can check your additions to the database configuration using the Drush commands detailed previously.

Editing the database init commands

The same approach can be used to manipulate certain init_commands options. The following example extends Acquia's default wait_timeout value, which can be useful in situations such as large site migrations.

Drupal 8

if (file_exists('/var/www/site-php')) {
  global $conf;
  global $databases;
  $conf['acquia_hosting_settings_autoconnect'] = FALSE;
  require '/var/www/site-php/[sitename]/[database]-settings.inc';
  $databases['default']['default']['init_commands'] = [ 'SET SESSION wait_timeout = 2147483' ]; 
  acquia_hosting_db_choose_active();
  unset($GLOBALS['conf']);
  unset($GLOBALS['databases']);
}
 

Verifying connections with queries

You can also use Drush to run test queries to verify that the connection to the third-party database is working. In these queries, we will be retrieving the number of entries from the node table, using SELECT COUNT(nid) FROM node.

Drupal 8


$ drush ev 'print_r(\Drupal\Core\Database\Database::getConnection("default", "default")->query("SELECT COUNT(nid) FROM node;")->fetchObject());'
stdClass Object
(
    [COUNT(nid)] => 100
)

Or against your alternative database (extra_db):


$ drush ev 'print_r(\Drupal\Core\Database\Database::getConnection("default", "extra_db")->query("SELECT COUNT(nid) FROM node;")->fetchObject());'
stdClass Object
(
    [COUNT(nid)] => 0)
)

Drupal 7

The default connection:


$ drush ev 'print_r(Database::getConnection("default", "default")->query("SELECT COUNT(nid) FROM node;")->fetchObject());'
stdClass Object
(
    [COUNT(nid)] => 100
)

Or against your alternative database (extra_db):


$ drush ev 'print_r(Database::getConnection("default", "extra_db")->query("SELECT COUNT(nid) FROM node;")->fetchObject());'
stdClass Object
(
    [COUNT(nid)] => 0
)

Drupal 6

The default connection:


$ drush ev '$result = db_query("SELECT COUNT(*) FROM node"); print db_result($result) . PHP_EOL;'
10

Or against your alternative database (extra_db):


$ drush ev 'db_set_active("extra_db"); $result = db_query("SELECT COUNT(*) FROM node"); db_set_active(); print db_result($result) . PHP_EOL;'
100

Be sure to switch back to the default connection so that Drupal can cleanly finish the request lifecycle and write to its system tables. It's particularly important to switch back to the active Drupal database before any calls to Drupal functions. Errors in the error.log about not being able to find the system table are an indication that calls to Drupal functions preceded a switch back to the default database.

Contact supportStill need assistance? Contact Acquia Support