Shell脚本中执行sql语句操作MySQL数据库的几个方法
发表于:2025-11-08 作者:千家信息网编辑
千家信息网最后更新 2025年11月08日,实验如下:[root@idb1 ~]# more /etc/issueCentOS release 6.5 (Final)Kernel \r on an \mmysql> show variables
千家信息网最后更新 2025年11月08日Shell脚本中执行sql语句操作MySQL数据库的几个方法实验如下:
[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、命令行调用单独的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.
#使用管道符调用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提示符下调用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
[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、命令行调用单独的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.
#使用管道符调用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提示符下调用SQL命令例子:
[root@idb1 ~]# cat shell_example02.sh
#!/bin/bash
mysql -umdba -pdsf0723 <
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
文件
脚本
例子
命令
语句
提示符
日志
管道
实验
提示
输出
数据
数据库
方法
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
c 判断数据库是否为空
怎么做游戏数据库
业务服务器文件包选多大
安徽生鲜配送软件开发
服务器管理员职职责
软件开发过程中的变更流程
福州仓山万达软件开发
rust 数据库实现
火影忍者忍者出击服务器连不上
2019网络安全大赛ctf
it软件技术管理软件开发
理正excel数据库对接
青鸟下载软件开发
吉林省开天通信网络技术有限公司
贵阳学网络技术中专学校
三门峡软件开发商家
太阳能控制器软件开发
水果网络技术创作的原声
数据库查询utl文件
计算机网络技术基础pdf
上海系统软件开发定制费用
查看服务器防火墙
如何让客户服务器运行更快
公司服务器做文件管理系统
计算机三级题库网络技术上机
网络安全法中明确国家实行
怎样下载万方数据库的文献
socket 丢失数据库
梦幻2008服务器等级分布
北京进口软件开发经历