How do I use mysql in Shell Scripts?
A. I recommend using Perl or php as it is easier to get data from mysql. You can easily use APIs. BASH/KSH Shell does not provide access to mysql directly.
However you can pass sql command using mysql -e swich. Following query connect as root and print mysql server uptime:
$ mysql -u root -p -e "STATUS" | grep -i uptime
Output:
Uptime: 4 hours 59 min 56 sec
You can add code as follows from shell script:
#!/bin/bash
mysql dbnane<
EOFMYSQL
Save and execute script as follows:
$ chmod +x script.sh
$ ./script.sh
I strongly recommend using perl or php as shell provides limited access.
沒有留言:
張貼留言