您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關(guān)如何實(shí)現(xiàn)mysql/mongo導(dǎo)出到本地文件的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。
一. mongo導(dǎo)出到本地
#!/bin/bash
set -e
#變量聲明
database=""
table=""
columns=""
query=""
datafile_path=""
#函數(shù)定義
usage(){
echo " usage(): $0 [-d 數(shù)據(jù)庫(kù)名] [-t 表名 ] [-c 列名] [-q query條件 ] " 1>&2;
exit 1;
}
#執(zhí)行
#檢測(cè)參數(shù) 給 對(duì)應(yīng)變量賦值
while getopts "d:t:c:q:" opt
do
case "$opt" in
d) database=$OPTARG ;;
#t) table=$(echo $OPTARG| tr '[A-Z]' '[a-z]') ;;
t) table=$OPTARG ;;
c) columns=$OPTARG ;;
q) query=$OPTARG ;;
*) usage;;
esac
done
shift $[ $OPTIND - 1 ]
echo "mongoexport --host $mongo_ip --port $mongo_port -u $mongo_user -p $mongo_password --authenticationDatabase=admin --db ${database} --collection ${table} --readPreference='secondaryPreferred' -f ${columns} --query "{${query}}" --type=csv"
mongoexport --host $mongo_ip --port $mongo_port -u $mongo_user -p $mongo_password --authenticationDatabase=admin --db ${database} --collection ${table} --readPreference='secondaryPreferred' -f ${columns} --query "{${query}}" --type=csv | tail -n+2 |sed 's/ObjectID[(]\([0-9a-zA-Z-]\+\)[)]/\1/i' > ${table}.csv
二. mysql 導(dǎo)出到本地
#!/bin/bash
set -e
#變量聲明
database=""
table=""
columns=""
where=""
datafile_path=""
#函數(shù)定義
usage(){
echo " usage(): $0 [-d 數(shù)據(jù)庫(kù)名] [-t 表名 ] [-c 列名] [-w 過濾條件 ] " 1>&2;
exit 1;
}
#執(zhí)行
#檢測(cè)參數(shù) 給 對(duì)應(yīng)變量賦值
while getopts "d:t:c:w:" opt
do
case "$opt" in
d) database=$OPTARG ;;
t) table=$(echo $OPTARG| tr '[a-z]' '[A-Z]') ;;
c) columns=$OPTARG ;;
w) where=$OPTARG ;;
*) usage;;
esac
done
shift $[ $OPTIND - 1 ]
#echo "database=${database} , table=${table} , columns=${columns} , where=${where}"
#數(shù)據(jù)庫(kù)鏈接
mysql_bin="mysql -h$mysql_ip -P$mysql_port -u$mysql_user -p$mysql_password --database=${database}"
#生成SQL語句
SQL_STR=""
if [ ${where} == "" ];then
SQL_STR="select "${columns}" from "${table}";"
else
SQL_STR="select "${columns}" from "${table}" where "${where}";"
fi
echo ${SQL_STR}
#執(zhí)行SQL語句 導(dǎo)入到本地文件
$mysql_bin -N -e "${SQL_STR}" > ${table}.csv
感謝各位的閱讀!關(guān)于“如何實(shí)現(xiàn)mysql/mongo導(dǎo)出到本地文件”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。