mongoDb : connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 - node.js

-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

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

Related

mongodb connect fail in osx

I'm newer to mongode, I use shell to start mongodb service but failed and get following error. What should I do?
mongod log
enter image description here
mongo log
MongoDB shell version v3.4.4
connecting to: mongodb://127.0.0.1:27017
2017-05-07T07:59:40.141+1000 W NETWORK [thread1] Failed to connect to 127.0.0.1:27017, in(checking socket for error after poll), reason: Connection refused
2017-05-07T07:59:40.142+1000 E QUERY [thread1] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed :
connect#src/mongo/shell/mongo.js:237:13
#(connect):1:6
exception: connect failed
Try this. Open terminal, and at the prompt type:
sudo chown -R `id -un` /data/db
Exactly as written. This will give you write access to the /data/db directory which is what Mongo is complaining about. It should prompt you for your password. Once it does enter it and hit enter. After that, kill mongod, if running, and restart.
Make sure the MongoDB server (mongod) is listening on the right interface.
Listening on 127.0.0.1 (loopback interface) will only work locally. If you want to handle remote requests you will have to listen on a network interface that can be accessed remotely. Listening on 0.0.0.0 will listen on all network interfaces.
Step 1: Please check whether you have C:\data\db. If you dont have it create it manually.
Step 2: in command line go to directory of mongodb such as c:\Program files\Mongodb\server\3.4\bin and type *mongod
Step 3: type Mongo
Hopefully its gonna work out for you.

Mongoose - remote database connect failed

I am trying to connect to my mongo database in my remote server:
$ mongo xxx.xxx.xxx.xx:27017
But I get this error:
MongoDB shell version: 2.6.10 connecting to: xxx.xxx.xxx.xx:27017/test
2016-11-07T05:18:39.140+0000 warning: Failed to connect to
xxx.xxx.xxx.xx:27017, reason: errno:111 Connection refused
2016-11-07T05:18:39.142+0000 Error: couldn't connect to server
xxx.xxx.xxx.xx:27017 (xxx.xxx.xxx.xx), connection attempt failed at
src/mongo/shell/mongo.js:148 exception: connect failed
Any ideas why?
I even have tried to connect it with an username and password but it still fails to connect...
Run this following command :
sudo service mongodb stop
sudo rm /var/lib/mongodb/mongod.lock
sudo service mongod restart
mongo

How to run mongodb again after executing db.shutdownServer(); in the admin db

I was just learning mongodb. I command I entered are :
use admin
db.shutdownServer();
This shutdown the database. After that whenever I'm writing mongo to start mongoDB it gives me :
MongoDB shell version: 3.0.12 connecting to: test
2016-10-09T18:24:27.661+0530 W NETWORK Failed to connect to
127.0.0.1:27017, reason: errno:111 Connection refused 2016-10-09T18:24:27.664+0530 E QUERY Error: couldn't connect to
server 127.0.0.1:27017 (127.0.0.1), connection attempt failed
at connect (src/mongo/shell/mongo.js:179:14)
at (connect):1:6 at src/mongo/shell/mongo.js:179 exception: connect failed
When I run this : sudo service mongodb start
I get :
Redirecting to /bin/systemctl start mongodb.service Failed to start
mongodb.service: Unit mongodb.service failed to load: No such file or
directory.
I tried other different commands (even I've re installed it again) but none of them restarted mongodb.
How to start mongodb?
shut down the node
:SECONDARY> db.shutdownServer()
up
:~$ mongod --config /db/mongod-repl-2.conf
:SECONDARY> rs.slaveOk()
:SECONDARY> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
testDatabase 0.000GB
You need to fork your service again,
Try
mongod --dbpath /var/lib/mongo/ --fork --logpath /var/log/mongodb/mongod.log

mongo db can't start ubuntu 15.10

sudo service mongodb start
Failed to start mongodb.service: Unit mongodb.service is masked.
What should I do? help me..
~$ mongo MongoDB shell version: 3.2.3 connecting to: test
2016-02-26T17:40:18.552+0200 W NETWORK [thread1] Failed to connect to
127.0.0.1:27017, reason: errno:111 Connection refused 2016-02-26T17:40:18.552+0200 E QUERY [thread1] Error: couldn't
connect to server 127.0.0.1:27017, connection attempt failed :
connect#src/mongo/shell/mongo.js:224:14 #(connect):1:6
exception: connect failed
I think it's sudo service mongod start - please note the lack of b in mongod
I find the solution;
sudo mongod --dbpath /var/lib/mongodb/ --journal
different terminal is opening and
mongo
command is starting with

MongoDB Data directory /data/db not found [duplicate]

This question already has answers here:
Mongod complains that there is no /data/db folder
(28 answers)
Closed 7 years ago.
When I try to run Mongod in terminal I got this message :
2015-05-14T17:33:04.554+0700 I STORAGE [initandlisten] exception in initAndListen: 29 Data directory /data/db not found., terminating
2015-05-14T17:33:04.554+0700 I CONTROL [initandlisten] dbexit: rc: 100
and running mongo command :
MongoDB shell version: 3.0.3
connecting to: test
2015-05-14T17:34:26.679+0700 W NETWORK Failed to connect to 127.0.0.1:27017, reason: errno:111 Connection refused
2015-05-14T17:34:26.681+0700 E QUERY Error: couldn't connect to server 127.0.0.1:27017 (127.0.0.1), connection attempt failed
at connect (src/mongo/shell/mongo.js:179:14)
at (connect):1:6 at src/mongo/shell/mongo.js:179
exception: connect failed
I Have tried to change permision in /var/lib/mongodb and /var/log/mongodb, but still doesnt work, and I tried to uninstall and install mongodb again, but still the same problem exist.
Anyone could help? Thanks
I'm using Ubuntu 14.04 LTS 64 bit
MongoDB needs data directory to store data.
Default path is /data/db
When you start MongoDB engine, it searches this directory which is missing in your case. Solution is create this directory and assign rwx permission to user.
If you want to change the path of your data directory then you should specify it while starting mongod server like,
mongod --dbpath /data/<path> --port <port no>
This should help you start your mongod server with custom path and port.

Resources