execute mysql command remotely from shell
# mysql -h [ip] -u [user] -p[pass] -e "[mysql commands]"
# mysql -h hostip -u root -pmysqlpassword -e "show databases"
Mention database to use.
Use -D option to specify the name of MySQL database :
# mysql -D [db name] -u [user] -p[pass] -e "[mysql commands]"
# mysql -D DBName -u root -pmysqlpassword -e "show tables"
Use the following syntax in your Bash scripts for running MySQL commands :
mysql -u [user] -p[pass] << EOF
[mysql commands]
EOF
#!/bin/bash mysql -u root -pmysqlpassword << EOF use mysql; show tables; EOF