Sharing one single database across multiple Drupal installations

More often than not, Drupal users get confident and enthusiastic and start creating more than one Drupal website. But there is one big problem - their hosting service only provides one database! Instead of paying more bucks for another database, there is another way to use multiple Drupal installations on the same database...

The solution to this problem is by using a technique called 'Prefixing' whereby you prefix all the table names for that Drupal installation. In other words if you have multiple website, you can share their tables within the same database by naming them as follows:


The prefix can be set for each Drupal site installation by editing the file 'settings.php' and setting the $db_prefix template. So for the above example you can set the prefix for site1, but adding this line in settings.php:

$db_prefix = 'site1_';

What happens is that once you run the Drupal installation wizard for site1, all the newly created tables will have the 'site1_' prefix appended to them. This will make tables from different Drupal installations to co-exist within the same database!

Note that you can use the same trick for existing sites which have already been installed. The difference is that you need to manually rename each existing table (ex. through scripts).

No comments: