显示我的代码
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" );
当主机为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
我已打开端口27017。
sudo iptables -A INPUT -ptcp --dport 27017 -j ACCEPT
我的操作系统是Ubuntu 14.04。
我该如何解决?谢谢!
答案 0 :(得分:5)
默认情况下,MongoDB仅绑定到loopback接口,这使得它只能从localhost访问。要更改它,您需要在mongod.conf
文件中编辑此行;
# /etc/mongod.conf
# Listen to local interface only. Comment out to listen on all interfaces.
bind_ip = 127.0.0.1
您可以将其更改为bind_ip = 127.0.0.1,192.168.1.102
以允许LAN和本地连接,或者您可以删除或注释掉该行以允许所有连接。
答案 1 :(得分:1)
我能够确定这与/etc/mongod.conf中的bind参数有关。我没有将其注释掉,而是将其全部设置为0.0.0.0以进行远程访问。
# network interfaces
net:
port: 27017
bindIp: 0.0.0.0
答案 2 :(得分:0)
可能有多种原因,总之可以归结为您的应用程序无法通信mongoDB服务
1。使用在application.yml文件中配置的相同IP检查您的MongoDB, 如果没有,请配置与MongoDB相同的名称:
spring:
profiles:
active: dev
---
spring:
profiles: dev
data:
mongodb:
host: localhost
port: 27017
在这里,我假设我的mongo在本地主机和端口27017上运行,因此我进行了相应的配置。
sudo服务mongodb状态
<pre><code>
● mongodb.service - An object/document-oriented database
Loaded: loaded (/lib/systemd/system/mongodb.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2020-07-03 20:10:15 IST; 1min 54s ago
Docs: man:mongod(1)
Main PID: 14305 (mongod)
Tasks: 23 (limit: 4915)
CGroup: /system.slice/mongodb.service
└─14305 /usr/bin/mongod --unixSocketPrefix=/run/mongodb --config /etc/mongodb.conf</pre></code>
如果“状态”不显示为活动且正在运行,则需要启动/重新启动服务
sudo服务mongodb重新启动
答案 3 :(得分:0)
异常: com.mongodb.MongoSocketOpenException:异常打开套接字
解决方案:
验证是否已启动“ mongo守护程序” 。
Windows终端: mongod.exe
Linux终端: mongod