Shell脚本中执行mysql的几种方式(转)

对于自动化运维,诸如备份恢复之类的,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.

4shell脚本中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

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

复制代码 代码如下:

[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

转自:http://blog.sina.com.cn/s/blog_4a80a5730101mbeg.html

下面附上通过shell命令行非交互式的操作数据库的方法:

mysql -hhostname -Pport -uusername -ppassword -e 相关mysql的sql语句,不用在mysql的提示符下运行mysql,即可以在shell中操作mysql的方法。

#!/bin/bash

HOSTNAME="192.168.111.84"  #数据库信息

PORT="3306"

USERNAME="root"

PASSWORD=""

DBNAME="test_db_test"  #数据库名称

TABLENAME="test_table_test" #数据库中表的名称

#创建数据库

create_db_sql="create database IF NOT EXISTS ${DBNAME}"

mysql -h${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} -e
"${create_db_sql}"

#创建表

create_table_sql="create table IF NOT EXISTS ${TABLENAME} ( name
varchar(20), id int(11) default 0 )"

mysql -h${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} ${DBNAME} -e
"${create_table_sql}"

#插入数据

insert_sql="insert into ${TABLENAME} values('billchen',2)"

mysql -h${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} ${DBNAME} -e
"${insert_sql}"

#查询

select_sql="select * from ${TABLENAME}"

mysql -h${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} ${DBNAME} -e
"${select_sql}"

#更新数据

update_sql="update ${TABLENAME} set id=3"

mysql -h${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} ${DBNAME} -e
"${update_sql}"

mysql -h${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} ${DBNAME} -e
"${select_sql}"

#删除数据

delete_sql="delete from ${TABLENAME}"

mysql -h${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} ${DBNAME} -e
"${delete_sql}"

mysql -h${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} ${DBNAME} -e
"${select_sql}"

(转)
http://www.cnblogs.com/zhuawang/p/5179838.html

Shell脚本中执行mysql的几种方式(转)的更多相关文章

  1. shell 脚本中执行mysql语句

    通过hash建表之后,表的数据量巨大2048,那怎么去验证表是否建成功呢? 逻辑生成表名这部分就不写了.只要能建表成功,这部分的脚本肯定是有的.那么怎么在shell中执行selec查询并返回呢 只要在 ...

  2. shell脚本中执行mysql sql脚本文件并传递参数

    1 shell 文件内容替换 sed是一个很好的文件处理工具,本身是一个管道命令,主要是以行为单位进行处理,可以将数据行进行替换.删除.新增.选取等特定工作,下面先了解一下sed的用法. 调用sed命 ...

  3. shell脚本中执行mysql命令

    1.mysql -hhostname -uuser -ppsword -e "mysql_cmd" 2. mysql -hhostname -uuser -ppsword < ...

  4. shell脚本中执行mysql 语句,去除warning using a password on the command line interface can be insecure信息

    方法二:使用mysql参数的方法 mysql -u$user -p$pass -D $db -e "select host from user;"当然,可以通过将传参的方式来传递 ...

  5. shell脚本循环执行mysql语句

    参考资料:Shell脚本中执行mysql语句 需求:数据库里有张数据表存储的是用户对电影的评价(user_id movie_id rating time),但是我现在要每部电影的总评分. 解决方法: ...

  6. Shell脚本中执行sql语句操作mysql的5种方法【转】

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

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

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

  8. 转 mysql mysql命令行中执行sql的几种方式总结

    https://www.jb51.net/article/96394.htm 1.直接输入sql执行 MySQL> select now(); +---------------------+ | ...

  9. shell脚本中执行另一个shell脚本

    分类: 可以在一个shell脚本中执行另一个shell脚本(或非可执行文件,主要用于取得一些变量的值),方法是: . 文件名(包括路径) 或 变量=文件名(包括路径) . $变量   注意,圆点后面有 ...

随机推荐

  1. VMWARE虚拟机CentOS6.4系统使用主机无线网卡上网的三种方法介绍

    转自:http://www.jb51.net/network/98820.html 如何真正的实现VMWARE虚拟机CentOS6.4系统使用主机无线网卡上网   环境:WIN7旗舰版,台式机,U盘无 ...

  2. PRD产品需求文档

    什么是PRD? PRD是Product Requirement Document的英文缩写,即产品需求文档的意思.PRD昰产品流程中的最后一步工作,是将原型中的功能.界面具象化描述,是提交给设计(UI ...

  3. Android LayoutInflater详解(转)

    在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById().不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且实例 ...

  4. 【JSON 注解】JSON循环引用2----JSON注解@JsonIgnoreProperties+JAVA关键字transient+后台对象与JSON数据的格式互相转化

    接着来说这个JSON循环引用的问题: 关于JSON格式的转化,其实关键就是这几个依赖: <!-- json --> <!-- 1号 --> <dependency> ...

  5. winmail服务器启动失败 无法启动

    1.解决句柄问题:打开命令行:开始 -> 运行-> 输入 cmd -> 确定.切换命令目录至winmail的服务目录,我的是:E:\htdocs\Winmail\server\> ...

  6. 20145223《Java程序程序设计》第10周学习总结

    20145223<Java网络编程> 一.Java的网络编程 ·网络编程是指编写运行在多个设备(计算机)的程序,这些设备都通过网络连接起来. ·java.net包中J2SE的API包含有类 ...

  7. storm安装笔记以及提交拓扑任务

    Storm -- Distributed and fault-tolerant realtime computation 这是一个分布式的.容错的实时计算系统 把Storm依赖组件的版本贴出来供各位参 ...

  8. 关于fragment保存变量的问题

    之前遇到一个问题:某个fragment在打开改变状态好后,然后关闭,要求是再次打开时该状态依然保留 这时候求度娘.自己解决问题后,现在整理过程如下: 1.新定义Bundle saveState=new ...

  9. ORACLE使用EXPDP和IMPDP数据泵进行导出导入的方法

    ORACLE使用EXPDP和IMPDP数据泵进行导出导入的方法 (2010-05-28 12:54:34) http://blog.sina.com.cn/s/blog_67d41beb0100ixn ...

  10. unity gizmo绘制圆形帮助调试

    using UnityEngine; using System.Collections; using System; public class LearnGrazio : MonoBehaviour ...