Share tables across instances

Table prefixing may be optionally applied to some tables and not others. This has the effect that multiple Drupal installations can share common tables. One interesting application for this is to share the taxonomy tables (vocabularies, term_data). Another interesting use is to share users across Drupal installations.

In order to use this capability, create two drupal installs in same DB using different database prefixes. In this example, one is prefixed 'master_' and the other 'slave1_'. Then edit the settings.php file of 'slave1_' so that it points some tables to the 'master_'. For sharing users, add the following:

$db_prefix = array(
    "default" => "slave1_", // the prefix for tables that are not shared.
    "users" => "master_",
    "sessions" => "master_",
    "authmap" => "master_",
    "sequences" => "master_",
    "profile_fields" => "master_",
    "profile_values" => "master_",
);

Note: The actual tables that you will share depends on your installation.
However, the following tables contain data that is highly site specific and therefore should not be shared:

  • cache
  • variable

There is a limitation in that you can only explicitly specify which tables will be shared and not the other way round. The following may fail (however a recently deleted comment on this page by marcob suggests this has been fixed:

$db_prefix = array(
  // Be careful of this setup.
  'default' = 'primary_',
  'cache' = 'slave1_',
  'node' = 'slave1_',
  'system' = 'slave1_',
  // etc...
);

Setup tip for Drupal 5

For Drupal 5+, an easy way to get started with table sharing across instances is to run the installer twice using the same database - it will create the prefixed tables and handle all the initial INSERTS (as with the 'system' and 'menu' tables, etc) for all your different prefixes.

step by step please

nickleus - March 13, 2008 - 14:40

ok, i've been trying all day to get this to work because your description is obviously not clear enough =)

1. i have 2 sites:
example.no
example.com

example.com is an addon domain to example.no
so they have the following www structure

public_html (<-- for .no)
-examplecom (<-- examplecom is a subdirectory in the .no directory, where i place my example.com files)
-images
-etc

2. according to this description i install drupal normally for each of the sites, using each their own prefix: no_, com_

3. then i change the settings.php file for example.com to something like this:
$db_prefix = array(
'default' => 'com_',
'users' => 'no_',
'sessions' => 'no_',
'role' => 'no_',
'authmap' => 'no_',
'sequences' => 'no_',
'profile_fields' => 'no_',
'profile_values' => 'no_',
'blocks' => 'no_',
'filters' => 'no_',
'users_roles' => 'no_',
);

and run the installation for example.com again? it doesn't seem to work.

Check out my music
Check out my phrasebook

the solution

nickleus - March 27, 2008 - 11:54

ok i figured it out:
1. i installed drupal on my .no site with the "db_prefix" "no_" and configured it the way i wanted
2. i installed a clean default version of drupal on my .com site with the "db_prefix" "com_" (i essentially have 2 copies of all the drupal files on the same server, i don't know if that is necessary, but that's the quickest way i knew of)
3. my .no site is how i want it to look and my .com is the default blue garland theme
4. next i update my settings.php file for my .com site (remembering to change permissions so i can copy over my updated version and then setting permissions back to what they were before i updated it so that there are no security risks) so that the db_prefix looks like this:
$db_prefix = array(
'default' => 'com_',
'users' => 'no_',
'sessions' => 'no_',
'role' => 'no_',
'authmap' => 'no_',
'sequences' => 'no_',
'profile_fields' => 'no_',
'profile_values' => 'no_',
'blocks' => 'no_',
'filters' => 'no_',
'users_roles' => 'no_',
);

5. next i copy my style.css from my .no site to the .com site:
/public_html/sites/default/files/color/garland-839a678b/style.css
to
/public_html/examplecom/sites/default/files/color/garland-5facff02/style.css

6. i still don't have all my links and page contents (which i will have to manually translate from norwegian to english afterwards) so i need to copy the following tables which i'm not sharing (can't share because they will have different contents, i just want the structure of the sites to be identical):
so in the case of copying the norwegian "no" site to my english "com" site i exported the following "can't be shared" tables using phpmyadmin's export function, to a (text file) sql file:

no_node_revisions : only needed the INSERT lines, so i only copied and pasted those lines into phpmyadmin's sql tab and executed them there

no_node : only needed the INSERT lines, so i only copied and pasted those lines into phpmyadmin's sql tab and executed them there

no_ boxes : only needed the INSERT lines, so i only copied and pasted those lines into phpmyadmin's sql tab and executed them there

no_ menu_links : in phpmyadmin had checked the following additional boxes:
-- add drop table
-- add if not exists
and then copied and pasted the whole sql file into phpmyadmin's sql tab and executed them there

no_menu_router : in phpmyadmin had checked the following additional boxes:
-- add drop table
-- add if not exists
and then copied and pasted the whole sql file into phpmyadmin's sql tab and executed them there

no_url_alias : only needed the insert lines and just executed them in the database using phpmyadmin

7. now if i refresh my browser and clear the cache (ctrl+F5) then the sites are almost identical =) (except for a couple of image paths for images i have added myself, that i need to manually update)

8. the last thing i need to do is login as my admin user and go to my garland them configuration page:
/admin/build/themes/settings/garland

and manually update the colors and upload my logo (copying it and setting the path manually and saving didn't work)

9. now i have successfully copied my norwegian site to my english one and am sharing the tables that define the link structure and site structure that are possible to share =)

*UPDATE 20080327*
10. enable "Path" in the modules section for my english site:
administer > site building > modules > put a checkmark by "Path" (Allows users to rename URLs) > save configuration

amen

Yours truly,
Nick Humphrey

for more information see my thread important drupal 6 mysql database tables

Check out my music
Check out my phrasebook

 
 

Drupal is a registered trademark of Dries Buytaert.