Skip to content

Instantly share code, notes, and snippets.

@AV4TAr
Last active December 2, 2015 13:21
Show Gist options
  • Save AV4TAr/f6cd7f369c7541bcee44 to your computer and use it in GitHub Desktop.
Save AV4TAr/f6cd7f369c7541bcee44 to your computer and use it in GitHub Desktop.
Script that reads php configuration file (that returns an array) and connects to a mysql database.
To use mysql client on staging, devs need to ssh to the server, then "cat" the configuration file of the application to get the username / password / host.
The idea of this script is to read the configuration from the php application and use that information to connect to mysql using the mysql client in 1 command.
<?php
return [
'db_name' => 'application',
'db_user' => 'root',
'db_password' => 'root',
'db_host' => '127.0.0.1'
];
#
# Aid script to connect to mysql easly in different environments.
# It reads the application configuration file and uses it to connect to mysql.
#
sqluname=$(php -r '$var = include("config.php"); echo $var["db_user"];')
sqlpasswd=$(php -r '$var = include("config.php"); echo $var["db_password"];')
dbname=$(php -r '$var = include("config.php"); echo $var["db_name"];')
sqlhost=$(php -r '$var = include("config.php"); echo $var["db_host"];')
if [ -n "$sqlpasswd" ]; then
/usr/bin/mysql -h$sqlhost -u $sqluname -p$sqlpasswd $dbname
else
/usr/bin/mysql -h$sqlhost -u $sqluname $dbname
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment