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

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

复制代码代码如下:
--演示环境  
[root@SZDB ~]# more /etc/issue  
CentOS release 5.9 (Final)  
Kernel \r on an \m  
  
root@localhost[(none)]> show variables like 'version';  
+---------------+------------+  
| Variable_name | Value      |  
+---------------+------------+  
| version       | 5.6.12-log |  
+---------------+------------+  
  
[root@SZDB ~]# more shell_call_sql1.sh   
#!/bin/bash  
# Define log  
TIMESTAMP=`date +%Y%m%d%H%M%S`  
LOG=call_sql_${TIMESTAMP}.log  
echo "Start execute sql statement at `date`." >>${LOG}  
  
# execute sql stat  
mysql -uroot -p123456 -e "  
tee /tmp/temp.log  
drop database if exists tempdb;  
create database tempdb;  
use tempdb  
create table if not exists tb_tmp(id smallint,val varchar(20));  
insert into tb_tmp values (1,'jack'),(2,'robin'),(3,'mark');  
select * from tb_tmp;  
notee  
quit"  
  
echo -e "\n">>${LOG}  
echo "below is output result.">>${LOG}  
cat /tmp/temp.log>>${LOG}  
echo "script executed successful.">>${LOG}  
exit;  
  
[root@SZDB ~]# ./shell_call_sql1.sh   
Logging to file '/tmp/temp.log'  
+------+-------+  
| id   | val   |  
+------+-------+  
|    1 | jack  |  
|    2 | robin |  
|    3 | mark  |  
+------+-------+  
Outfile disabled.  

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

复制代码代码如下:
[root@SZDB ~]# more temp.sql   
tee /tmp/temp.log  
drop database if exists tempdb;  
create database tempdb;  
use tempdb  
create table if not exists tb_tmp(id smallint,val varchar(20));  
insert into tb_tmp values (1,'jack'),(2,'robin'),(3,'mark');  
select * from tb_tmp;  
notee  
  
[root@SZDB ~]# mysql -uroot -p123456 -e "source /root/temp.sql"  
Logging to file '/tmp/temp.log'  
+------+-------+  
| id   | val   |  
+------+-------+  
|    1 | jack  |  
|    2 | robin |  
|    3 | mark  |  
+------+-------+  
Outfile disabled.  

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

复制代码代码如下:
[root@SZDB ~]# mysql -uroot -p123456 </root/temp.sql  
Logging to file '/tmp/temp.log'  
id      val  
1       jack  
2       robin  
3       mark  
Outfile disabled.  
  
#使用管道符调用SQL文件以及输出日志  
[root@SZDB ~]# mysql -uroot -p123456 </root/temp.sql >/tmp/temp.log  
[root@SZDB ~]# more /tmp/temp.log  
Logging to file '/tmp/temp.log'  
id      val  
1       jack  
2       robin  
3       mark  
Outfile disabled.  

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

复制代码代码如下:
[root@SZDB ~]# more shell_call_sql2.sh  
#!/bin/bash  
mysql -uroot -p123456 <<EOF  
source /root/temp.sql;  
select current_date();  
delete from tempdb.tb_tmp where id=3;  
select * from tempdb.tb_tmp where id=2;  
EOF  
exit;  
[root@SZDB ~]# ./shell_call_sql2.sh  
Logging to file '/tmp/temp.log'  
id      val  
1       jack  
2       robin  
3       mark  
Outfile disabled.  
current_date()  
2014-10-14  
id      val  
2       robin  

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

复制代码代码如下:
[root@SZDB ~]# more shell_call_sql3.sh  
#!/bin/bash  
cmd="select count(*) from tempdb.tb_tmp"  
cnt=$(mysql -uroot -p123456 -s -e "${cmd}")  
echo "Current count is : ${cnt}"  
exit   
[root@SZDB ~]# ./shell_call_sql3.sh   
Warning: Using a password on the command line interface can be insecure.  
Current count is : 3  
  
[root@SZDB ~]# echo "select count(*) from tempdb.tb_tmp"|mysql -uroot -p123456 -s  
3  
  
[root@SZDB ~]# more shell_call_sql4.sh  
#!/bin/bash  
id=1  
cmd="select count(*) from tempdb.tb_tmp where id=${id}"  
cnt=$(mysql -uroot -p123456 -s -e "${cmd}")  
echo "Current count is : ${cnt}"  
exit   
  
[root@SZDB ~]# ./shell_call_sql4.sh   
Current count is : 1  
  
#以上脚本演示中,作抛砖引玉只用,对于输出的结果不是很规整友好,需要进一步改善和提高。
 
转自
Shell脚本中执行sql语句操作mysql的5种方法 - mysql数据库栏目 - 红黑联盟 https://www.2cto.com/database/201707/660664.html

Shell脚本中执行sql语句操作mysql的5种方法【转】的更多相关文章

  1. SHELL脚本中执行SQL语句操作MYSQL的5种方法

    对于自动化运维,诸如备份恢复之类的,DBA经常需要将SQL语句封装到shell脚本.本文描述了在Linux环境下mysql数据库中,shell脚本下调用sql语句的几种方法,供大家参考.对于脚本输出的 ...

  2. Shell脚本中执行sql语句操作mysql

    对于自动化运维,诸如备份恢复之类的,DBA经常需要将SQL语句封装到shell脚本.本文描述了在Linux环境下mysql数据库中,shell脚本下调用sql语句的几种方法,供大家参考.对于脚本输出的 ...

  3. shell 脚本中执行SQL语句 -e "..."

    /usr/local/mysql/bin/mysql -uroot -p123456 -e " use faygo source faygo.sql select * from devqui ...

  4. 浅谈MySQL中优化sql语句查询常用的30种方法 - 转载

    浅谈MySQL中优化sql语句查询常用的30种方法 1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中使 ...

  5. shell脚本中执行sql的例子

    这个例子演示了如何在shell脚本中执行多个sql来操作数据库表. #! /bin/sh USER_HOME=/home/`whoami` . /etc/profile if [ -f ${USER_ ...

  6. Shell脚本直接执行sql语句和不显示列名

    在shell脚本编程的时候,可以通过在mysql连接命令添加-N和-e参数实现查询结果不显示列名和直接执行sql语句操作 demo $(mysql -h ${HOST} -u ${USER} -p${ ...

  7. 如何在脚本中执行SQL语句并获得结果输出?

    这里需要用到的工具叫做sqlcmd.exe, 它随SQL server的安装而安装. 该可执行程序的位置在: C:\Program Files\Microsoft SQL Server\xxx\Too ...

  8. shell脚本中执行sql命令

    1.mysql 数据库表信息 2.shell脚本(a.sh)信息 #!/bin/sh mysql -u root << myInsert insert into test.t values ...

  9. shell脚本中执行sql脚本并传递参数(mysql为例)

    1.mysql脚本文件 t.sql insert into test.t values(@name,@age); exit 2.shell脚本文件 a.sh  (为方便演示,与t.sql文件放在同一目 ...

随机推荐

  1. 洛谷 P1053 音乐会的等待 解题报告

    P1823 音乐会的等待 题目描述 \(N\)个人正在排队进入一个音乐会.人们等得很无聊,于是他们开始转来转去,想在队伍里寻找自己的熟人.队列中任意两个人\(A\)和\(B\),如果他们是相邻或他们之 ...

  2. shell(2)-&& 与 || 逻辑或与非

    test 命令测试 -常见的测试类型–测试文件状态–字符串比较–整数值比较–逻辑测试&& 如果是“前面”(真),则“后面”[ -f /var/run/dhcpd.pid ] & ...

  3. pyinstaller模块使用

    目前pip install pyinstaller已经成熟 但是还是有一些坑,郁闷了好久,记一下注意点吧. 将py脚本打包成exe文件时,如果导入了非python自带库,则需要将导入的库从site-p ...

  4. A1015. Reversible Primes

    A reversible prime in any number system is a prime whose "reverse" in that number system i ...

  5. CRC-16的原理和实现

    CRC的全称为Cyclic Redundancy Check,中文名称为循环冗余校验.它是一类重要的线性分组码,编码和解码方法简单,检错和纠错能力强,在通信领域广泛地用于实现差错控制.实际上,除 数据 ...

  6. weichat Small 程序

    微信小程序教程用什么语言  ? 答:php java html5 css, 咫尺微页很棒可以自制 html 它是一套自有的语言(暂且叫 WeLang),使用 WeLang 开发出来的页面. 一个微信页 ...

  7. 关于mysql数据库优化

    关于mysql数据库优化 以我之愚见,数据库的优化在于优化存储和查询速度 目前主要的优化我认为是优化查询速度,查询速度快了,提高了用户的体验 我认为优化主要从两方面进行考虑, 优化数据库对象, 优化s ...

  8. python Elasticsearch5.x使用

    文档:http://elasticsearch-py.readthedocs.io/en/master/ Elasticsearch官方API文档:https://www.elastic.co/gui ...

  9. 用Shell编写的俄罗斯方块代码

    用Shell编写的俄罗斯方块代码 不得不承认任何一门语言玩6了,啥都能搞出来啊,竟然用Shell编写出来了一个俄罗斯方块游戏的代码,很有意思,这个代码不是我写出来的,不过大家可以下载一下在window ...

  10. mvn依赖冲突

    https://www.cnblogs.com/twoheads/p/10082453.html