---
title: "Connecting a Drupal application to a database outside of Drupal"
date: "2024-02-21T02:10:39+00:00"
summary:
image:
type: "article"
url: "/acquia-cloud-platform/help/93321-connecting-drupal-application-database-outside-drupal"
id: "48f3f2a0-2af0-4743-878a-9ec52b1db2cd"
---

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](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](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:

*   [https://www.drupal.org/docs/7/creating-custom-modules/howtos/how-to-connect-to-multiple-databases-within-drupal#s-drupal-8](https://www.drupal.org/docs/7/creating-custom-modules/howtos/how-to-connect-to-multiple-databases-within-drupal#s-drupal-8)
*   [https://www.drupal.org/docs/8/api/database-api/database-configuration](https://www.drupal.org/docs/8/api/database-api/database-configuration)

  
**2. Switch to the Database inside of your module**  
Switch to the additional database by using the [Database::setActiveConnection()](https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Database%21Database.php/function/Database%3A%3AsetActiveConnection/10) and [Database::getConnection()](https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Database%21Database.php/function/Database%3A%3AgetConnection/10.x) 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!