Connect to any Lando service via Shell alias

Connect to any Lando service via Shell alias
Roman Zipp

When using Lando as local development tooling, you will be familiar that localhost ports get assigned randomly. This can be tricky if you need to connect to a container regurarily.

This is a short shell function which builds a MySQL/MariaDB connection URI string using the lando and jq binaries.

Install jq

JQ is a lightweight and flexible command-line JSON processor made by Stephen Dolan which is used to extract details form the lando info JSON output.


brew install jq


sudo apt-get install jq


See official install guide.

Shell function

Of course, you can alter the function to output any combination of connection details.

This example always uses the database lando service.

function db() {
    [ ! -f .lando.yml ] && { echo "Info: No .lando.yml file found"; }

    LANDO_INFO=$(lando info --filter "service=$LANDO_SERVICE" --format json)

    DB_HOST=$(echo $LANDO_INFO | jq '.[0]' | sed 's/\"//g')
    DB_PORT=$(echo $LANDO_INFO | jq '.[0].external_connection.port' | sed 's/\"//g')
    DB_DATABASE=$(echo $LANDO_INFO | jq '.[0].creds.database' | sed 's/\"//g')
    DB_USERNAME=$(echo $LANDO_INFO | jq '.[0].creds.user' | sed 's/\"//g')
    DB_PASSWORD=$(echo $LANDO_INFO | jq '.[0].creds.password' | sed 's/\"//g')


    echo "Opening ${DB_URL}"
    open $DB_URL