Connection failed while creating user

vagrant@m103:~$ mongo admin --host localhost:27000 --eval '
>   db.createUser({
>     user: "m103-admin",
>     pwd: "m103-pass",
>     roles: [
>       {role: "root", db: "admin"}
>     ]
>   })
> '
MongoDB shell version v3.6.16
connecting to: mongodb://localhost:27000/admin?gssapiServiceName=mongodb
2019-12-29T05:56:51.855+0000 W NETWORK  [thread1] Failed to connect to 127.0.0.1:27000, in(checking socket for error after poll), reason: Connection refused
2019-12-29T05:56:51.856+0000 E QUERY    [thread1] Error: couldn't connect to server localhost:27000, connection attempt failed :
connect@src/mongo/shell/mongo.js:263:13
@(connect):1:6
exception: connect failed
vagrant@m103:~$

Rather than just posting a command and an error message, can you please give some background on what you did previously?

I was following the Lab in first chapter, there was a code which was supposed to be copied and pasted in the server, which I did, but still gives following error

There are two things in play here:

  1. The mongod isn’t running. Check it using this command:
    ps -ef | grep [m]ongo
  2. You probably already created the first user on the localhost. You have only one chance to create the first user on the localhost. So try and login with the user:
    mongo admin --host localhost:27000 -u "m103-admin" -p "m103-pass" --authenticationDatabase admin
1 Like

vagrant 7132 6619 0 06:38 pts/0 00:00:00 grep --color=auto mongo

received this after running first command

and this after running second command -

mongo admin --host localhost:27000 -u "m103-admin" -p "m103-pass" --authenticationDatabase admin
MongoDB shell version v3.6.16
connecting to: mongodb://localhost:27000/admin?authSource=admin&gssapiServiceName=mongodb
2019-12-29T06:39:16.807+0000 W NETWORK  [thread1] Failed to connect to 127.0.0.1:27000, in(checking socket for error after poll), reason: Connection refused
2019-12-29T06:39:16.807+0000 E QUERY    [thread1] Error: couldn't connect to server localhost:27000, connection attempt failed :
connect@src/mongo/shell/mongo.js:263:13
@(connect):1:6
exception: connect failed

You ran ps -ef | grep mongo instead of ps -ef | grep [m]ongo that’s why you’re seeing this one line. If you had run the exact command I gave, you wouldn’t see any results. The line above is for the grep process that was executed and in my command I’m filtering it out. However, if there are mongod processes running, it will output one or more results.

Basically what this means is that your mongod hasn’t been started yet. So you need to startup the mongod using the requirements from the lab:

Oh, I thought that would be executed by the following command which was present.

mongod --port 27000 --dbpath /data/db --logpath /data/db/mongod.log --fork

I don’t know how to listen to the connection from that IP address, wasn’t there in previous chapters or lectures, and nothing about enabling authentication, can you please point me to it or tell me what I need to add in my code above?

Yes that command would normally startup a mongod, but it most likely failed and threw an error that’s why it never started. So each time you run this command, make sure it doesn’t show “Error” and check using the ps command as well.

To meet requirements 3 and 4, take a look in the mongod documentation to see what you can come up with.

I ran the above command as is and it worked, couldn’t figure out how to enable authentication or listen to the IP address, but somehow it worked and created the new user, thank you for your help

It worked in the sense that you have a running mongod but you still haven’t met requirements 3 and 4 so the validator will surely fail.

Now you need to find the process, kill it, and start it up with the correct set of options to meet all requirements.

Thanks @007_jb.

Hi @Sachin_54317,

As I can see, you have completed this lab so I’m closing this thread. Please feel free to create another thread if you face any other issue.

Thanks,
Shubham Ranjan
Curriculum Support Engineer