日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

shell脚本mysql_Shell脚本中执行sql语句操作MySQL数据库的几个方法

發(fā)布時間:2025/3/19 数据库 15 豆豆
生活随笔 收集整理的這篇文章主要介紹了 shell脚本mysql_Shell脚本中执行sql语句操作MySQL数据库的几个方法 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

實驗如下:

[root@idb1 ~]# more /etc/issue

CentOS release 6.5 (Final)

Kernel \r on an \m

mysql> show variables like 'version';

+---------------+------------+

| Variable_name | Value ? ? ?|

+---------------+------------+

| version ? ? ? | 5.6.27-log |

+---------------+------------+

1 row in set (0.00 sec)

1、將SQL語句直接嵌入到shell腳本文件中

[root@idb1 ~]# cat shell_example01.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 -umdba -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@idb1 ~]# chmod +x shell_example01.sh

[root@idb1 ~]# sh shell_example01.sh

Warning: Using a password on the command line interface can be insecure.

Logging to file '/tmp/temp.log'

+------+-------+

| id ? | val ? |

+------+-------+

| ? ?1 | jack ?|

| ? ?2 | robin |

| ? ?3 | mark ?|

+------+-------+

Outfile disabled.

2、命令行調(diào)用單獨的SQL文件例子:

[root@idb1 ~]# cat 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,'duansf'),(2,'liuyb'),(3,'jack');

select * from tb_tmp;

notee

quit

[root@idb1 ~]# mysql -umdba -p -e "source /root/temp.sql"

Enter password:

Logging to file '/tmp/temp.log'

+------+--------+

| id ? | val ? ?|

+------+--------+

| ? ?1 | duansf |

| ? ?2 | liuyb ?|

| ? ?3 | jack ? |

+------+--------+

Outfile disabled.

#使用管道符調(diào)用SQL文件以及輸出日志

[root@idb1 ~]# mysql -umdba -p

Enter password:

Logging to file '/tmp/temp.log'

id ? ? ?val

1 ? ? ? duansf

2 ? ? ? liuyb

3 ? ? ? jack

Outfile disabled.

4、shell腳本中MySQL提示符下調(diào)用SQL命令例子:

[root@idb1 ~]# cat shell_example02.sh

#!/bin/bash

mysql -umdba -pdsf0723 <

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@idb1 ~]# sh shell_example02.sh

Warning: Using a password on the command line interface can be insecure.

Logging to file '/tmp/temp.log'

id ? ? ?val

1 ? ? ? duansf

2 ? ? ? liuyb

3 ? ? ? jack

Outfile disabled.

current_date()

2017-03-14

id ? ? ?val

2 ? ? ? liuyb

來自 “ ITPUB博客 ” ,鏈接:http://blog.itpub.net/15498/viewspace-2135352/,如需轉(zhuǎn)載,請注明出處,否則將追究法律責(zé)任。

總結(jié)

以上是生活随笔為你收集整理的shell脚本mysql_Shell脚本中执行sql语句操作MySQL数据库的几个方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。