Raphael Rafatpanah
Raphael Rafatpanah

Reputation: 19977

Drupal - best practice for database settings

In Drupal, what is the best practice for configuring database settings (database, username, password, host, etc.)?

In sites/default/default.settings.php it states the following:

/**
 * Database settings:
 *
 * The $databases array specifies the database connection or
 * connections that Drupal may use.  Drupal is able to connect
 * to multiple databases, including multiple types of databases,
 * during the same request.
 *
 * One example of the simplest connection array is shown below. To use the
 * sample settings, copy and uncomment the code below between the @code and
 * @endcode lines and paste it after the $databases declaration. You will need
 * to replace the database username and password and possibly the host and port
 * with the appropriate credentials for your database system.
 *
 * The next section describes how to customize the $databases array for more
 * specific needs.
 *
 * @code
 * $databases['default']['default'] = array (
 *   'database' => 'databasename',
 *   'username' => 'sqlusername',
 *   'password' => 'sqlpassword',
 *   'host' => 'localhost',
 *   'port' => '3306',
 *   'driver' => 'mysql',
 *   'prefix' => '',
 *   'collation' => 'utf8mb4_general_ci',
 * );
 * @endcode
 */
 $databases = array();

But what if your development environment and your production environment will have different database settings?

Upvotes: 0

Views: 305

Answers (1)

Ricardo Velhote
Ricardo Velhote

Reputation: 4680

Drupal supports multisites. You can use that for multiple configurations for development, staging and production.

Create a folder in the sites folder with the name of your development, staging and production domains and each of them will have their own settings.php file which in turn means they will have separate database connection configurations. Drupal will auto-select the correct folder depending on the domain that is currently being used.

Usually I setup three folders:

  • development.myproject.com
  • staging.myproject.com
  • myproject.com (production)

Configuring sites.php is optional if the folders you create are exactly the domain name. You can configure sites.php if you want to access via a path (e.g. myproject.com/devel).

Some more info in the Drupal docs.

Upvotes: 1

Related Questions