mongodb connection refused的可能原因

一 OS环境:

 
[root@localhost ~]# cat /etc/redhat-release 
CentOS Linux release 7.5.1804 (Core) 
[root@localhost ~]# uname -rm
3.10.0-862.3.2.el7.x86_64 x86_64
[root@localhost ~]#

二 mongo信息:

MongoDB server version: 4.0.0

且防火墙,SELINUX全关闭的情况下,访问mongodb报错。

三 错误信息如下:

[root@localhost ~]# mongo 172.16.143.250:27017/admin
MongoDB shell version v4.0.0
connecting to: mongodb://172.16.143.250:27017/admin
2018-07-20T09:30:52.691+0800 E QUERY    [js] Error: couldn't connect to server 172.16.143.250:27017, connection attempt failed: SocketException: Error connecting to 172.16.143.250:27017 :: caused by :: Connection refused :
connect@src/mongo/shell/mongo.js:251:13
@(connect):1:6
exception: connect failed
[root@localhost ~]#

且在服务器本地及其它机器上telnet该机器的mongodb默认端口27017都不通:

$ telnet 172.16.143.250 27017
Trying 172.16.143.250...
telnet: Unable to connect to remote host: Connection refused

四 原因及解决办法:

如果服务器上有多个IP地址的话,需要将mongodb的服务绑定到多个IP地址,在配置文件里加上:

bind_ip_all=on

当该参数配置为bind_ip_all=all时,启动mongodb会报出下述错误:

 Error parsing INI config file: the argument ('all') for option 'bind_ip_all' is invalid. Valid choices are 'on|off', 'yes|no', '1|0' and 'true|false'​

重启mongodb,然后访问正常。

该机器172.16.143.250的IP地址如下:

[root@localhost mongodata]# ifconfig 
docker0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.10.10.1  netmask 255.255.255.0  broadcast 0.0.0.0
        inet6 fe80::42:2cff:fef4:41cb  prefixlen 64  scopeid 0x20
        ether 02:42:2c:f4:41:cb  txqueuelen 0  (Ethernet)
        RX packets 15725  bytes 857602 (837.5 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 20945  bytes 2675605 (2.5 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

enp0s31f6: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.16.143.250  netmask 255.255.255.0  broadcast 172.16.143.255
        inet6 fe80::3bf1:41bf:bb41:1a74  prefixlen 64  scopeid 0x20
        ether 2c:fd:a1:be:65:d3  txqueuelen 1000  (Ethernet)
        RX packets 83706  bytes 17618724 (16.8 MiB)
        RX errors 0  dropped 102  overruns 0  frame 0
        TX packets 30918  bytes 6621448 (6.3 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
        device interrupt 16  memory 0xdf200000-df220000  

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 332117  bytes 34574785 (32.9 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 332117  bytes 34574785 (32.9 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

veth6d13f43: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::38d0:60ff:fe0d:1867  prefixlen 64  scopeid 0x20
        ether 3a:d0:60:0d:18:67  txqueuelen 0  (Ethernet)
        RX packets 4059  bytes 273076 (266.6 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 5426  bytes 580733 (567.1 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

veth8741a2f: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::14d2:d7ff:fe8e:75f7  prefixlen 64  scopeid 0x20
        ether 16:d2:d7:8e:75:f7  txqueuelen 0  (Ethernet)
        RX packets 3401  bytes 245833 (240.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 4769  bytes 871463 (851.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

vetha20a789: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::e829:79ff:fea8:558d  prefixlen 64  scopeid 0x20
        ether ea:29:79:a8:55:8d  txqueuelen 0  (Ethernet)
        RX packets 4058  bytes 273390 (266.9 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 5423  bytes 637072 (622.1 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

vetha26bafa: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::58cc:3aff:fec9:ad8e  prefixlen 64  scopeid 0x20
        ether 5a:cc:3a:c9:ad:8e  txqueuelen 0  (Ethernet)
        RX packets 4063  bytes 273661 (267.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 5430  bytes 594535 (580.6 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@localhost mongodata]#

发表评论

邮箱地址不会被公开。 必填项已用*标注