0
votes

I just started learning MongoDB. When I run for the first time, I get an error. MongoDB 4 version Windows 10 x64

MongoDB shell version v4.0.1 connecting to: mongodb://127.0.0.1:27017 2018-08-13T00:15:54.149+0300 E QUERY [js] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: No connection could be made because the target machine actively refused it. : connect@src/mongo/shell/mongo.js:257:13 @(connect):1:6 exception: connect failed

3
That's the shell, You also have to start mongod, the server.Sergio Tulentsev

3 Answers

3
votes

Make sure you created the /data/db at the root. otherwise, do the simple steps.

For Mac or Linux user

1 - first open the terminal as root.

sudo bash

2 - make db folder

mkdir -p /data/db

3 - give the chmod 777 to the directory.

chmod 777 /data
chmod 777 /data/db

4 - confirm the /data/db is 777

ls -ld /data/db

5 - exit the root shell

6 - run the mongod cmd at mongodb_path/bin directory.

mongod

make sure it is listing port. By default, it listens to the 27017 port.

7- open the new terminal window and run the mongo cmd at mongodb_path/bin directory.

mongo

For window user

1- create the /data/db at like C:\data\db

2- open the terminal at mongodb_path/bin

mongod

make sure it is listing port. By default, it listens to the 27017 port.

3- open the new terminal window at mongodb_path/bin

mongo
0
votes

Please check weather bellow steps did u follow:

1) Install MongoDB

2) create path to store data (eg: C:\data\db)

3) Go to bin location from cmd

 cd C:\Program Files\MongoDB\Server\4.XXX\bin

4) Then enter

mongod

5) Then open another cmd and enter

 mongo
0
votes

In some cases it may happen if you try to reach a mongodb server from another ip and it stops you because of mongodb configurations.

At the location you have installed mongodb (in my computer it is C:\Program Files\MongoDB\Server\4.0\bin) you will see the config file: mongod.cfg. When you open the file for editing you will find some parameters under net part. As default, it is like the following:

network interfaces

net:

port: 27017

bindIp: 127.0.0.1

But if you want to connect to mongodb from another machine you have to change it as following:

network interfaces

net:

port: 27017

bindIp: 0.0.0.0

When you change it as 0.0.0.0, it means it accepts connections from all ip.