how to run multiple drupal installations on a single database

Posted by: 
Dominique De Cooman

This is the situation:
You have two drupal sites with each a database. Your hosting allows only one database.
This is your solution:

Rename all the tables in one of your databases to prefix_TABLENAME.
Follow this procedure to rename tables:
Type the mysql statement

show tables

Paste the result in a text editor like PSPad editor
Use regular expression to build your mysql statement. (in pspad CRTL+H)
Replace:RENAME TABLE $1 TO prefix_$1;
Execute the result as mysql statement.
Your tables are renamed to prefix_wathevertheirnamewasbefore
Export your tables and import them into your other drupal database.

Go to your drupal settings.php file that belongs to the site you changed the table names from.
Right underneath the db_url you ll see $db_prefix = '';
Fill this in with your prefix -> $db_prefix = 'prefix_';
Put the same db_url in the $db_rul var as in your other drupal installation.

Right now you have two drupal installations with their database tables are in a single database. But one set is prefixed allowing drupal to know which tables belong to the which installation.

Warning this will only work if you use the database abstraction layer. If you start making your own connections in custom modules or query the db without using db_query() it wont work. An other reason to do things the right way and use the abstraction layer at all times.

Add new comment