Question

I currently have a Drupal 7 site with a big database. I have to install the Drupal Commerce module and I would like to install the module using a separate database.

I know I have to setup the second database in the settings.php file, but I don't know the next steps I should follow.

How can I tell Drupal to use a different database for a module I am installing?

Was it helpful?

Solution

Drupal can be configured to use a different database for some of the tables. For example, with the following database settings, I would tell Drupal to use the db2 database for the users table.

$databases['default']['default'] = array(
  'driver' => 'mysql',
  'database' => 'db1',
  'username' => 'username',
  'password' => 'password',
  'host' => 'localhost',
  'prefix' => array(
    'users' => 'db2.',
  ),
);

$databases['default']['db2'] = array(
  'driver' => 'mysql',
  'database' => 'db2',
  'username' => 'username',
  'password' => 'password',
  'host' => 'localhost',
  'prefix' => '',
);

It doesn't actually work for PostgreSQL because a bug in Drupal core, and it works for those database engines for which using a different schema means using a different database.

Doing so for every table used for a module is more problematic, since the prefix must be explicitly set for all the tables used for that module, which means:

  • You need to know which tables are used for that module, including the tables Drupal creates for that module
  • If a new version of the module uses tables the previous version didn't use, the prefix must be explicitly set before updating the module
  • If a new version of Drupal uses tables for that module the previous version didn't use, the prefix must be explicitly set before updating Drupal
  • If you plan to install another module that store data related to that module, the prefix for the new tables must be explicitly set before installing the new module

Furthermore, for data related to that module that are stored in a table that contains data from all the installed modules, storing that data in a separate table only for a module is probably impossible.

Using two multi-site instances—Drupal installations that use the same code base, are accessible from different URLs (for example, https://example.net instead of https://example.org, or https://example.com/commerce instead of https://example.com), and use different databases (which could also use different database engines)—where one of the instances has the Drupal Commerce module installed, you would resolve the problem to put all the Drupal Commerce tables in a different database.

You would not need to set which database is used for the Drupal Commerce module, as that is already set for each multi-site instance. This means that any change in the tables used for the Drupal Commerce module (which are created from the module itself, Drupal core, or any third-party module) would not require changes to the Drupal database settings. Even updating the Drupal Commerce module or Drupal core would not require extra work to change the Drupal database settings. It would work for any database engine supported by Drupal or from third-party modules.

Since the multi-site instances don't normally share any data, in the same way two different Drupal sites don't share any data, if you want that users with an account on the multi-site instance without the Drupal Commerce module are able to access also the multi-site instance with that module without creating an account, or be logged-in when they already logged in into the first instance, you need to implement SSO on both the multi-site instances. There would be other data duplicated between the two multi-site instances, but those data would probably be less than all the data stored for the Drupal Commerce module.

Licensed under: CC-BY-SA with attribution
Not affiliated with drupal.stackexchange
scroll top