Unable to connect to the hostname node

Hey

After I’ve removed and readied a node, and when I try to connect to this node using the hostname, it fails with Error: couldn't connect to server m103:27001, connection attempt failed :

Full message bellow

mongo --host “m103:27001” -u “m103-admin” -p “m103-pass” --authenticationDatabase “admin”

MongoDB shell version v3.6.8

connecting to: mongodb://m103:27001/

2018-10-24T13:18:58.671+0000 W NETWORK [thread1] Failed to connect to 127.0.1.1:27001, in(checking socket for error after poll), reason: Connection refused

2018-10-24T13:18:58.672+0000 E QUERY [thread1] Error: couldn’t connect to server m103:27001, connection attempt failed :

connect@src/mongo/shell/mongo.js:251:13

@(connect):1:6

exception: connect failed

Thank you for your help

make sure the mongod is running and listening to the right port

netstat -na | grep 27001
ps -ef | grep mongod

1 Like

Also, verify that the mongod listens to the correct IP.

I have noticed with this VM that the hostname resolves to 127.0.1.1 instead of 127.0.0.1. Try doing a ping to “m103” and see where it heads off to. You may need to adjust the mongod config file.

Technically this is Networking-101, but I understand that not everybody joining these classes will be versed in that field.

2 Likes

@Hasna,

Please try to steps others have helpfully provided. If none of them work, please follow up!

Thank you for all your messages.
Unfortunately, It didn’t work for me.

@TimHon Yes, mongod is running

vagrant@m103:~$ ps -ef | grep mongod
vagrant 2241 1 1 10:21 ? 00:00:17 mongod -f lab_node1.conf
vagrant 2349 1 1 10:23 ? 00:00:15 mongod -f lab_node4.conf
vagrant 2436 1 1 10:23 ? 00:00:15 mongod -f lab_node5.conf
vagrant 2708 2047 0 10:39 pts/1 00:00:00 grep --color=auto mongod

@Tess_Sluijter Yes it’s 127.0.1.1
(and yes its definitely networking basics, but don’t have this kind of knowledge, if you have any good mooc to suggest to me, I’ll be thankfull)

vagrant@m103:~$ ping m103

PING m103.mongodb.university (127.0.1.1) 56(84) bytes of data.

64 bytes from m103.mongodb.university (127.0.1.1): icmp_seq=1 ttl=64 time=0.030 ms

64 bytes from m103.mongodb.university (127.0.1.1): icmp_seq=2 ttl=64 time=0.056 ms

Initially, Before removing the node, I have this members list using rs.status()

“members” : [
{
“_id” : 3,
“name” : “192.168.103.100:27001”,
“health” : 1,
“state” : 2,
“stateStr” : “SECONDARY”
},
{
“_id” : 5,
“name” : “192.168.103.100:27005”,
“health” : 1,
“state” : 1,
“stateStr” : “PRIMARY”
},
{
“_id” : 6,
“name” : “192.168.103.100:27004”,
“health” : 1,
“state” : 2,
“stateStr” : “SECONDARY”
}
],

After
rs.remove("192.168.103.100:27001")
rs.add("m103:27001")

Members list is as follow

“members” : [
{
“_id” : 5,
“name” : “192.168.103.100:27005”,
“health” : 1,
“state” : 1,
“stateStr” : “PRIMARY”
},
{
“_id” : 6,
“name” : “192.168.103.100:27004”,
“health” : 1,
“state” : 2,
“stateStr” : “SECONDARY”
},
{
“_id” : 7,
“name” : “m103:27001”,
“health” : 1,
“state” : 2,
“stateStr” : “SECONDARY”
}
],

If I try to connect to mongo using
mongo --host "m103-repl/m103:27001" -u "m103-admin" -p "m103-pass" --authenticationDatabase "admin"

I keep getting

