16

使用. Mongo_ homebrew如果我mongo在 shell 上键入,它会连接到test. 但是当我输入ip address本地机器的而不是127.0.0.1

mongo --host 192.168.1.100 --verbose

它给了我错误信息

MongoDB shell version: 2.4.6
Fri Aug 23 15:18:27.552 versionArrayTest passed
connecting to: 192.168.1.100:27017/test
Fri Aug 23 15:18:27.579 creating new connection to:192.168.1.100:27017 
Fri Aug 23 15:18:27.579 BackgroundJob starting: ConnectBG
Fri Aug 23 15:18:27.580 Error: couldn't connect to server 192.168.1.100:27017 at src/mongo/shell/mongo.js:147
Fri Aug 23 15:18:27.580 User Assertion: 12513:connect failed

已尝试mongo.conf通过评论bind_ip或将 IP 地址从更改为127.0.0.1修改,0.0.0.0但没有运气。这应该很简单,但现在不知道。使用麦克。

谢谢

更新:根据要求。这在我按照您的建议进行更改后有效。

ifconfig 输出

lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 16384
   options=3<RXCSUM,TXCSUM>
   inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1 
   inet 127.0.0.1 netmask 0xff000000 
   inet6 ::1 prefixlen 128 
gif0: flags=8010<POINTOPOINT,MULTICAST> mtu 1280
stf0: flags=0<> mtu 1280
en0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
   options=b<RXCSUM,TXCSUM,VLAN_HWTAGGING>
   ether XX:XX:XX:
   media: autoselect (none)
   status: inactive
en1: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
   ether XX:XX:XX:XX:01 
   inet6 XXXX:XXXX:XXXX: %en1 prefixlen 64 scopeid 0x5 
   inet 192.168.1.100 netmask 0xffffff00 broadcast 192.168.1.255
   media: autoselect
   status: active
 p2p0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> mtu 2304
   ether XX:XX:XX:XX:XX
   media: autoselect
   status: inactive
 fw0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 4078
   lladdr XX:XX:XX:XX
   media: autoselect <full-duplex>
   status: inactive

执行命令时输出mongo --host 192.168.1.100 --verbose

MongoDB shell version: 2.4.5
Fri Aug 23 16:42:09.806 versionArrayTest passed
connecting to: 192.168.1.100:27017/test
Fri Aug 23 16:42:09.837 creating new connection to:192.168.1.100:27017
Fri Aug 23 16:42:09.837 BackgroundJob starting: ConnectBG
Fri Aug 23 16:42:10.129 connected connection!
Server has startup warnings: 
Fri Aug 23 16:41:59.025 [initandlisten] 
Fri Aug 23 16:41:59.025 [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000

文件mongod.conf

# Store data in /usr/local/var/mongodb instead of the default /data/db
dbpath = /usr/local/var/mongodb

# Append logs to /usr/local/var/log/mongodb/mongo.log
logpath = /usr/local/var/log/mongodb/mongo.log
logappend = true

# Only accept local connections
bind_ip = 0.0.0.0`
4

5 回答 5

28

我刚刚在我的 Mac 上使用 Homebrew 进行了测试,如果您更改绑定地址,则可以正常工作。我怀疑你可能只是没有得到正确的绑定配置?

就这样我们有了所有的信息,你能粘贴 please 的输出ifconfig吗?

默认情况下,MongoDB 应该监听所有接口,您不需要更改配置,但是,Homebrew 设置似乎覆盖了这个(/usr/local/etc/mongod.conf):

# Only accept local connections
bind_ip = 127.0.0.1

请杀死 MongoDB 并运行它(注意 -v):

$ mongod --bind_ip 0.0.0.0 -v
warning: bind_ip of 0.0.0.0 is unnecessary; listens on all ips by default
all output going to: /usr/local/var/log/mongodb/mongo.log

请为此粘贴您的输出?

然后尝试:

$ mongo --host 192.168.43.2 --verbose
MongoDB shell version: 2.4.6
Sat Aug 24 09:07:14.556 versionArrayTest passed
connecting to: 192.168.43.2:27017/test
Sat Aug 24 09:07:14.657 creating new connection to:192.168.43.2:27017
Sat Aug 24 09:07:14.657 BackgroundJob starting: ConnectBG
Sat Aug 24 09:07:14.657 connected connection!
Server has startup warnings: 
Sat Aug 24 09:06:44.360 [initandlisten] 
Sat Aug 24 09:06:44.360 [initandlisten] ** WARNING: soft rlimits too low. Number of files     is 256, should be at least 1000
> 

显然用你的IP地址替换它。让我们知道情况如何。

于 2013-08-23T23:09:55.180 回答
7

成功:

  1. 更改服务配置文件 C:\Program Files\MongoDB\Server\4.0\bin\mongod.cfg
# network interfaces (wjp: comment bindIp listening on all IPs)
net:
  port: 27017
#  bindIp: 127.0.0.1
  bindIp: 0.0.0.0
  1. 重启mongodb服务(或重启电脑);
  2. 确保机器级防火墙打开端口 27017
  3. 连接好。
于 2019-05-23T16:02:04.933 回答
2

否 事实上,如果你想从任何 ip 访问 rest 接口,你不需要在 mongod.conf 中将 bind_ip 设置为 0.0.0.0,你只需要注释或删除其中的配置项,类似于

#bind_ip=127.0.0.1

然后,重启你的服务,你会发现你可以从你的机器上的28017端口访问rest服务

于 2017-12-20T02:50:16.150 回答
1

对我来说,我用 bindIpAll: true 替换了 bindIp(有关详细信息,请参阅http://docs.mongodb.org/manual/reference/configuration-options/)。

这是我的 mongod.conf 文件的内容。

# mongod.conf

# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/

# Where and how to store data.
storage:
  dbPath: /var/lib/mongodb
  journal:
    enabled: true
#  engine:
#  mmapv1:
#  wiredTiger:

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

# network interfaces
net:
  port: 27017
  # bindIp: 127.0.0.1
  bindIpAll: true


# how the process runs
processManagement:
  timeZoneInfo: /usr/share/zoneinfo

#security:

#operationProfiling:

#replication:

#sharding:

## Enterprise-Only Options:

#auditLog:

#snmp:
于 2019-12-04T04:52:21.843 回答
0

注意:以下配置适用于 windows 10 和 MongoDB 4.0

有两种配置方法。

  1. 使用 shell 命令。
  2. 或通过mongod.cfg.

使用 shell 命令

首先打开cmdasadministrator并转到--YourPath--\MongoDB\Server\4.0\bin

  1. 如果您已经在运行 MongoDB 服务,则运行此命令,否则,转到第二点。mongod --remove

  2. 然后使用以下命令设置数据文件夹、日志文件和 ip 绑定的路径

    mongod --dbpath "C:\Program Files\MongoDB\Server\4.0\data" --logpath "C:\Program Files\MongoDB\Server\4.0\log\mongod.log" --bind_ip "0.0.0.0" --install --serviceName "MongoDB"

警告:此 ip 配置允许通过网络访问您的数据库。

  1. 现在转到services.msc并找到MongoDB,然后右键单击 -> 启动服务。

通过mongod.cfg

MongoDB 在这里有一个非常好的配置文档。

只需更改bindIp: 127.0.0.1bindIp: 0.0.0.0. 然后使用上述主题中的#3 点重新启动服务。

于 2018-12-20T09:05:19.727 回答