【问题标题】:MongoDB: Unable to connect from remote machineMongoDB:无法从远程机器连接
【发布时间】:2019-02-15 13:54:38
【问题描述】:

可能是重复的here,但无法找到正确答案,因此提出了这个问题。

所以,我在本地机器上有一个mongodb 设置说M1mongo 服务正在运行并且db 有一些数据。

现在,我想在其他机器上使用 mongo 客户端连接到这个 mongodb,例如 M2,但无法连接。

根据所有答案,我尝试在mongodb.conf 文件中添加M2 地址,但没有成功。

net:
   bindIp: [m2.some.valid.ip,127.0.0.1]

我什至尝试给0.0.0.0,但仍然没有运气。使用 localhost,我可以从我自己的机器连接,但如果我提到 M1 地址并在本地尝试使用该地址,我会得到

Cannot connect to Mongo DB at M1 address, network is unreachable.

我在这里卡了很久。刚开始使用mongodb,请原谅任何新手问题。

我所有的 mongo 服务都已启动并正在运行,并且能够使用终端或本地主机上的任何 mongo 客户端访问数据。

这是我在终端上运行命令mongod 时的日志:

2018-09-11T11:14:13.977+0530 I CONTROL  [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
2018-09-11T11:14:13.993+0530 I CONTROL  [initandlisten] MongoDB starting : pid=11307 port=27017 dbpath=/data/db 64-bit host=DELC02S5E2CG8WN.sea.corp.expecn.com
2018-09-11T11:14:13.993+0530 I CONTROL  [initandlisten] db version v4.0.2
2018-09-11T11:14:13.993+0530 I CONTROL  [initandlisten] git version: fc1573ba18aee42f97a3bb13b67af7d837826b47
2018-09-11T11:14:13.993+0530 I CONTROL  [initandlisten] allocator: system
2018-09-11T11:14:13.993+0530 I CONTROL  [initandlisten] modules: none
2018-09-11T11:14:13.993+0530 I CONTROL  [initandlisten] build environment:
2018-09-11T11:14:13.993+0530 I CONTROL  [initandlisten]     distarch: x86_64
2018-09-11T11:14:13.993+0530 I CONTROL  [initandlisten]     target_arch: x86_64
2018-09-11T11:14:13.993+0530 I CONTROL  [initandlisten] options: {}
2018-09-11T11:14:13.993+0530 I STORAGE  [initandlisten] Detected data files in /data/db created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.
2018-09-11T11:14:13.993+0530 I STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=7680M,session_max=20000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),statistics_log=(wait=0),verbose=(recovery_progress),
2018-09-11T11:14:14.698+0530 I STORAGE  [initandlisten] WiredTiger message [1536644654:698414][11307:0x7fff8fb43380], txn-recover: Main recovery loop: starting at 2/7040
2018-09-11T11:14:14.792+0530 I STORAGE  [initandlisten] WiredTiger message [1536644654:792304][11307:0x7fff8fb43380], txn-recover: Recovering log 2 through 3
2018-09-11T11:14:14.856+0530 I STORAGE  [initandlisten] WiredTiger message [1536644654:856010][11307:0x7fff8fb43380], txn-recover: Recovering log 3 through 3
2018-09-11T11:14:14.904+0530 I STORAGE  [initandlisten] WiredTiger message [1536644654:904754][11307:0x7fff8fb43380], txn-recover: Set global recovery timestamp: 0
2018-09-11T11:14:15.037+0530 I RECOVERY [initandlisten] WiredTiger recoveryTimestamp. Ts: Timestamp(0, 0)
2018-09-11T11:14:15.103+0530 I CONTROL  [initandlisten] 
2018-09-11T11:14:15.103+0530 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-09-11T11:14:15.103+0530 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2018-09-11T11:14:15.103+0530 I CONTROL  [initandlisten] 
2018-09-11T11:14:15.103+0530 I CONTROL  [initandlisten] ** WARNING: This server is bound to localhost.
2018-09-11T11:14:15.103+0530 I CONTROL  [initandlisten] **          Remote systems will be unable to connect to this server. 
2018-09-11T11:14:15.103+0530 I CONTROL  [initandlisten] **          Start the server with --bind_ip <address> to specify which IP 
2018-09-11T11:14:15.103+0530 I CONTROL  [initandlisten] **          addresses it should serve responses from, or with --bind_ip_all to
2018-09-11T11:14:15.103+0530 I CONTROL  [initandlisten] **          bind to all interfaces. If this behavior is desired, start the
2018-09-11T11:14:15.103+0530 I CONTROL  [initandlisten] **          server with --bind_ip 127.0.0.1 to disable this warning.
2018-09-11T11:14:15.103+0530 I CONTROL  [initandlisten] 
2018-09-11T11:14:15.157+0530 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data'
2018-09-11T11:14:15.161+0530 I NETWORK  [initandlisten] waiting for connections on port 27017

