leoraw houses banner for website

Technical Posts with Code Exploring Web Development

Lando + Drupal 8 + Second Database

Some people make success look easy. For me, a new piece can be a struggle. When I succeed, I like to cheer a little. I invite you to applaud along with me.

A while back I was working on a migration. A migration is when you move parts of one site to a different site. My migration kept trying to migrate to itself instead from old site to new site. I got quite frustrated!

Fast forward to recently, I had the time and the clearness of head to solve the setup properly. So for those who came seeking the technical solution, here is how to setup a second database with Lando and Drupal 8.

1. Set up the .lando.yml file first.

name: namesite
recipe: drupal8
config:
  webroot: web
services:
  database:
    portforward: 3307
  db2:
    type: mysql
    portforward: 3308

Your .lando.yml file may not quite look like this, but the essential part is in services – the first one, called database, is the new site. The second one, which is not a full site but just a database exported from your old site, I called db2, but you can call it legacy or whatever suits you. I also added the portforward so I could more easily access either database using Sequel-Pro.

2. Make backups along the way. If you use Lando, you probably learned that you can use

lando db-import yoursite.sql

to import a database. You can also do an export as a backup, which come in quite handy if you want to change your site back to where it was, and clearing cache or re-importing config is not getting you to where you want:

lando db-export bk-date.sql

Move the created bk-date.sql (instead of date, type the approximate date that you did the backup, just as a reminder) outside of your project. Use it by doing a db-import back into your site if you want to go back to an early stage of the site.

3. When setting up your migration, first set up the new site. Make sure Drupal is already installed before importing the old database to the db2 spot. Once the new site is ready, then you can do the db2 import:

lando db-import --host db2 myoldsite.sql --no-wipe

4. This is how the database section of settings.php of your Drupal 8 site might look, with the two databases in places, the top one being the main Drupal 8 site and the second one being your source:


$databases['default']['default'] = array (
  'database' => 'drupal8',
  'username' => 'drupal8',
  'password' => 'drupal8',
  'prefix' => '',
  'host' => 'database',
  'port' => '3306',
  'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',
  'driver' => 'mysql',
);

 $databases['db2']['default'] = array(
  'database' => 'database',
  'username' => 'mysql',
  'password' => 'mysql',
  'prefix' => '',
  'host' => 'db2',
  'port' => '3306',
  'namespace' => 'Drupal\Core\Database\Driver\mysql',
  'driver' => 'mysql',
);

Hope someone finds this helpful! Thank you for reading.

Vocabulary:

Lando: Lando is a free, open-source platform built on Docker for building local Drupal, WordPress or other PHP sites. More on Lando.

Drupal: Drupal is powerful content management software. More on Drupal.