0

I'm building an encyclopedia website. There are about 20 subjects that must be stored in the Database (media,library,article,translations,...) and we want to create 20 databases and allocate one DB for every subject. is it possible to do such thing? The main site has 3 sub domains and every sub domain covers specific rows of the subjects. for example the library is a subject and math.mainsite.com must have access only to the math type of the library subject.

the sub-domains must not have not any data, they all should get their data from the 20 above mentioned DBs, so every site must connect to 20 DB.

We selected this strategy because the subjects are subdomain as well, I mean library.mainsite.com and article.mainsite.com and ... are valid urls.

thanks

1 Answer 1

5

Two steps:

  1. Add database credentials to the $databases array in settings.php file. You can have as many additional databases as you want. Example code:

    $databases = array (
      'default' => 
      array (
        'default' => 
        array (
          'database' => 'YOUR_DEFAULT_DATABASE',
          'username' => 'username',
          'password' => 'password',
          'host' => 'localhost',
          'port' => '',
          'driver' => 'mysql',
          'prefix' => '',
        ),
      ),
      'your_second_db_alias' => 
      array (
        'default' => 
        array (
          'database' => 'YOUR_SECOND_DB',
          'username' => 'username',
          'password' => 'password',
          'host' => 'localhost',
          'port' => '',
          'driver' => 'mysql',
          'prefix' => '',
        ),
      ),
    );
    
  2. Use the following code to change the database and run queries. Note that you must switch back to the original database afterwards.

    // Change active database.
    db_set_active('your_second_db_alias');
    // Run queries.
    $query = db_query("SELECT * FROM yourtable");
    // IMPORTANT: return current active database to the default one. If you
    // don't do this, everything after that point will be executed against the
    // above database, and cause errors most probably. If you are connecting
    // multiple Drupal installations, you can cause a pretty big mess if data is
    // partially saved in the other database.
    // Always double check that you have this line!
    db_set_active();
    

Hope this helps!

4
  • So how do I can create views for different databases?
    – M a m a D
    Commented Apr 7, 2014 at 13:12
  • 1
    If you are having issues with views and multiple DBs, post it as a separate question. Have a look at this thread first. Commented Apr 7, 2014 at 15:55
  • Aram - the linked thread is missing. Commented Aug 7, 2016 at 19:56
  • Well that page says that This question was removed from Drupal Answers for reasons of moderation.. I suggest posting your problem as a separate question. Commented Aug 7, 2016 at 20:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.