Designer, Coder & More...

Share users on a Drupal multi-site

Let users login once to access all sites

A multi-site is a good way of splitting up a large site into separate, more manageable sections. There's more information about it in my previous post.

One snag, however, is that by default, they don't offer a seamless user experience, as users will have to log-on and register at each site.

Don't bother with modules

There are a number of modules that allows you to share users, but they all have limitations

Bakery: This doesn't share user roles, which makes it less helpful.
Account Sync: This doesn't seem to work and hasn't been updated in a while.
CAS: This requires phpCAS which isn't available on all hosts.

Use a shared database

Step 1: If your main sites are already set up, make sure they share the same users (and roles). At the very least, make sure they share the same admin user (User 0).

Step 2: Create a new database. In this example, we will call it db-users. In mysql, every database has a user that can access the database. Make sure all the users of your existing database can access this new db-users.

Step 3: From one of the existing sites, copy the following tables into db-users. PHPmyadmin has a copy function which makes this very easy:
user
authmap
sessions
users_roles - You only need to copy user_roles if all the sites are going to share the same role.

Step 4: Open up the setting.php file for all your sites. By default, the settings should look something like this:

$databases['default']['default'] = array(
  'driver' => 'mysql',     
  'database' => 'NAME OF INDIVIDUAL SITE DATABASE',
  'username' => 'DATABASE INDIVIDUAL SITE USER',
  'password' => 'DATABASE INDIVIDUAL SITE PASSWORD',
  'host' => 'localhost',
  'collation' => 'utf8_general_ci',
);

Change it to this:

$databases['default']['default'] = array(
  'driver' => 'mysql',     
  'database' => 'NAME OF INDIVIDUAL SITE  DATABASE',
  'username' => 'DATABASE INDIVIDUAL SITE USER',
  'password' => 'DATABASE INDIVIDUAL SITE PASSWORD',
  'host' => 'localhost',
  'prefix' => array(
    'default'   => 'db-users.',
    'users'     => 'db-users.',
    'sessions'  => 'db-users.',
    'role'      => 'db-users.',
    'authmap'   => 'db-users.',
  ),
  'collation' => 'utf8_general_ci',
);

The dots on the end of 'users' => 'db-users.' tells Drupal to query the users table in db-users data base (SQL syntax)

It is possible not to have a separate db-users database. You can just keep all the users in one database and then share them between the sites. Personally, I think it's better to have a separate user database, as it avoids having a single point of failure, but in everyday usage I doubt it makes a difference.

Is sharing databases a good idea?

Drupal.org recommends against doing this, as it says certain modules aren't set up to work like this. I've used a variety of popular modules, including Drupal Commerce, Views and Entities and not had any problems.

Special thanks to LaTradia on Drupal Answers for the advice about the dot usage.

style="display:inline-block;width:728px;height:90px"
data-ad-client="ca-pub-3945684648599994"
data-ad-slot="7784927124">