Saturday, March 21, 2020

Shell script to execute mysql query

We can run mysql queries via shell scripts, since this is required at times .

Say  a query is to be scheduled via crontab and shell script.


Here is a simple shell script example, as an example:


vi testscript.sh


-----------------------------------------------------------------------

#!/usr/bin/bash

DBUSER='username'

DBPASSWD='Password'

Query='show databases'   -- example query


mysql -u $DBUSER -p $DBPASSWD  <


$Query

EOF

----------------------------------------------------------------------

The above is a simple script to show databases and is executed with shell script..

# sh testscript.sh

We can schedule the above in crontab to run at a designated time.

Many such scripts can be written to automate daily tasks - backups etc..



No comments:

Post a Comment