MongoDB之数据导出

今天要导出服务器上MongoDB的数据,期间遇到多个问题,因此记录一下。

1. Auth Failed

使用图形化工具能够连接到远程MongoDB,但是使用mongo命令连接不上。

错误信息

这里写图片描述

出错原因

远程MongoDB是3.x,本地版本是2.x,版本太低,所以需要把本地MongoDB版本升级或者重装3.x版本。

2. Authentication failed

错误信息

这里写图片描述

出错原因

MongoDB自3.x开始开启了SCRAM-SHA-1认证,需要在命令中带上--authenticationMechanism='SCRAM-SHA-1'

正确的导出命令

./mongoexport --authenticationDatabase=db -h 127.0.0.1 --port 27017 -u admin -p 'admin' --authenticationMechanism='SCRAM-SHA-1' -d db -c Place -f ident -q '{published:true}' --type=csv -o ident.csv

其中某些参数含义:
authenticationDatabase:数据库名
authenticationMechanism:使用SCRAM-SHA-1认证
-c:代表某个Collection名
-f:代表要输出的Collection的字段名,多个字段时使用分割
-q:代表过滤条件,是MongoDB的过滤语法

另附导入命令

./mongoexport --authenticationDatabase=db -h 127.0.0.1 --port 27017 -u username -p password --authenticationMechanism='SCRAM-SHA-1' -d db -c CollectonName -o name.csv

猜你喜欢

转载自blog.csdn.net/haiyanggeng/article/details/80389628