How can I create and load a second database in ddev?

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


You can import additional databases directly with ddev import-db --target-db=newdb. 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 --src=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/stable/users/basics/database_management/


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