Running Mysql commands from Bash script

database
ID: 20140920 ACCESSING DATA...
SYSTEM: ARCHIVE FILE: RUNNING MYSQL COMMANDS FROM BASH SCRIPT STATUS: ACTIVE

Eventually, you’ll need to automate queries on your Mysql database, let’s say, for reporting purposes. Hee’s how to put SQL queries into a shell script, and possibly including variables based on the date or whatever fits your needs:

$ cat execmysql.sh
#!/bin/sh
ids="3,4"
table="NMS.main"
qry="select id,data from $table where id in ($ids)"
echo "Executing the query..."
echo $qry
/usr/bin/mysql -u root << eof
$qry
eof

And the execution output:

$ ./execmysql.sh
Executing the query...
select id,data from NMS.main where id in (3,4)
id data
3 pizza
4 bash

Tags: