Shell脚本中执行sql语句操作mysql的5种方法【转】
对于自动化运维,诸如备份恢复之类的,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文件
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文件
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
#!/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脚本中变量输入与输出
#!/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种方法【转】的更多相关文章
- SHELL脚本中执行SQL语句操作MYSQL的5种方法
对于自动化运维,诸如备份恢复之类的,DBA经常需要将SQL语句封装到shell脚本.本文描述了在Linux环境下mysql数据库中,shell脚本下调用sql语句的几种方法,供大家参考.对于脚本输出的 ...
- Shell脚本中执行sql语句操作mysql
对于自动化运维,诸如备份恢复之类的,DBA经常需要将SQL语句封装到shell脚本.本文描述了在Linux环境下mysql数据库中,shell脚本下调用sql语句的几种方法,供大家参考.对于脚本输出的 ...
- shell 脚本中执行SQL语句 -e "..."
/usr/local/mysql/bin/mysql -uroot -p123456 -e " use faygo source faygo.sql select * from devqui ...
- 浅谈MySQL中优化sql语句查询常用的30种方法 - 转载
浅谈MySQL中优化sql语句查询常用的30种方法 1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中使 ...
- shell脚本中执行sql的例子
这个例子演示了如何在shell脚本中执行多个sql来操作数据库表. #! /bin/sh USER_HOME=/home/`whoami` . /etc/profile if [ -f ${USER_ ...
- Shell脚本直接执行sql语句和不显示列名
在shell脚本编程的时候,可以通过在mysql连接命令添加-N和-e参数实现查询结果不显示列名和直接执行sql语句操作 demo $(mysql -h ${HOST} -u ${USER} -p${ ...
- 如何在脚本中执行SQL语句并获得结果输出?
这里需要用到的工具叫做sqlcmd.exe, 它随SQL server的安装而安装. 该可执行程序的位置在: C:\Program Files\Microsoft SQL Server\xxx\Too ...
- shell脚本中执行sql命令
1.mysql 数据库表信息 2.shell脚本(a.sh)信息 #!/bin/sh mysql -u root << myInsert insert into test.t values ...
- shell脚本中执行sql脚本并传递参数(mysql为例)
1.mysql脚本文件 t.sql insert into test.t values(@name,@age); exit 2.shell脚本文件 a.sh (为方便演示,与t.sql文件放在同一目 ...
随机推荐
- java freemarker导出word时添加或勾选复选框
最近项目导出word碰到一个需求,要求根据数据动态的决定word里的复选框是否勾选, 公司导出word用的是freemarker,相比较其他技术,freemarker可以很容易的控制输出样式, 在wo ...
- I/O多路复用详解
要想完全理解I/O多路复用,需先要了解I/O模型: 一.五种I/O模型 1.阻塞I/O模型 最流行的I/O模型是阻塞I/O模型,缺省情形下,所有套接口都是阻塞的.我们以数据报套接口为例来讲解此模型(我 ...
- Luogu 1979 NOIP 2013 华容道(搜索,最短路径)
Luogu 1979 NOIP 2013 华容道(搜索,最短路径) Description 小 B 最近迷上了华容道,可是他总是要花很长的时间才能完成一次.于是,他想到用编程来完成华容道:给定一种局面 ...
- JS数组冒泡排序&去重
冒泡排序: var a = [2,1,4,3,6,5]; for(var d = 0 ; d< a.length; d++){ for(var b = d+1; b < a.length; ...
- 洛谷P1315 观光公交
SB贪心......暴露了我代码能力巨弱的本质. 题面 解:首先我们应该想到DP(但是我想到了贪心......) 然后分析题目本质,每个点有个限制,最早开走时间不得早于最晚上车时间. 然后我们就可以把 ...
- 二分查找、two points、排序
二分查找 1.查找某元素.循环条件 low <= high,最终结果位mid, 如果查询失败则返回-1. int binSearch(int num[], int low, int high, ...
- 使用sass切图
明日复明日,明日何其多!我生待明日,万事成蹉跎!ruby挺可爱的.没有进入工程项目中使用sass. 尝试是一件快乐的事情.在页面中直接引进编译出来的css即可.
- python基础练习题30道
1.执行python脚本的两种方式 答:1>可以在python /home/xxxx.py 2>cd /home ./xxxx.py 因为py脚本里面指定了python解释器的位置 ...
- sed命令使用介绍(转载)
sed命令介绍 (转自:http://www.cnblogs.com/ggjucheng/archive/2013/01/13/2856901.html) 简介 sed 是一种在线编辑器,它一次处理一 ...
- Linux下Shell去除空行的方法
1.用grep命令 grep -v “^$” 文件名 2.用sed命令 cat 文件名 | sed ‘/^$/d' 3.用awk命令 cat 文件名 | awk ‘{if($0!=”")pr ...