16

I have a Drupal multisite that needs to have one database for each site, and want it to run in ddev, but ddev just has the one database by default, named 'db'. How can I get a second database?

rfay
  • 9,963
  • 1
  • 47
  • 89
  • I'm curious why would you do something like that? Why not just have 2 totally separated sites? I never saw setup like that before... – MilanG Apr 13 '18 at 06:30
  • Yeah, I sure wouldn't, I've thought multisite for Drupal was a bad idea since the first time I tried to update one site of a multisite and found that it broke all the others. But people do ask for it. There are totally good (other) reasons to have an extra database though: Some nontrivial sites have application-specific data in a separate database (and that's a good idea). – rfay Apr 14 '18 at 16:36
  • I maintain some 3 sites where they share just some files (admin/user uploaded) and except saving some space (2 of the are created from initial one) I never saw any benefit of that kind of setup...Only trouble. – MilanG Apr 16 '18 at 06:45
  • This is what I was actually what I've been looking for as well. I have multiple sites using one code base but varying DBs/configurations. Having multiple databases for each would enable me to only focus on my settings.php to map my DBs. – Sivuyile Shasha Apr 08 '21 at 19:12
  • Projects can talk to each others' databases. See the FAQ, https://ddev.readthedocs.io/en/stable/users/faq/ - "Can different projects communicate with each other" – rfay Apr 12 '21 at 12:26

3 Answers3

43

You can import additional databases directly with ddev import-db --target-db=newdb --file=/path/to/file.sql.gz. The created database will already have permissions, etc.

You can also manually create and manage databases (although this is rarely necessary any more). The root password for the db server is 'root', so you can mysql -uroot -proot in there (or use ddev mysql -uroot -proot).

  • ddev mysql -uroot -proot
  • CREATE DATABASE newdb;
  • GRANT ALL ON newdb.* to 'db'@'%' IDENTIFIED BY 'db';
  • Now, if you want to load from a db dump, ddev import-db --target-db=newdb --file=dumpfile.sql
  • Your normal web user can now access this alternate db, and it can be used in the settings.php for your alternate multisite.
  • There are many other things you'll want to do for your Drupal multisite; there is a full tutorial at https://github.com/drud/ddev-contrib/tree/master/recipes/drupal8-multisite

More details about database management at https://ddev.readthedocs.io/en/latest/users/usage/database-management/

rfay
  • 9,963
  • 1
  • 47
  • 89
7

In addition to rfay answer, The trick used in the last link was exactly what I wanted to propose and what I'm currently using:

Add this hook to the config.yml file

hooks:
  post-start:
  - exec: mysql -uroot -proot -hdb -e "CREATE DATABASE IF NOT EXISTS second_db; GRANT
      ALL ON second_db.* TO 'db'@'%';"

And load data to the second database by using the param --target-db:

ddev import-db --target-db=second-db --src=second-db.sql

Ignacio Sánchez
  • 409
  • 4
  • 14
  • 2
    You don't have to do the CREATE or the GRANT , those get done by the `ddev import-db --target-db=second_db` But you can do the import-db as a post-start hook. The import-if-empty script in ddev-contrib would be nice here as well, https://github.com/drud/ddev-contrib/tree/master/hook-examples/import-db-if-empty – rfay Jun 02 '20 at 16:36
  • 2
    Good to know. Thanks! – Ignacio Sánchez Jun 03 '20 at 10:25
3

I like the approach of creating an additional config.multisite.yaml and add add the definition there:

additional_hostnames:
  - basic
  - umami
hooks:
  post-start:
  - exec: mysql -uroot -proot -e "CREATE DATABASE IF NOT EXISTS basic; GRANT ALL ON basic.* to 'db'@'%';"
    service: db
  - exec: mysql -uroot -proot -e "CREATE DATABASE IF NOT EXISTS umami; GRANT ALL ON umami.* to 'db'@'%';"
    service: db

Above is from ddev-contrib recieps https://github.com/drud/ddev-contrib/blob/master/recipes/drupal8-multisite/dot.ddev/config.multisite.yaml

hugronaphor
  • 948
  • 8
  • 23