Using Lando with Multiple Databases
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:<br> legacy:<br> 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:
{<br> ...<br> "legacy": {<br> "type": "mysql",<br> "version": "5.7",<br> "hostnames": [<br> "legacy",<br> "legacy.clientname.internal"<br> ],<br> "creds": {<br> "user": "mysql",<br> "password": "password",<br> "database": "database"<br> },<br> "internal_connection": {<br> "host": "legacy",<br> "port": 3306<br> },<br> "external_connection": {<br> "host": "localhost",<br> "port": "not forwarded"<br> },<br> "urls": []<br> }<br>}
With that information, you can add the new DB configuration to Drupal's settings.php
file.
$databases['old_db']['default'] = array (<br> 'database' => 'database',<br> 'username' => 'mysql',<br> 'password' => 'password',<br> 'prefix' => '',<br> 'host' => 'legacy',<br> 'port' => '3306',<br> 'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',<br> 'driver' => 'mysql',<br>);
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)
Tags: