0

<code>enter image description here</code>

-bash-4.2$ mongo

MongoDB shell version v4.0.2

connecting to: mongodb://127.0.0.1:27017
2018-10-13T05:21:36.532+0000 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 :: Connection refused :
connect@src/mongo/shell/mongo.js:257:13
@(connect):1:6

exception: connect failed
Victor M Perez
  • 2,185
  • 3
  • 19
  • 22
  • 1
    Possible duplicate of [mongo - couldn't connect to server 127.0.0.1:27017](https://stackoverflow.com/questions/13312358/mongo-couldnt-connect-to-server-127-0-0-127017) – IftekharDani Oct 13 '18 at 08:06

1 Answers1

0

This method only works if you want to repair your data files without preserving the original files

To find where you dbpath resides- vim /etc/mongodb.conf

check for option dbpath=

(I have dbpath=/var/lib/mongodb)

Default: /data/db/

Typical locations include: /srv/mongodb, /var/lib/mongodb or /opt/mongodb .

Replace the /var/lib/mongodb with your dbpath

sudo rm /var/lib/mongodb/mongod.lock
sudo mongod --dbpath /var/lib/mongodb/ --repair
sudo mongod --dbpath /var/lib/mongodb/ --journal

(Make sure that you leave you terminal running in which you have run above lines, dont press 'Ctrl+c' or quit it.) Type the command to start mongo now in another window.

Hope this works for you ! for those who want to repair your data files while preserving the original files mongo recover

RN Kushwaha
  • 2,081
  • 3
  • 29
  • 40