平常执行sql,需要登录到mysql的shell下,然后再执行。比如:
$ /usr/bin/mysql -u root
mysql> select * from users;
但是如果写一点简单的脚本,也可以在命令行下直接运行sql并显示结果,比如:
$ cat executemysql.sh
#!/bin/sh
qry=$1;
echo "Executing the following query"
echo "$qry"
mysql -u root << eof
$qry
eof
运行一下:
$ ./executemysql.sh "select id,name,age from test.users limit 2"
Executing the following query
select id,name,age from test.users limit 2
id name age
3 john 20
4 tom 21