Anonymous Anonymous - 2 years ago 334
Bash Question

How to auto login mysql in shell scripts?

I have a mysql server with empty root password. I want to execute some sql statements in shell scripts without specifying the password like this:

config.sh:

MYSQL_ROOT="root"
MYSQL_PASS=""


mysql.sh

source config.sh
mysql -u$MYSQL_ROOT -p$MYSQL_PASS -e "SHOW DATABASES"


How do I auto execute that sql in shell with the -p option and an empty password?

Answer Source

Try:

if [ $MYSQL_PASS ]
then
  mysql -u "$MYSQL_ROOT" -p "$MYSQL_PASS" -e "SHOW DATABASES"
else
  mysql -u "$MYSQL_ROOT" -e "SHOW DATABASES"
fi
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download