【问题标题】:Using mongodump with a standard Mongo URI使用带有标准 Mongo URI 的 mongodump
【发布时间】:2017-09-15 09:42:58
【问题描述】:

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

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

但是,mongodump 似乎需要一种尴尬的语法,将其分解为不同的参数:

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

是否也有一种快速简便的方法将 URI 传递给 mongodump

【问题讨论】:

  • 是的。从 MongoDB 3.4.6 起可以使用--uri。见TOOLS-1567
  • @NeilLunn 很高兴接受您的评论,如果您将其发布为答案。
  • 好的。甚至没有注意到它,可能只是在做其他事情之间“即时”发送了链接。

标签: mongodb database-backups


【解决方案1】:

--uri 选项是在 MongoDB 3.4.6 的次要版本中添加的。这在 JIRA 问题 TOOLS-1587 中被引用。

其实直到 MongoDB 3.6 发布才拿到官方文档,不过是now in the manual page

--uri
3.4.6 版中的新功能。

为要连接的 mongod 指定一个可解析的 URI 连接字符串。

以下是标准的 URI 连接方案:

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

有关此字符串的组成部分的详细说明,请参阅Connection String URI Format 文档。

mongoexportmongoimportmongorestore 等其他工具中添加了相同的 --uri 选项。

【讨论】:

  • 如果你有一个mongodb+srv:// 连接字符串,删除+srv 它应该可以工作
  • mongoclient 4.2 - 无需删除 srv。只是工作。
【解决方案2】:

答案: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

现在应该可以了。

【讨论】:

    【解决方案3】:

    在任何数据丢失的情况下一次获取转储和恢复整个数据库的简单步骤

    在本地机器上进行转储

    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
    

    【讨论】:

      【解决方案4】:

      一个简单易懂的字符串

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

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

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

      【讨论】:

        【解决方案5】:

        即使您提供了uri 选项,您仍然需要提供db 选项。当db 选项与uri 选项“不兼容”时,https://docs.mongodb.com/manual/reference/program/mongorestore/ 的文档不正确。如果您不提供db 选项和uri 选项,那么您尝试恢复的每个.bson 文件都会出现don't know what to do with file 错误。我在 ubuntu 和 mac osx 上使用 mongorestore 版本 3.6.9 进行了测试。

        【讨论】:

        猜你喜欢
        • 2021-04-27
        • 2021-03-20
        • 2013-07-01
        • 1970-01-01
        • 2011-05-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多