共計(jì) 1688 個(gè)字符,預(yù)計(jì)需要花費(fèi) 5 分鐘才能閱讀完成。
這篇文章給大家分享的是有關(guān)如何實(shí)現(xiàn) mysql/mongo 導(dǎo)出到本地文件的內(nèi)容。丸趣 TV 小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨丸趣 TV 小編過來看看吧。
一. mongo 導(dǎo)出到本地
#!/bin/bash
set -e
#變量聲明
database=
table=
columns=
query=
datafile_path=
#函數(shù)定義
usage(){
echo usage(): $0 [-d 數(shù)據(jù)庫名] [-t 表名] [-c 列名] [-q query 條件] 1
exit 1;
}
#執(zhí)行
#檢測參數(shù) 給 對應(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ù)庫名] [-t 表名] [-c 列名] [-w 過濾條件] 1
exit 1;
}
#執(zhí)行
#檢測參數(shù) 給 對應(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ù)庫鏈接
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)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!