Java mongoDB,连接被拒绝
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37902942/
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
mongoDB, connection refused
提问by GoingMyWay
Show my code
显示我的代码
conf.set( "mongo.input.uri" , "mongodb://127.0.0.1/stackoverflow.mrtest" );
conf.set( "mongo.output.uri" , "mongodb://127.0.0.1/stackoverflow.mrtest_out2" );
the code runs without error when the host is localhost
or 127.0.0.1
. But when the host changed to my ip wlan0 192.168.1.102
, it returned the following error
当主机是localhost
或时,代码运行没有错误127.0.0.1
。但是当主机改成我的ip wlan0时192.168.1.102
,它返回了以下错误
Cluster created with settings {hosts=[192.168.1.102:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
Exception in monitor thread while connecting to server 192.168.1.102:27017
com.mongodb.MongoSocketOpenException: Exception opening socket
at com.mongodb.connection.SocketStream.open(SocketStream.java:63)
at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:114)
at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:127)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.net.ConnectException: connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at com.mongodb.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:50)
at com.mongodb.connection.SocketStream.open(SocketStream.java:58)
... 3 more
I have open the port 27017.
我已经打开了 27017 端口。
sudo iptables -A INPUT -ptcp --dport 27017 -j ACCEPT
My OS is Ubuntu 14.04.
我的操作系统是 Ubuntu 14.04。
How should I fix it? Thank you!
我该如何解决?谢谢!
采纳答案by Onur
By default MongoDB only binds to the loopback interface which makes it only accessible from localhost. To change that you need to edit this line in mongod.conf
file;
默认情况下,MongoDB 只绑定到环回接口,这使得它只能从本地主机访问。要更改您需要在mongod.conf
文件中编辑这一行;
# /etc/mongod.conf
# Listen to local interface only. Comment out to listen on all interfaces.
bind_ip = 127.0.0.1
you can change it to bind_ip = 127.0.0.1,192.168.1.102
to allow LAN and local connections or you can remove or comment out that line to allow all connections.
您可以将其更改bind_ip = 127.0.0.1,192.168.1.102
为允许 LAN 和本地连接,或者您可以删除或注释掉该行以允许所有连接。
For more info : MongoDB – Allow remote access
欲了解更多信息:MongoDB – 允许远程访问
回答by Wabwire Levis
I was able to determine that it was an issue with the bind parameter in the /etc/mongod.conf. Instead of commenting it out I set it to 0.0.0.0 to all for remote access.
我能够确定这是 /etc/mongod.conf 中的 bind 参数的问题。我没有将其注释掉,而是将其设置为 0.0.0.0 以进行远程访问。
# network interfaces
net:
port: 27017
bindIp: 0.0.0.0