在命令行里执行mysql的sql

平常执行sql,需要登录到mysql的shell下,然后再执行。比如:
$ /usr/bin/mysql -u root
mysql> select * from users;

但是如果写一点简单的脚本,也可以在命令行下直接运行sql并显示结果,比如:

CODE:
  1. $ cat executemysql.sh
  2. #!/bin/sh
  3.  
  4. qry=$1;
  5.  
  6. echo "Executing the following query"
  7. echo "$qry"
  8.  
  9. mysql -u root <<eof
  10. $qry
  11. eof

运行一下:

CODE:
  1. $ ./executemysql.sh "select id,name,age from test.users limit 2"
  2. Executing the following query
  3. select id,name,age from test.users limit 2
  4. id name age
  5. 3 john 20
  6. 4 tom 21

原文:Execute mysql from bash script

作者: Volcano 发表于June 23, 2008 at 11:05 am

版权信息: 可以任意转载, 转载时请务必以超链接形式标明文章原始出处作者信息及此声明

Tags: ,,

留条评论