Chinaunix首页 | 论坛 | 博客
  • 博客访问: 698982
  • 博文数量: 178
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1507
  • 用 户 组: 普通用户
  • 注册时间: 2014-04-27 23:20
文章分类

全部博文(178)

文章存档

2015年(58)

2014年(121)

我的朋友

分类: LINUX

2015-06-26 17:52:01

 对于自动化运维,诸如备份恢复之类的,DBA经常需要将SQL语句封装到shell脚本。本文描述了在Linux环境下mysql数据库中,shell脚本下调用sql语句的几种方法,供大家参考。对于脚本输出的结果美化,需要进一步完善和调整。以下为具体的示例及其方法。

 

1、将SQL语句直接嵌入到shell脚本文件中

  1. --演示环境  
  2. [root@SZDB ~]# more /etc/issue  
  3. CentOS release 5.9 (Final)  
  4. Kernel \r on an \m  
  5.   
  6. root@localhost[(none)]> show variables like 'version';  
  7. +---------------+------------+  
  8. | Variable_name | Value      |  
  9. +---------------+------------+  
  10. | version       | 5.6.12-log |  
  11. +---------------+------------+  
  12.   
  13. [root@SZDB ~]# more shell_call_sql1.sh   
  14. #!/bin/bash  
  15. # Define log  
  16. TIMESTAMP=`date +%Y%m%d%H%M%S`  
  17. LOG=call_sql_${TIMESTAMP}.log  
  18. echo "Start execute sql statement at `date`." >>${LOG}  
  19.   
  20. execute sql stat  
  21. mysql -uroot -p123456 -e "  
  22. tee /tmp/temp.log  
  23. drop database if exists tempdb;  
  24. create database tempdb;  
  25. use tempdb  
  26. create table if not exists tb_tmp(id smallint,val varchar(20));  
  27. insert into tb_tmp values (1,'jack'),(2,'robin'),(3,'mark');  
  28. select * from tb_tmp;  
  29. notee  
  30. quit"  
  31.   
  32. echo -e "\n">>${LOG}  
  33. echo "below is output result.">>${LOG}  
  34. cat /tmp/temp.log>>${LOG}  
  35. echo "script executed successful.">>${LOG}  
  36. exit;  
  37.   
  38. [root@SZDB ~]# ./shell_call_sql1.sh   
  39. Logging to file '/tmp/temp.log'  
  40. +------+-------+  
  41. | id   | val   |  
  42. +------+-------+  
  43. |    1 | jack  |  
  44. |    2 | robin |  
  45. |    3 | mark  |  
  46. +------+-------+  
  47. Outfile disabled.  
  48. --Author : Leshami  
  49. --Blog   : http://blog.csdn.net/leshami  

2、命令行调用单独的SQL文件

  1. [root@SZDB ~]# more temp.sql   
  2. tee /tmp/temp.log  
  3. drop database if exists tempdb;  
  4. create database tempdb;  
  5. use tempdb  
  6. create table if not exists tb_tmp(id smallint,val varchar(20));  
  7. insert into tb_tmp values (1,'jack'),(2,'robin'),(3,'mark');  
  8. select * from tb_tmp;  
  9. notee  
  10.   
  11. [root@SZDB ~]# mysql -uroot -p123456 -e "source /root/temp.sql"  
  12. Logging to file '/tmp/temp.log'  
  13. +------+-------+  
  14. | id   | val   |  
  15. +------+-------+  
  16. |    1 | jack  |  
  17. |    2 | robin |  
  18. |    3 | mark  |  
  19. +------+-------+  
  20. Outfile disabled.  

3、使用管道符调用SQL文件

  1. [root@SZDB ~]# mysql -uroot -p123456 </root/temp.sql  
  2. Logging to file '/tmp/temp.log'  
  3. id      val  
  4. 1       jack  
  5. 2       robin  
  6. 3       mark  
  7. Outfile disabled.  
  8.   
  9. #使用管道符调用SQL文件以及输出日志  
  10. [root@SZDB ~]# mysql -uroot -p123456 </root/temp.sql >/tmp/temp.log  
  11. [root@SZDB ~]# more /tmp/temp.log  
  12. Logging to file '/tmp/temp.log'  
  13. id      val  
  14. 1       jack  
  15. 2       robin  
  16. 3       mark  
  17. Outfile disabled.  

4、shell脚本中MySQL提示符下调用SQL

  1. [root@SZDB ~]# more shell_call_sql2.sh  
  2. #!/bin/bash  
  3. mysql -uroot -p123456 <<EOF  
  4. source /root/temp.sql;  
  5. select current_date();  
  6. delete from tempdb.tb_tmp where id=3;  
  7. select * from tempdb.tb_tmp where id=2;  
  8. EOF  
  9. exit;  
  10. [root@SZDB ~]# ./shell_call_sql2.sh  
  11. Logging to file '/tmp/temp.log'  
  12. id      val  
  13. 1       jack  
  14. 2       robin  
  15. 3       mark  
  16. Outfile disabled.  
  17. current_date()  
  18. 2014-10-14  
  19. id      val  
  20. 2       robin  

5、shell脚本中变量输入与输出

  1. [root@SZDB ~]# more shell_call_sql3.sh  
  2. #!/bin/bash  
  3. cmd="select count(*) from tempdb.tb_tmp"  
  4. cnt=$(mysql -uroot -p123456 -s -e "${cmd}")  
  5. echo "Current count is : ${cnt}"  
  6. exit   
  7. [root@SZDB ~]# ./shell_call_sql3.sh   
  8. Warning: Using a password on the command line interface can be insecure.  
  9. Current count is : 3  
  10.   
  11. [root@SZDB ~]# echo "select count(*) from tempdb.tb_tmp"|mysql -uroot -p123456 -s  
  12. 3  
  13.   
  14. [root@SZDB ~]# more shell_call_sql4.sh  
  15. #!/bin/bash  
  16. id=1  
  17. cmd="select count(*) from tempdb.tb_tmp where id=${id}"  
  18. cnt=$(mysql -uroot -p123456 -s -e "${cmd}")  
  19. echo "Current count is : ${cnt}"  
  20. exit   
  21.   
  22. [root@SZDB ~]# ./shell_call_sql4.sh   
  23. Current count is : 1  
  24.   
  25. #以上脚本演示中,作抛砖引玉只用,对于输出的结果不是很规整友好,需要进一步改善和提高。  

 

阅读(1067) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~