简体   繁体   English

mongoDB,连接被拒绝

[英]mongoDB, connection refused

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 .当主机是localhost127.0.0.1时,代码运行没有错误。 But when the host changed to my ip wlan0 192.168.1.102 , it returned the following error但是当主机更改为我的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 default MongoDB only binds to the loopback interface which makes it only accessible from localhost.默认情况下,MongoDB 只绑定到环回接口,这使得它只能从本地主机访问。 To change that you need to edit this line in mongod.conf file;要更改它,您需要在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 – 允许远程访问

There could be several reasons of it , which in short can be concluded as Your Application is unable to Communicate mongoDB service可能有多种原因,简而言之,您的应用程序无法与 mongoDB 服务进行通信。

1.Check your MongoDB using the same IP configured in your application.yml file, If not then configure the same used by MongoDB: 1.使用 application.yml 文件中配置的相同 IP 检查您的 MongoDB,如果没有,则配置 MongoDB 使用的相同 IP:

spring:
  profiles:
    active: dev
---
  spring:
    profiles: dev
    data:
      mongodb:
        host: localhost
        port: 27017

Here i assumed my mongo running on localhost, and port 27017, so i configured accordingly.在这里,我假设我的 mongo 在 localhost 和端口 27017 上运行,因此我进行了相应的配置。

  1. Check whether your MongoDB service up and running , How to check ?检查您的 MongoDB 服务是否已启动并运行,如何检查? Execute following command in your terminal在终端中执行以下命令

sudo service mongodb status须藤服务 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>

If Status not visible as active and running, you need to start/restart the service如果 Status 不可见为 active 和 running,则需要启动/重新启动服务

sudo service mongodb restart须藤服务 mongodb 重启

I was able to determine that it was an issue with the bind parameter in the /etc/mongod.conf.我能够确定这是 /etc/mongod.conf 中的 bind 参数的问题。 Instead of commenting it out I set it to 0.0.0.0 to all for remote access.我没有将其注释掉,而是将其设置为 0.0.0.0 以进行远程访问。

# network interfaces
net:
  port: 27017
  bindIp: 0.0.0.0

Exception: com.mongodb.MongoSocketOpenException: Exception opening socket异常: com.mongodb.MongoSocketOpenException:打开套接字的异常

Solution:解决方法:

Verify whether you have started "mongo daemon" or not.验证您是否已启动“mongo daemon”

Windows Terminal: mongod.exe Windows 终端: mongod.exe

Linux Termina: mongod Linux 终端: mongod

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM