With over a decade of web development experience, I specialize in Drupal (7, 8, 9, 10), CodeIgniter, Laravel, and WordPress. I offer extensive expertise in both module and theme development, providing customized solutions for complex projects. Whether you need to enhance an existing platform, create new features, or seek expert guidance, I'm here to assist. My dedication to delivering high-quality, efficient, and scalable solutions is unmatched. Feel free to contact me to explore how I can contribute to your project's success. Let's turn your ideas into reality!

“Explore step-by-step instructions to set up and manage multiple database connections in Drupal 9 for improved scalability and performance.”

Setting up multiple database connections in Drupal 9 involves configuring your settings.php file and then accessing the databases in your code. Below is a step-by-step guide with code snippets and explanations.

  1. Update settings.php: Open your settings.php file located in the sites/default directory of your Drupal installation. Add the following code to define additional database connections:

    // sites/default/settings.php
    
    // Primary database connection.
    $databases['default']['default'] = array(
      'database' => 'your_primary_database_name',
      'username' => 'your_primary_database_username',
      'password' => 'your_primary_database_password',
      'prefix' => '',
      'host' => 'localhost',
      'port' => '3306',
      'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',
      'driver' => 'mysql',
    );
    
    // Secondary database connection.
    $databases['second']['default'] = array(
      'database' => 'your_secondary_database_name',
      'username' => 'your_secondary_database_username',
      'password' => 'your_secondary_database_password',
      'prefix' => '',
      'host' => 'localhost',
      'port' => '3306',
      'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',
      'driver' => 'mysql',
    );

    Replace your_primary_database_name, your_primary_database_username, etc., with your actual primary database credentials. Similarly, replace the values for the secondary database.

  2. Accessing the Secondary Database in Code: After defining the additional database connection in settings.php, you can access the secondary database in your code. Below is an example that retrieves data from the secondary database:

    // Use the Database Connection service.
    $database_connection = \Drupal::database();
    
    // Use the primary database (default connection).
    $query_primary = $database_connection->select('table_name_in_primary_db', 't')
      ->fields('t')
      ->condition('t.field_name', 'some_value')
      ->execute()
      ->fetchAssoc();
    
    // Use the secondary database.
    $query_secondary = Database::getConnection('default', 'second')
      ->select('table_name_in_secondary_db', 't')
      ->fields('t')
      ->condition('t.field_name', 'some_value')
      ->execute()
      ->fetchAssoc();
    
    // Process the results from both databases as needed.

    Replace table_name_in_primary_db and table_name_in_secondary_db with the actual table names in your databases. Also, adjust the conditions and fields based on your requirements.

This example assumes you are using Drupal 9 and the MySQL database driver. If you are using a different database system, you may need to adjust the driver and connection settings accordingly.

Remember to clear the Drupal cache after modifying the settings.php file by running:

drush cr

or

drupal cr

Now you should be able to use multiple database connections in your Drupal 9 project.

Posted by Sujan Shrestha
Categorized:
PREVIOUS POST
banner