blog-banner

Using Drush to Administer Multisite

  • Drupal Planet
  • Drush
  • MULTISITE

Drupal Multisite with Single Database

 

One of the admiring features of Drupal is its ability to leverage a single code base to power a stack of sites. Drush the excellent command line utility eases the work of administering Drupal sites.

Our recent work raised a question, will these two blend & work together?

Of course, we tried this, and the answer is yes but to be used with more caution!!

In the case of Drupal multisite, Apache (or any Web server) is configured such that more than one domain name points to the same document root. Drupal by identifying the request originating domain and folder structures loads the respective modules/themes & database. Drush utilizes a similar approach.

In the case of multisite, to run a drush command against a specific site/database there are two ways,

1. Switching to the respective site folder

In this case cd to the site folder. Ideally from inside the folder sites/one.example.com or similar the drush commands to be issued. This will run all the commands against one.example.com site.

Assume a multisite setup with a separate database per site. Then

$ drush sql-connect

would return

user@hostname:/var/www/sites/one.example.com$ drush sql-connect
mysql --database=example_one --host=localhost --user=example_one --password=example1

2. Passing an additional argument to Drush commands

This works by passing the argument -l (or --uri). The request originating domain is mentioned explicitly here and hence takes more precedence over #1.

user@hostname:/var/www/sites/one.example.com$ drush sql-connect -l https://two.example.com
mysql --database=example_two --host=localhost --user=example_two --password=example2
Since in multisite environment resources are shared extra care to be taken when commands like pm-updatecode, updatedb, etc. as these are more likely to perform a write operation on database and/or files and hence could affect more than one site at once. So it is recommended to use drush for limited cases when it comes to administering a multisite environment.
Get awesome tech content in your inbox