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 </root/temp.sql
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 <<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@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 </root/temp.sql
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 <<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@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/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- MySQL資料庫中SQL語句分幾類?MySql資料庫
- 通過shell指令碼批量操作mysql資料庫指令碼MySql資料庫
- 資料庫常用操作SQL語句資料庫SQL
- MySQL 5.5 執行指令碼中的SQLMySql指令碼
- Oracle資料庫SQL語句執行過程Oracle資料庫SQL
- Shell 指令碼語句指令碼
- mysql的sql語句執行流程MySql
- Shell多執行緒備份資料庫的指令碼執行緒資料庫指令碼
- MySQL資料庫詳解(一)SQL查詢語句是如何執行的?MySql資料庫
- 1.4 資料庫和常用SQL語句(正文)——MySQL資料庫命令和SQL語句資料庫MySql
- MySQL資料庫備份的shell指令碼MySql資料庫指令碼
- 建立管理MySQL資料庫的shell指令碼MySql資料庫指令碼
- Linux shell:執行shell指令碼的幾種方式Linux指令碼
- 一條SQL語句在MySQL中如何執行的MySql
- mysql便於管理的幾個shell指令碼MySql指令碼
- mysql執行sql語句過程MySql
- Liunx備份mysql資料庫的shell指令碼MySql資料庫指令碼
- shell 操作mysql資料庫MySql資料庫
- 一條sql語句在mysql中是如何執行的MySql
- 一條 SQL 語句在 MySQL 中是如何執行的?MySql
- DM聯機執行SQL語句進行資料庫備份SQL資料庫
- Linux中執行Shell指令碼的方式(三種方法)Linux指令碼
- 資料庫操作語句資料庫
- mysql如何跟蹤執行的sql語句MySql
- 檢視mysql正在執行的SQL語句MySql
- 【資料庫】SQL語句資料庫SQL
- cmd命令列下用命令執行SQL指令碼到SQL Server資料庫中命令列SQL指令碼Server資料庫
- MySQL cron定時執行SQL語句MySql
- 一個 MySQL sql 語句執行順序帶來的 bugMySql
- idea內建資料庫 + sql語句庫表操作Idea資料庫SQL
- bash shell指令碼執行方法總結指令碼
- 執行shell指令碼指令碼
- mysql資料庫sql語句基礎知識MySql資料庫
- MySQL資料庫:7、SQL常用查詢語句MySql資料庫
- MySql定位執行效率較低的SQL語句MySql
- 資料庫常用的sql語句大全--sql資料庫SQL
- 資料庫環境中的shell指令碼應用資料庫指令碼
- 資料庫常用sql 語句資料庫SQL