shell腳本批量執(zhí)行指定路徑下sql腳本的實(shí)現(xiàn)
1. 場(chǎng)景描述
Linux環(huán)境下通過(guò)shell腳本批量執(zhí)行指定目錄下所有sql語(yǔ)句,用來(lái)建表建庫(kù),初始化項(xiàng)目sql等。
Linux shell在線格式化:https://tool.lu/shell/
2. 創(chuàng)建sql
創(chuàng)建contract_ddl.sql
-- 創(chuàng)建數(shù)據(jù)庫(kù)contract_user CREATE DATABASE `contract_user` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci; -- 創(chuàng)建合同表contract DROP TABLE IF EXISTS `contract`; CREATE TABLE `contract` ?( ? `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID主鍵', ? `name` varchar(64) NOT NULL COMMENT '合同名稱', ? `code` varchar(64) NOT NULL COMMENT '合同編號(hào)', ? `deleted` tinyint NOT NULL DEFAULT 0 COMMENT '是否刪除 0 未刪除 1 刪除 默認(rèn)是0', ? `create_id` bigint(20) NOT NULL DEFAULT 0 COMMENT '創(chuàng)建人賬號(hào)id', ? `create_time` timestamp(0) NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '創(chuàng)建時(shí)間', ? `update_id` bigint(20) NOT NULL DEFAULT 0 COMMENT '更新人賬號(hào)id', ? `update_time` timestamp(0) NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP?? ?COMMENT '更新時(shí)間', ?? ? ? PRIMARY KEY (`id`) USING BTREE, ? index `idx_code_name`(`code`,`name`) USING BTREE ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '合同表' ROW_FORMAT = Dynamic;
創(chuàng)建template_ddl.sql
-- 創(chuàng)建數(shù)據(jù)庫(kù)template_user CREATE DATABASE `template_user` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci; -- 模板設(shè)置表template DROP TABLE IF EXISTS `template`; CREATE TABLE `template` ?( ? `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID主鍵', ? `name` bigint(20) NOT NULL COMMENT '模板名稱', ? `code` bigint(20) NOT NULL COMMENT '模板編碼', ? `deleted` tinyint NOT NULL DEFAULT 0 COMMENT '是否刪除 0 未刪除 1 刪除 默認(rèn)是0', ? `create_id` bigint(20) NOT NULL DEFAULT 0 COMMENT '創(chuàng)建人賬號(hào)id', ? `create_time` timestamp(0) NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '創(chuàng)建時(shí)間', ? `update_id` bigint(20) NOT NULL DEFAULT 0 COMMENT '更新人賬號(hào)id', ? `update_time` timestamp(0) NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP?? ?COMMENT '更新時(shí)間', ?? ? ? PRIMARY KEY (`id`) USING BTREE, ? index `idx_code_name`(`code`, `name`) USING BTREE ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '模板表' ROW_FORMAT = Dynamic;
3. 創(chuàng)建腳本
3.1 方式一
方式一需要手動(dòng)指定sql全路徑名稱,相對(duì)比較麻煩,僅供參考。
#!/bin/sh
start_date=`date '+%Y%m%d-%H%M%S'`
echo $start_date ${USER} "execute ddl start..."
# mysql
shost=127.0.0.1
sport=3306
suser=root
spwd=123456
# path,sql放入ddl下
sqlpath="source /home/ddl/"
sqlsource="${sqlpath}contract_ddl.sql;${sqlpath}template_ddl.sql;"
# 執(zhí)行sql腳本,這里會(huì)有告警,不影響執(zhí)行,提示直接輸入密碼不安全,去掉-p后的spwd,執(zhí)行時(shí)輸入安全
# Warning: Using a password on the command line interface can be insecure.
mysql -h$shost -p$sport -u$suser -p$spwd -e"$sqlsource"
# end
end_date=`date '+%Y%m%d-%H%M%S'`
echo $end_date ${USER} "execute ddl end..."3.2 方式二
方式二只需要指定sql路徑即可,通過(guò)shell遍歷,相對(duì)方便。
#!/bin/bash
#execute all script in specified directory
MYDATE=$(date +%F'-'%T'-'%w)
MYSQL_PATH=/tmp/scripts #指定的目錄
LOG_FILE=/tmp/scripts/exec_${MYDATE}.log
confirm=
db_name=
db_pass=
for file in ${MYSQL_PATH}/*; do
?? ?if [ -f "$file" ]; then
?? ??? ?postfix=$(echo $file | awk -F'.' '{print "."$NF}')
?? ??? ?if [ $postfix = ".sql" ]; then
?? ??? ??? ?if [ ! $db_name ]; then #如果沒有指定數(shù)據(jù)庫(kù)
?? ??? ??? ??? ?read -p "請(qǐng)輸入數(shù)據(jù)庫(kù)名:" db_name
?? ??? ??? ??? ?read -p "你輸入的數(shù)據(jù)名是【$db_name】,確認(rèn)繼續(xù)請(qǐng)輸入--yes--: " confirm
?? ??? ??? ?fi
?? ??? ??? ?if [ "$confirm" = "yes" ] && [ -n $confirm ]; then
?? ??? ??? ??? ?if [ ! $db_pass ]; then #如果沒有設(shè)置密碼
?? ??? ??? ??? ??? ?stty -echo #密碼輸入保護(hù)關(guān)閉顯示
?? ??? ??? ??? ??? ?read -p "請(qǐng)輸入數(shù)據(jù)庫(kù)密碼:" db_pass
?? ??? ??? ??? ??? ?echo -e "\n"
?? ??? ??? ??? ??? ?stty echo
?? ??? ??? ??? ?fi
?? ??? ??? ??? ?mysql -uroot -p$db_pass -P3306 --default-character-set=utf8 ${db_name} <$file >&error.log
?? ??? ??? ??? ?echo $file
?? ??? ??? ??? ?echo -e "\n===========$file=============\n" >>${LOG_FILE}
?? ??? ??? ??? ?cat error.log >>${LOG_FILE} ? #輸出執(zhí)行日志
?? ??? ??? ??? ?error=$(grep ERROR error.log) #讀取錯(cuò)誤日志信息
?? ??? ??? ??? ?if [ -n "$error" ]; then #如果有錯(cuò)誤就退出程序
?? ??? ??? ??? ??? ?echo $error
?? ??? ??? ??? ??? ?exit
?? ??? ??? ??? ?fi
?? ??? ??? ?else
?? ??? ??? ??? ?echo "您已經(jīng)取消操作!"
?? ??? ??? ??? ?exit
?? ??? ??? ?fi
?? ??? ?fi
?? ?fi
done到此這篇關(guān)于shell腳本批量執(zhí)行指定路徑下sql腳本的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)shell批量執(zhí)行sql腳本內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
linux?shell字符串截取的詳細(xì)總結(jié)(實(shí)用!)
在開發(fā)的時(shí)候經(jīng)常會(huì)自行寫一些小的腳本,其中就用到截取字符串的操作,這篇文章主要給大家介紹了關(guān)于linux?shell字符串截取的詳細(xì)方法,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07
分享一個(gè)實(shí)用的iptables腳本(各種過(guò)濾寫法參考)
這篇文章主要介紹了分享一個(gè)實(shí)用的iptables腳本(各種過(guò)濾寫法參考),需要的朋友可以參考下2014-04-04
shell腳本正則匹配文件中的Email并寫入到文件中代碼分享
有時(shí)我們會(huì)處理日志文件,或其他文本文件,并將里面含有的Email讀取出來(lái),可以利用shell處理文件的方法來(lái)讀取2014-04-04
shell腳本學(xué)習(xí)指南[二](Arnold Robbins & Nelson H.F. Beebe著)
這篇文章主要介紹了shell腳本學(xué)習(xí)指南[二](Arnold Robbins & Nelson H.F. Beebe著),需要的朋友可以參考下2014-02-02
一鍵配置CentOS iptables防火墻的Shell腳本分享
這篇文章主要介紹了一鍵配置CentOS iptables防火墻Shell腳本分享,可保存到一個(gè)腳本文件中,在新安裝的CentOS系統(tǒng)時(shí)一條命令搞定iptables配置,需要的朋友可以參考下2014-07-07

