使用标准的Mongo URI进行mongodump

18

mongo 客户端可以使用标准的 URI 进行连接:

mongo mongodb://<dbuser>:<dbpassword>@<server>:<port>/<db>

然而,mongodump 似乎需要一种繁琐的语法将其拆分为不同的参数:

Translated text:

然而,mongodump 似乎需要一种繁琐的语法将其拆分为不同的参数:

mongodump -u dbuser -p dbpassword -h server -p port -d db ...

是否有一种快捷简便的方法来将URI传递给 mongodump 吗?


2
是的。自MongoDB 3.4.6起,可以使用--uri选项。请参见TOOLS-1567 - Neil Lunn
@NeilLunn 如果您将评论发布为答案,我很乐意接受。 - Andrew Mao
好的。我甚至没有注意到,可能是在做其他事情时“即兴”发送了链接。 - Neil Lunn
5个回答

19

--uri选项是在MongoDB 3.4.6的小版本中添加的,并在JIRA问题TOOLS-1567中提到。

它实际上直到MongoDB 3.6发布之后才获得正式文档,但现在已经在手册页面中了。

--uri
在3.4.6版本中新增。

指定mongod连接的可解析URI连接字符串。

以下是标准的URI连接格式:

mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]

要详细了解此字符串的组成部分,请参阅连接字符串URI格式文档。

其他工具,如mongoexportmongoimportmongorestore,也添加了相同的--uri选项。


2
如果您有一个 mongodb+srv:// 连接字符串,请删除 +srv,然后它应该可以工作。 - Gianfranco P.
mongoclient 4.2 - 无需删除 srv,直接可用。 - Hertzel Guinness

18

答案:2020年5月15日

我知道这是一个晚回答,但它解决了我的问题。
要使用URI进行转储,请执行以下操作:

mongodump --uri=[uri]/[db]

其中:

  • uri是您的URI(例如 mongodb+srv://john:xxxxxxxxxxxxxxx@cluster0-jdtjt.mongodb.net
  • db是您想要备份的数据库(例如 sales

因此,在这个人为的例子中,它应该像这样:

mongodump --uri=mongodb+srv://john:xxxxxxxxxxxxxxx@cluster0-jdtjt.mongodb.net/sales

如果这个方法真的行不通,编辑文件/etc/resolv.conf并将nameserver的值更改为例如8.8.8.8,就像这样:

# This file is managed by man:systemd-resolved(8). Do not edit.
#
# This is a dynamic resolv.conf file for connecting local clients to the
# internal DNS stub resolver of systemd-resolved. This file lists all
# configured search domains.
#
# Run "systemd-resolve --status" to see details about the uplink DNS servers
# currently in use.
#
# Third party programs must not access this file directly, but only through the
# symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a different way,
# replace this symlink by a static file or a different symlink.
#
# See man:systemd-resolved.service(8) for details about the supported modes of
# operation for /etc/resolv.conf.

# nameserver 127.0.0.53
nameserver 8.8.8.8
options edns0

现在它应该可以正常工作了。


16

轻松获取整个数据库的转储和恢复步骤,无需丢失任何数据

在本地机器上进行数据转储

mongodump --uri "mongodb+srv://username:password@dbexmplae-fsddf.igt.mongodb.net/dbname" --out "C:\databaseDump"

**定位到已转储的文件夹并将其恢复到本地数据库**

mongorestore --db dbname C:\databaseDump\dbname

恢复到远程数据库

mongorestore --uri "mongodb+srv://username:password@dbexmplae-fsddf.igt.mongodb.net/dbname" --db dbname C:\databaseDump\dbname

1

一个简单易懂的字符串

mongodump --uri='mongodb://...url/...db-name' --out $(pwd)

或者

mongodump --uri='mongodb+srv://...url/...db-name' --out $(pwd)

根据您的实现,将转储位于$(url)上的$(db-name)整个数据库,并将内容放置在当前工作目录$(pwd)中


-2
即使您提供了uri选项,仍然需要提供db选项。https://docs.mongodb.com/manual/reference/program/mongorestore/上的文档在说db选项与uri选项“不兼容”时是错误的。如果您不使用db选项和uri选项一起提供,则会针对每个要恢复的.bson文件出现“不知道如何处理文件”的错误。我在Ubuntu和Mac OS X上测试了mongorestore版本3.6.9。

实际上,情况恰恰相反。文档明确指出,当您使用--uri时,必须忽略某些参数,包括--db https://docs.mongodb.com/manual/reference/program/mongorestore/。 - Mauricio
你需要在URI字符串中连接/your_db。 - igorkf

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接