Unable to connect to mongod from mongo shell

I’m having issues with connection to mongod from mongo shell. I was able to successfully connect to mongod from mongo shell for the first time but creation of collection was not reflected in mongod although it was successfully created in mongo shell. Also, shutdownServer command failed with error unable to connect to the server. So, I tried quitting from the shell and now it fails when I try to bring up the shell.

image

Not sure what I’m missing here. Timely help would be greatly appreciated as I cannot proceed further with the labs.

Are you using vagrant or local Windows host for your work?
How did you connect first time?
Can you show us the screenshot

1 Like

HI @Aravinthkumar_74587,

If you are doing this for the lab, then you should first ssh into the running virtual machine by running this command vagrant ssh.

Please share the information requested by @Ramachandra_37567 if your issue is not resolved.

~ Shubham

Hello guys, thanks for your response.

Yes, I’m using Vagrant and connected via command vagrant ssh.

I tried stopping the VM using command vagrant halt and have restarted my machine.

So, this is treated as first time connection and provisioning was successful via vagrant up --provision. Then vagrant ssh was also successful followed by successful launch of mongod. Please see screenshot below as requested.


Then I tried launching mongo shell using second command prompt. Its successful as well. Please see screenshot below.

But shutdownServer command failed as shown below.

Since I’m not able to shutdown from mongo shell, I simply quit by typing exit command.

I get the below error when I try to connect to mongod again from the shell. Please see below.

Hope I’ve included all the information that you need. Please advise.

Just to add to it, although collection employees was created, it didn’t appear on mongod. It still shows up as waiting for connections on port 27017.

I think you are mixing up sessions
Where did you start your mongod?

Are you using vagrant or localhost for your work?
You have to do vagrant ssh and perform all operations there
Please check again

The mistake you are doing is you are trying to connect to your mongod started on Vagrant Linux box from Windows.It won’t work

In the second cmd prompt first run vagrant ssh then connect to your mongod

On windows mongod runs as service and will be up and running.Thats why you are able to connect it by just issuing mongo

Try the same on Linux box.It will fail with connect error
Always start mongod with specific port and connect using that port

mongod --port xyz
mongo --port xyz

Hope it is clear

Sorry, I’m still confused.

I started VM from first cmd prompt via ssh and connected to mongod. Then in the second cmd prompt, I issued mongo.

Can you please correct me if the above sequence is not correct? Thanks again.

Please start another VM from second cmd prompt and try to connect
You should be on m103 box always

I tried destroying the VM and set it up again using command vagrant up --provision but getting an error. Please see screenshot below.

Could be network/antivirus/vpn issues

GPG: Keyserver timed out

1 Like

In addition to @Ramachandra_37567, it also turns out that the key that is referenced in the provision file is incorrect.

To get the correct key, see this post for a fix.

The provisioning was successful after I tweaked the firewall setup but I’m still having issues connecting mongod from mongo shell. Still getting the same error as in my previous post.

What error?
exception connect failed?

Please don’t run your mongod and mongo commands at Windows cmd prompt
I am repeating this again and again
You have to login to vagrant Linux box by vagrant ssh
You can open as many sessions as you want but all work should be done on Linux box only
You cannot mix Linux and Windows sessions

1 Like

Perhaps @Aravinthkumar_74587 should share a screenshot of the exact command and error message as of now.

I had shared all the screenshots in my previous post. Anyways, it worked now. I didn’t understand @Ramachandra_37567 's point in running both mongod & mongo shell from Vagrant. I was running mongod from vagrant but not the mongo shell. Thanks again guys. That was my fault.

Hi @Aravinthkumar_74587,

I’m glad your issue got resolved. Please feel free to get back to us if you face any other difficulty.

~ Shubham

It might sound stupid but I am getting this error when I try to connect to mongo and I am not using the Windows cmd, I am at vagrant and I’m getting the error couldn’t connect to the server.

It is not stupid at all.

That is the normal behaviour if no server is running. You might issue ps -aef | grep [m]ongod to see if any is running.

2 Likes

Your mongod is not up and running on port 27017
That’s why your mongo command failing
You can verify by ps -efgrep mongo as steevej-1495 mentioned

run mongod-it will bring up mongod on default port
then you can connect by issuing mongo

2 Likes