MongoDB shell version v3.6.8
connecting to: mongodb://m103:27001/?replicaSet=m103-repl
2018-10-25T10:49:39.558+0000 I NETWORK [thread1] Starting new replica set monitor for m103-repl/m103:27001
2018-10-25T10:49:39.560+0000 W NETWORK [thread1] Failed to connect to 127.0.1.1:27001, in(checking socket for error after poll), reason: Connection refused
2018-10-25T10:49:39.561+0000 W NETWORK [thread1] Unable to reach primary for set m103-repl
2018-10-25T10:49:39.562+0000 I NETWORK [thread1] Cannot reach any nodes for set m103-repl. Please check network connectivity and the status of the set. This has happened for 1 checks in a row.
2018-10-25T10:49:40.066+0000 W NETWORK [thread1] Failed to connect to 127.0.1.1:27001, in(checking socket for error after poll), reason: Connection refused
2018-10-25T10:49:40.066+0000 W NETWORK [thread1] Unable to reach primary for set m103-repl

And If I try to connect directly to the node m103:27001 I get

vagrant@m103:~$ mongo --host “m103:27001” -u “m103-admin” -p “m103-pass” --authenticationDatabase “admin”
MongoDB shell version v3.6.8
connecting to: mongodb://m103:27001/
2018-10-25T10:51:47.475+0000 W NETWORK [thread1] Failed to connect to 127.0.1.1:27001, in(checking socket for error after poll), reason: Connection refused
2018-10-25T10:51:47.475+0000 E QUERY [thread1] Error: couldn’t connect to server m103:27001, connection attempt failed :
connect@src/mongo/shell/mongo.js:251:13
@(connect):1:6
exception: connect failed

It only works if I connect using --host "m103-repl/192.168.103.100:27005" or --host "m103-repl/192.168.103.100:27004" but I keep getting

MongoDB Enterprise m103-repl:PRIMARY> 2018-10-25T10:56:20.902+0000 W NETWORK [ReplicaSetMonitor-TaskExecutor-0] Failed to connect to 127.0.1.1:27001, in(checking socket for error after poll), reason: Connection refused

As mentioned in the lab I haven’t touched the 3 config files
all bindIp are set the same way as follows
bindIp: localhost,192.168.103.100

Thank you

Hi Hasna,

From your vagrant machine, do the following:

     vagrant@m103:~$  sudo vi /etc/hosts
  • Go in insert mode by pressing i .

  • Edit the file’s first line, make it 127.0.0.1 instead of 127.0.1.1, it should look something like this:

     127.0.0.1       m103.mongodb.university m103
    
  • Save the file by pressing Esc and then press :wq. It will exit the editor and you will be back on command line.

  • Check by pinging m103 again.

     vagrant@m103:~$ ping m103
     PING m103.mongodb.university (127.0.0.1) 56(84) bytes of data.
    

Kanika

2 Likes

The problem is exactly what I mentioned:

  • The hostname m103 resolves to 127.0.1.1, not 127.0.0.1.
  • Your MongoD config tells it to listen only on 127.0.0.1 and 192.168.103.100.

So you need to add the extra IP address to the mongod config to make this work.

EDIT:
Or as Kanika says you can edit /etc/hosts to point the hostname to the right IP :slight_smile: Of course, adjusting mongod.conf is something that’s part of this class, while /etc/hosts is not :wink:

1 Like

Thank you
I have tested the solution given by @Kanika and it worded fine for me.
You were right @Tess_Sluijter but I didn’t know how to fix it. Now I know :wink:

Thank you for you help :slight_smile:

1 Like

Glad to hear that Hasna!

Kanika

I mean, changing the hosts name just makes it easier to nick name stuff. It’s best to actually get the hostname. Here’s what I did.
mongo admin --port 27011 -u m103-user -p m103-pass --authenticationDatabase admin --eval 'db.hostinfo()'
Go into to my admin database on my mongo instance@27011 and grab the hostname.

which turned out to be m103:27011
vagrant@m103:~$ mongo --host "m103-example/m103:27011" --port 27011 --authenticationDatabase "admin" -u "m103-admin" -p "m103-pass"
You could edit your hosts and make m103 resolve to whatever you have on your etc/hosts. I like this course but I hated this part of it.