无法使用机器 ip 连接到 mongodb
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18412850/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot connect to mongodb using machine ip
提问by java_dude
Installed Mongo
using homebrew
. If I type mongo
on shell, it gets connected to test
. But when I type the ip address
of the local machine instead of 127.0.0.1
安装Mongo
使用homebrew
. 如果我mongo
在 shell 上输入,它会连接到test
. 但是当我输入ip address
本地机器的而不是127.0.0.1
mongo --host 192.168.1.100 --verbose
It gives me error message
它给了我错误信息
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
Have tried modifying the mongo.conf
by commenting bind_ip
or by changing the ip address from 127.0.0.1
to 0.0.0.0
but no luck. This should be simple but have no clue now. Using mac.
已尝试mongo.conf
通过评论bind_ip
或将 IP 地址从 更改为来修改127.0.0.1
,0.0.0.0
但没有运气。这应该很简单,但现在没有线索。使用 mac。
Thanks
谢谢
Update: As requested. This works after I have made the changes as you suggested.
更新:根据要求。在我按照您的建议进行更改后,此方法有效。
ifconfig output
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
Output when executing command mongo --host 192.168.1.100 --verbose
执行命令时的输出 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
File mongod.conf
文件 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`
回答by victorhooi
I just tested this on my Mac with Homebrew, works fine ifyou change the bind address. I suspect you probably just didn't get the config for bind correct?
我刚刚用 Homebrew 在我的 Mac 上测试了这个,如果你更改绑定地址,它可以正常工作。我怀疑你可能只是没有得到正确的绑定配置?
Just so we have all the information, can you paste the output of ifconfig
please?
就这样我们有了所有的信息,你能粘贴输出ifconfig
吗?
By default, MongoDB should listen on all interfaces, you shouldn't need to change the configuration, however, the Homebrew setup seems to override this (/usr/local/etc/mongod.conf):
默认情况下,MongoDB 应该侦听所有接口,您不需要更改配置,但是,Homebrew 设置似乎覆盖了这个 (/usr/local/etc/mongod.conf):
# Only accept local connections
bind_ip = 127.0.0.1
Please kill MongoDB and run this (note the -v):
请杀死 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
Just paste your output for that please?
请粘贴您的输出?
And then just try:
然后尝试:
$ 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
>
Obviously replace it with your IP address. Let us know how that goes.
显然,将其替换为您的 IP 地址。让我们知道这是怎么回事。
回答by Jiping
success:
成功:
- change service config file C:\Program Files\MongoDB\Server\4.0\bin\mongod.cfg
- 更改服务配置文件 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
- restart mongodb service (or restart computer);
- make sure machine level firewall open port 27017
- connect ok.
- 重启mongodb服务(或重启电脑);
- 确保机器级防火墙打开端口 27017
- 连接好。
回答by Zhu Zhaohua
No In fact, if you want to access the rest interface from any ip, you needn't set bind_ip to 0.0.0.0 in the mongod.conf, you only comment or remove the configuration item from it, as similar as
否 其实,如果你想从任何ip访问rest接口,你不需要在mongod.conf中设置bind_ip为0.0.0.0,你只需在其中注释或删除配置项,类似于
#bind_ip=127.0.0.1
And then, restart your service, you can find that you can access the rest service from the 28017 port from your machine
然后,重启你的服务,你会发现你的机器可以从28017端口访问rest服务
回答by John Forbes
For me I replaced the bindIp with bindIpAll: true (see http://docs.mongodb.org/manual/reference/configuration-options/for details).
对我来说,我用 bindIpAll: true 替换了 bindIp(有关详细信息,请参阅http://docs.mongodb.org/manual/reference/configuration-options/)。
This is the content of my mongod.conf file.
这是我的 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:
回答by satheesh
Note: Below configuration is for windows 10 & MongoDB 4.0
注意:以下配置适用于 Windows 10 和 MongoDB 4.0
There are two ways to configure this.
有两种方法可以配置它。
- Using shell commands.
- Or through
mongod.cfg
.
- 使用 shell 命令。
- 或通过
mongod.cfg
.
Using shell commands
使用 shell 命令
First open the cmd
as administrator
and go to --YourPath--\MongoDB\Server\4.0\bin
首先打开cmd
asadministrator
并转到--YourPath--\MongoDB\Server\4.0\bin
If you already running the MongoDB service then run this command or else, go to second point.
mongod --remove
Then set the path for data folder, log file and ip bind using the below command
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"
如果您已经在运行 MongoDB 服务,则运行此命令,否则,转到第二点。
mongod --remove
然后使用以下命令设置数据文件夹、日志文件和 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"
caution: This ip configuration gives access to your DB throughout the network.
注意:此 ip 配置允许在整个网络中访问您的数据库。
- Now go to
services.msc
and find theMongoDB
, then Right click -> Startthe service.
- 现在转到
services.msc
并找到MongoDB
,然后右键单击 -> 启动服务。
Through mongod.cfg
通过 mongod.cfg
MongoDB has a very good documentation for Configurationhere.
MongoDB 有一个非常好的配置文档here。
Just change the bindIp: 127.0.0.1
to bindIp: 0.0.0.0
. Then restart the service using the #3 point from the above topic.
只需将 更改bindIp: 127.0.0.1
为bindIp: 0.0.0.0
。然后使用上述主题中的 #3 点重新启动服务。