Loading...


Related Products


Date Published: February 21, 2024

Connecting a Drupal application to a database outside of Drupal

Issue

How do I connect to a database outside of Drupal?

Resolution

There are steps listed out in this Drupal.org page:

https://www.drupal.org/docs/7/creating-custom-modules/howtos/how-to-connect-to-multiple-databases-within-drupal

In short:

  1. Define the secondary Database in your "settings.php",
  2. Switch to the Database inside of your module,
  3. Query that database,
  4. Swap back to the default Database when finished.
 

Detailed example

The following is an example of changing between the default Drupal database and a second, additional, database in a custom module, to query the second one.

1. Define the secondary Database in your "settings.php"
The file, https://api.drupal.org/api/drupal/sites%21default%21default.settings.php/10 , under the "View Source" section, defines the format for the $databases variable.
Essentially, the first key is the database name and the second key is used as a replication database name, or a "target" name, if you wish to use primary/replica database replication for your site.
$databases['database_name']['database_target']

If you are wanting to access a different database, in addition to your Drupal database, then use the first key name for the $databases variable.

For example, the following code snippet defines the configurations for the default Drupal database and an additional database, named "second_db". Note the keys used for  the $databases variable.
 
// Default Drupal database configuration
$databases['default']['default'] = array (
  'database' => 'drupal',
  'username' => 'drupal',
  'password' => 'drupal',
  'prefix' => '',
  'host' => 'localhost',
  'port' => '3306',
  'isolation_level' => 'READ COMMITTED',
  'driver' => 'mysql',
  'namespace' => 'Drupal\\mysql\\Driver\\Database\\mysql',
  'autoload' => 'core/modules/mysql/src/Driver/Database/mysql/',
);


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

See further details, for the database configuration definitions, in the following Drupal documentation pages:
2. Switch to the Database inside of your module
Switch to the additional database by using the Database::setActiveConnection() and Database::getConnection() static methods.
For the example presented here, this would be a call such as:
// Change the active connection to the additional database
Database::setActiveConnection('second');

// Get the database connection for the additional database
$second_db_conn = Database::getConnection();
 
3. Query the database
Perform the query to the database that is not the default Drupal database.
// Execute a query on tables in the additional database
$result = $second_db_conn->select('some_table', 't')
      ->fields('t', ['id', 'column_name'])
      ->range(0, 1)
      ->execute()
      ->fetchAll();

// Do something with the result
// ...


4. Swap back to the default Database when finished
Change the connection back to the default Drupal database connection.
// Switch back to the default Drupal database.
Database::setActiveConnection();

That's it, you have successfully implemented a change between your default Drupal database and an additional one!

Did not find what you were looking for?

If this content did not answer your questions, try searching or contacting our support team for further assistance.

Back to Section navigation