shellmysql执⾏sql⽂件_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}
shell脚本返回执行结果# 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
logging to file '/tmp/temp.log'
id      val
1      jack
2      robin
3      mark
outfile disabled.
#使⽤管道符调⽤sql⽂件以及输出⽇志
[root@szdb ~]# mysql -uroot -p123456 /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 <
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
#以上脚本演⽰中,作抛砖引⽟只⽤,对于输出的结果不是很规整友好,需要进⼀步改善和提⾼。如您对本⽂有疑问或者有任何想说的,请点击进⾏留⾔回复,万千⽹友为您解惑!

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。