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
if not test -f .lando.yml
echo "Info: No .lando.yml file found"
return
end
set LANDO_SERVICE "database"
set LANDO_INFO (lando info --filter "service=$LANDO_SERVICE" --format json)
set DB_CONNECTION "mysql"
set DB_HOST (echo $LANDO_INFO | jq '.[0].external_connection.host' | sed 's/\"//g')
set DB_DATABASE (echo $LANDO_INFO | jq '.[0].creds.database' | sed 's/\"//g')
set DB_USERNAME (echo $LANDO_INFO | jq '.[0].creds.user' | sed 's/\"//g')
set DB_PASSWORD (echo $LANDO_INFO | jq '.[0].creds.password' | sed 's/\"//g')
set DB_PORT (echo $LANDO_INFO | jq '.[0].external_connection.port' | sed 's/\"//g')
if test "$DB_PORT" = "true"
set DB_PORT (echo $LANDO_INFO | jq '.[0].internal_connection.port' | sed 's/\"//g')
end
set DB_URL "$DB_CONNECTION://$DB_USERNAME:$DB_PASSWORD@$DB_HOST:$DB_PORT/$DB_DATABASE"
echo "Opening $DB_URL"
open $DB_URL
end

Comments
Nothing yet. Say the first thing.
Sign in to join the conversation.