mongodb 版本是db version v4.0.2,我使用的是mac。

另外,当我绑定 M2 机器的 ip 并执行 mongo --host m2.address 时,我得到了这个:

mongo --host m2.address
MongoDB shell version v4.0.2
connecting to: mongodb://m2.address:27017/
2018-09-11T11:31:07.186+0530 E QUERY    [js] Error: couldn't connect to server m2.address:27017, connection attempt failed: SocketException: Error connecting to m2.address:27017 :: caused by :: Connection refused :
connect@src/mongo/shell/mongo.js:257:13
@(connect):1:6
exception: connect failed

更新

这些是我在尝试连接之前执行的步骤:

  1. 启动mongodb服务:brew services start mongodb
  2. 通过运行命令mongod 启动mongod
  3. 使用 Robo3T 通过指定0.0.0.0 作为连接地址来创建连接。也试过具体地址。
  4. 全部给出“网络无法访问”错误。

如果在启动时缺少任何参数或做错了什么,请告诉我。

【问题讨论】:

  • @AnthonyWinzlet 如果您提到的链接解决了我的问题,我真的不明白。请详细说明。
  • 我能做到brew services start mongodb 猜猜它启用了吗?
  • 是的。它开始了。检查提到步骤的更新问题。
  • 据我了解,如果您的MongoDB 设置在M1 计算机(IP:192.168.1.1)中。您可以使用 M1(192.168.1.1) 的 IP 地址访问 M2 上的 MongoDB。
  • 这是主要问题。它不适用于 M2。

标签: mongodb macos robo3t


【解决方案1】:

根据所有答案,我尝试在 mongodb.conf 文件中添加 M2 地址,但没有成功。

这是错误的。 net.bindIp 包含服务器监听的 M1 接口列表,因此 M2 要连接到 M1,net.bindIp 应该有 M1 的 IP 地址。

另外,当我绑定我的 M2 机器的 ip 并执行 mongo --host m2.address 时,我得到了这个:

这是错误的。 --host 参数应该有服务器的 IP 地址 - 在你的情况下是 M1。

使用 Robo3T 通过指定 0.0.0.0 创建连接

这毫无意义。 0.0.0.0 根本不是有效的 IP 地址。它可以用作 M1 上 mongodb.conf 中 net.bindIp 的通配符值,以使服务器侦听所有可用接口。 Robo3T 是一个客户端,你需要给它服务器的 ip 地址才能连接到它。

在 M1(服务器)上:

  • 设置net.bindIp: 0.0.0.0
  • 重启mongod

在 M2(客户端)上:

  • 连接服务器:mongo --host m1.address

疑难解答:

  1. 确认 mongodb 已启动并在 M1 上运行 - 运行命令 sudo lsof -i | grep mongod。它应该打印至少 1 个带有 TCP *:27017 (LISTEN) 之类的进程。
  2. 如果 p1 正常工作,请确认网络正常且没有防火墙阻止连接 - 在 M2 上执行 telnet m1_ip 27017 它应该会给您类似 Escape character is '^]' 的信息。

【讨论】:

  • 做了你提到的一切。 mongo --host m1.address 在终端上给出 Operation Time out 而 Robo3T 给出 Unreachable Network m1.address
  • 我已经更新了最常见情况的答案 - 服务器没有启动或防火墙阻止连接。请检查是不是这样。
  • 另外,我注意到2018-09-11T11:14:13.993+0530 I CONTROL [initandlisten] options: {} - 在您更改mongodb.conf 后,您能否确认它有所不同。可能只是您正在修改的配置文件没有被数据库使用。在这种情况下,它只侦听本地接口,其他计算机无法访问。
猜你喜欢
  • 1970-01-01
  • 2010-12-22
  • 1970-01-01
  • 2011-10-09
  • 2016-05-21
  • 2018-11-12
  • 2017-05-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多