Using Lando with Multiple Databases

Parent Feed: 

A lot of my work over the last few years has been working on migrations between various versions of Drupal. That usually means that I need to configure a local Drupal development environment with more than one database. And although this is relatively easy to do with Lando, I often have to look up how I did it before. So, I figured I should write it down and share with everyone else at the same time.

.lando.yml

Adding a database to an existing Lando environment is as easy as adding a few lines to the .lando.yml file and restarting.

services:
  legacy:
    type: mysql

This will create a new container called legacy with a MySQL database in it. Out of the box, Lando supports many common types of DB servers, including: MySQL, MariaDB, MongoDB, MSSQL, and PostgreSQL.

Often, your .lando.yml file might already have configuration in it. If the services line already exists, just put the new configuration underneath with the correct indentation. You can see examples of more complex configuration files at any of the links in the previous paragraph.

settings.php

Now, you will need to tell Drupal about the new DB. To do this, go to the command line and type lando info. In the output, you should see something like this:

{
  ...
  "legacy": {
    "type": "mysql",
    "version": "5.7",
    "hostnames": [
      "legacy",
      "legacy.clientname.internal"
    ],
    "creds": {
      "user": "mysql",
      "password": "password",
      "database": "database"
    },
    "internal_connection": {
      "host": "legacy",
      "port": 3306
    },
    "external_connection": {
      "host": "localhost",
      "port": "not forwarded"
    },
    "urls": []
  }
}

With that information, you can add the new DB configuration to Drupal's settings.php file.

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

Note that, by default the host name is going to correspond to the name of the service/container and will not necessarily be the same as the name of the database (or the name of the Drupal DB alias, for that matter). In other words, you should find the host and port values in Lando's internal_connection array. If, for some reason, you need to have a custom database name, credentials, port numbers or something else, you can refer to the links above.

Related Article(s)

Author: 
Original Post: 

About Drupal Sun

Drupal Sun is an Evolving Web project. It allows you to:

  • Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
  • Facet based on tags, author, or feed
  • Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
  • View the entire article text inline, or in the context of the site where it was created

See the blog post at Evolving Web

Evolving Web