Drupal shared database multi-site
Drupal allows you to run from multiple different databases by being creative with the use of the multi-site functionality built into core. Using multi-site in this method requires certain intentional design decisions.
Note, this method is no replacement for fault-tolerant redundancy through replication, but it will allow a difference of content management between a primary domain and subdomains with different priorities.
In this example, I will show how to run a single site through multiple different subdomains and setup a development environment separate from the live configuration. This site will have a www domain for edited articles, blogs for users, and forums as a free-for-all.
First of all, administration of each multi-site will be essentially independent from another with the exception of the user tables, because we will share accounts across the sites. The three tables you'll want to separate from the rest of your configuration are at least
- authmap
- sessions
- users
You might as why not share the role
and users_roles
, but in this example, a content editor role won't have any usefulness on the blogs subdomain, neither will a forum moderator have a point administering blogs.
As part of this, there will be three subdomains: www, blogs, and forums, which means our database names will be
- drupal_www
- drupal_blogs
- drupal_forums
- drupal_userdb
Your file directory structure will request primarily from www, meaning that will be the default configuration of your Drupal multi-site installation.
/root folder
- sites
- default (dir)
- settings.php
$db_url = 'mysql://user:password@localhost/drupal_www';<br>$db_prefix = array(<br> 'default' => '',<br> 'authmap' => 'drupal_userdb.',<br> 'sessions' => 'drupal_userdb.',<br> 'users' => 'drupal_userdb.'<br>);<br>$base_url = 'http://www.mysite.com';
- settings.php
- blogs.mysite.com (dir)
- settings.php
$db_url = 'mysql://user:password@localhost/drupal_blogs';<br>$db_prefix = array(<br> 'default' => '',<br> 'authmap' => 'drupal_userdb.',<br> 'sessions' => 'drupal_userdb.',<br> 'users' => 'drupal_userdb.'<br>);<br>$base_url = 'http://blogs.mysite.com';
- settings.php
- forums.mysite.com (dir)
- settings.php
$db_url = 'mysql://user:password@localhost/drupal_forums';<br>$db_prefix = array(<br> 'default' => '',<br> 'authmap' => 'drupal_userdb.',<br> 'sessions' => 'drupal_userdb.',<br> 'users' => 'drupal_userdb.'<br>);<br>$base_url = 'http://forums.mysite.com';
- settings.php
- default (dir)
So far to this point, it's a fairly standard installation of multi-site Drupal with sharing the tables, but it's hard to use multi-site in a local environment if you want to be able to test each of the subdomains, which will only respond to the domains set in the directory names.
Since you shouldn't check your settings file into version control, your local copy of the settings.php files will change the $base_url
to something more local-ish.
$base_url = 'http://forums.mysite.local';
Then update your /etc/hosts
file to have some new entries:
127.0.0.1 www.mysite.local<br>127.0.0.1 blogs.mysite.local<br>127.0.0.1 forums.mysite.local
Then make a symbolic link to each one of your multi-site directories so Drupal will be able to find the local domains and respond to them with the local domain settings.
/root folder
- sites
- default (dir)
- blogs.mysite.com (dir)
- blogs.mysite.local (symlink)
- forums.mysite.com (dir)
- forums.mysite.local (symlink)
Your symlinks and settings.php files won't be in version control because you ignored them (right?), so you should be able to easily develop from a local multi-site environment without having to setup a fancy MAMP-like package to manage your virtual, local domains.
Post categories