溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何實(shí)現(xiàn)mysql/mongo導(dǎo)出到本地文件

發(fā)布時(shí)間:2021-12-20 09:47:21 來源:億速云 閱讀:183 作者:小新 欄目:數(shù)據(jù)庫(kù)

這篇文章給大家分享的是有關(guān)如何實(shí)現(xiàn)mysql/mongo導(dǎo)出到本地文件的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。

一. mongo導(dǎo)出到本地

  1. #!/bin/bash

  2. set -e



  3. #變量聲明

  4. database=""

  5. table=""

  6. columns=""

  7. query=""


  8. datafile_path=""



  9. #函數(shù)定義

  10. usage(){

  11. echo " usage(): $0 [-d 數(shù)據(jù)庫(kù)名] [-t 表名 ] [-c 列名] [-q query條件 ] " 1>&2;

  12. exit 1;

  13. }


  14. #執(zhí)行


  15. #檢測(cè)參數(shù) 給 對(duì)應(yīng)變量賦值

  16. while getopts "d:t:c:q:" opt

  17. do

  18. case "$opt" in

  19. d) database=$OPTARG ;;

  20. #t) table=$(echo $OPTARG| tr '[A-Z]' '[a-z]') ;;

  21. t) table=$OPTARG ;;

  22. c) columns=$OPTARG ;;

  23. q) query=$OPTARG ;;

  24. *) usage;;

  25. esac

  26. done

  27. shift $[ $OPTIND - 1 ]


  28. 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"


  29. 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)出到本地

  1. #!/bin/bash

  2. set -e



  3. #變量聲明

  4. database=""

  5. table=""

  6. columns=""

  7. where=""


  8. datafile_path=""



  9. #函數(shù)定義

  10. usage(){

  11. echo " usage(): $0 [-d 數(shù)據(jù)庫(kù)名] [-t 表名 ] [-c 列名] [-w 過濾條件 ] " 1>&2;

  12. exit 1;

  13. }


  14. #執(zhí)行


  15. #檢測(cè)參數(shù) 給 對(duì)應(yīng)變量賦值

  16. while getopts "d:t:c:w:" opt

  17. do

  18. case "$opt" in

  19. d) database=$OPTARG ;;

  20. t) table=$(echo $OPTARG| tr '[a-z]' '[A-Z]') ;;

  21. c) columns=$OPTARG ;;

  22. w) where=$OPTARG ;;

  23. *) usage;;

  24. esac

  25. done

  26. shift $[ $OPTIND - 1 ]



  27. #echo "database=${database} , table=${table} , columns=${columns} , where=${where}"


  28. #數(shù)據(jù)庫(kù)鏈接

  29. mysql_bin="mysql -h$mysql_ip -P$mysql_port -u$mysql_user -p$mysql_password --database=${database}"


  30. #生成SQL語句

  31. SQL_STR=""

  32. if [ ${where} == "" ];then

  33. SQL_STR="select "${columns}" from "${table}";"

  34. else

  35. SQL_STR="select "${columns}" from "${table}" where "${where}";"

  36. fi

  37. echo ${SQL_STR}


  38. #執(zhí)行SQL語句 導(dǎo)入到本地文件

  39. $mysql_bin -N -e "${SQL_STR}" > ${table}.csv

感謝各位的閱讀!關(guān)于“如何實(shí)現(xiàn)mysql/mongo導(dǎo)出到本地文件”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

向AI問一下細(xì)節(jié)

免責(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)容。

AI