无法使用机器ip连接到mongodb

时间:2013-08-23 22:31:58

标签: mongodb

使用Mongo安装homebrew。如果我在shell上输入mongo,它就会连接到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或将IP地址从bind_ip更改为127.0.0.1来尝试修改0.0.0.0,但没有运气。这应该很简单但现在没有任何线索。使用mac。

由于

更新:根据要求。这可以在我按照您的建议进行更改后起作用。

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`

5 个答案:

答案 0 :(得分:16)

我刚刚在我的Mac上用Homebrew测试了这个,如果你改变了绑定地址,那么工作正常。我怀疑你可能只是没有得到绑定的配置正确吗?

我们拥有所有信息,您可以粘贴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地址替换它。让我们知道这是怎么回事。

答案 1 :(得分:2)

没有 实际上,如果你想从任何ip访问rest接口,你不需要在mongod.conf中将bind_ip设置为0.0.0.0,你只需要注释或删除配置项,就像

#bind_ip=127.0.0.1

然后,重新启动您的服务,您可以发现可以从您的机器从28017端口访问其余服务

答案 2 :(得分:0)

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

有两种配置方式。

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

使用shell命令

首先以cmd的身份打开administrator并转到--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在documentation上有一个非常好的文档。

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

答案 3 :(得分:0)

成功:

  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. 连接正常。

答案 4 :(得分:0)

对我来说,我将bindIp替换为bindIpAll:true(有关详细信息,请参见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: