通過shell腳本批量導入sql文件

Mysql批量導入sql文件

今天剛接觸公司項目,需要創建數據庫,但是sql文件有幾十個,而navicat又不能批量導入,一個一個文件導入簡直要死了,乾脆自己寫了一個腳本。

如下,如果對你有幫助,歡迎點個贊,收藏一下再走。

GitHub 地址: https://github.com/Mcliuyi/script

#/bin/bash

sql_path=$1;
if [ ! -n "$sql_path" ];
then

	echo "請輸入sql所在目錄: ";
	read sql_path;
fi;

if [ ! -d "$sql_path" ];
then
	echo "目錄不存在";
	exit 1;
else
	echo "目錄存在";
fi;

echo "請輸入需要導入的數據庫名: "
read db;

eval " echo 'use $db;' >  batch.sql"

for file in $(find $sql_path -name "*.sql" | sort)
do
	eval "echo 'source $file;' >> batch.sql";
done;

echo "批量導入sql文件已生成,是否需要自動導入至mysql: (yes/no) 默認no"
read status;

if [ $status == "yes" ]
then
	read -p "請輸入數據庫賬號: " username;
	if [ -z "$username" ];
	then
		exit 1;	
	fi;
	read -p "請輸入數據庫密碼: " pwd;
	if [ -z "$pwd" ];then
		exit 1;
	fi;
	read -p "請輸入數據庫IP:(默認 127.0.0.1) " ip;
	if [ -z "$ip" ];then
		ip=127.0.0.1;
	fi;	
	read -p "請輸入端口:(默認: 3306) " port;
	if [ -z "$port" ];then
		port=3306;
	fi;
else
	echo "退出";
	exit 1;
fi

eval "mysql -u $username -p$pwd -h $ip -P $port  --max_allowed_packet=1048576 --net_buffer_length=16384 < batch.sql"
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章