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.
macOS
brew install jq
Ubuntu
sudo apt-get install jq
Windows
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_SERVICE="database"
LANDO_INFO=$(lando info --filter "service=$LANDO_SERVICE" --format json)
DB_CONNECTION="mysql"
DB_HOST=$(echo $LANDO_INFO | jq '.[0].external_connection.host' | 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')
DB_URL="${DB_CONNECTION}://${DB_USERNAME}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_DATABASE}"
echo "Opening ${DB_URL}"
open $DB_URL
}