MongoDB error: Cannot Connect to Server

If you are reading this post, chances are that you are having difficulties connecting mongodb to the server using command prompt.

visual of the error

This is a common problem for Windows users, but not to worry here is a simple solution assuming of course you have installed mongodb correctly;

  1. Set dbpath if it is not already set. Add it to your PATH of environment variables, it Should be from: C:\Program Files\MongoDB\Server\3.6\bin

click on advanced system settings

you can access this by left clicking on computer on clicking properties
click on environment variables

Edit the path to include dbpath (include it after the last semi-colon).

Just like this.

2. Create a data folder in your system (also create a db folder inside your data folder) i.e C:\username\data\db

3. Open a new cmd prompt window bin directory of Mongo and then type in the command

=> mongod

You should see something like the picture below and then it listens to a port.

After that is done, open another command prompt, leaving the previous one running the server.

Type in the command prompt

=> mongo

It should display the shell version and connect to a database.

This means you have successfully connected to the server.

Leave a Reply