MySql connect bash script from Laravel .env file
This is small bash scripts which reads Laravel .env configuration and connects to mysql database with mysql cli client tool.
Instead of opening .env file and copy pasting mysq; connections credentials manually, run this bash script and connect to mysql database
#!/bin/bash
host=""
database=""
user=""
password=""
while read -r line; do
key=$(awk -F '=' '{print $1}' <<< $line)
val=$(awk -F '=' '{print $2}' <<< $line)
if [ "$key" = "DB_HOST" ]; then
host=$val
elif [ "$key" = "DB_USERNAME" ]; then
user=$val
elif [ "$key" = "DB_DATABASE" ]; then
database=$val
elif [ "$key" = "DB_PASSWORD" ]; then
password=$val
fi
done <<< "$(cat .env)"
command="mysql -h $host -u $user"
# password
if [ -n "$password" ]; then
command=$command" -p$password"
fi
# db name
command=$command" $database"
$command