MEAN.js Could not connect to MongoDB - node.js

in creating my app with MEAN.js can not do mongodb connect on port 27017 in any way.
I installed mongodb manually in directory
~$ ls -la /etc/init.d/mongod
output
lrwxrwxrwx 1 root root 56 Nov 4 20:45 /etc/init.d/mongod -> /home/myuser/projects/packages/linux-mongodb/bin/mongod
I try to run the file or `` server.js` grunt` the NASC repository command and got the following error:
Running node server.js i receive this output:
Application loaded using the "development" environment configuration
Failed to load c ++ bson extension, using pure JS version
Insecurely using http protocol
MEAN.JS application started on port 27017
Could not connect to MongoDB!
Error: connection closed
Repository URL:
https://github.com/meanjs/mean
Some hint of what might be?
I look forward personal contact.
Thanks in advance!
Link for reference:
https://stackoverflow.com/a/22557417/3332734
running cat /etc/mongod.conf
# mongod.conf
# Where to store the data.
# Note: if you run mongodb as a non-root user (recommended) you may
# need to create and set permissions for this directory manually,
# e.g., if the parent directory isn't mutable by the mongodb user.
dbpath=/var/lib/mongodb
###dbpath=/home/myuser/projects/packages/mongodb-linux/databases
#where to log
logpath=/var/log/mongodb/mongod.log
###logpath=/home/myuser/projects/packages/mongodb-linux/mongod.log
logappend=true
port = 27017
# Listen to local interface only. Comment out to listen on all interfaces.
bind_ip = 127.0.0.1
# Disables write-ahead journaling
# nojournal = true
# Enables periodic logging of CPU utilization and I/O wait
#cpu = true
# Turn on/off security. Off is currently the default
#noauth = true
#auth = true
# Verbose logging output.
#verbose = true
# Inspect all client data for validity on receipt (useful for
# developing drivers)
#objcheck = true
# Enable db quota management
#quota = true
# Set oplogging level where n is
# 0=off (default)
# 1=W
# 2=R
# 3=both
# 7=W+some reads
#diaglog = 0
# Ignore query hints
#nohints = true
# Enable the HTTP interface (Defaults to port 28017).
#httpinterface = true
httpinterface = true
# Turns off server-side scripting. This will result in greatly limited
# functionality
#noscripting = true
# Turns off table scans. Any query that would do a table scan fails.
#notablescan = true
# Disable data file preallocation.
#noprealloc = true
# Specify .ns file size for new databases.
# nssize = <size>
# Replication Options
# in replicated mongo databases, specify the replica set name here
#replSet=setname
# maximum size in megabytes for replication operation log
#oplogSize=1024
# path to a key file storing authentication info for connections
# between replica set members
#keyFile=/path/to/keyfile
Run mongo in console and retrieve output:
$ mongo
MongoDB shell version: 2.6.5
connecting to: test
2014-11-05T19:23:18.957-0200 warning: Failed to connect to 127.0.0.1:27017, reason: errno:111 Connection refused
2014-11-05T19:23:18.958-0200 Error: couldn't connect to server 127.0.0.1:27017 (127.0.0.1), connection attempt failed at src/mongo/shell/mongo.js:146
exception: connect failed
Run mongod in console and retrieve output:
$ mongod
mongod --help for help and startup options
2014-11-05T19:26:53.215-0200 [initandlisten] MongoDB starting : pid=3487 port=27017 dbpath=/data/db 64-bit host=francis-tosystems
2014-11-05T19:26:53.215-0200 [initandlisten] db version v2.6.5
2014-11-05T19:26:53.215-0200 [initandlisten] git version: e99d4fcb4279c0279796f237aa92fe3b64560bf6
2014-11-05T19:26:53.215-0200 [initandlisten] build info: Linux build8.nj1.10gen.cc 2.6.32-431.3.1.el6.x86_64 #1 SMP Fri Jan 3 21:39:27 UTC 2014 x86_64 BOOST_LIB_VERSION=1_49
2014-11-05T19:26:53.215-0200 [initandlisten] allocator: tcmalloc
2014-11-05T19:26:53.215-0200 [initandlisten] options: {}
2014-11-05T19:26:53.215-0200 [initandlisten] exception in initAndListen: 10296
*********************************************************************
ERROR: dbpath (/data/db) does not exist.
Create this directory or give existing directory in --dbpath.
See http://dochub.mongodb.org/core/startingandstoppingmongo
*********************************************************************
, terminating
2014-11-05T19:26:53.215-0200 [initandlisten] dbexit:
2014-11-05T19:26:53.215-0200 [initandlisten] shutdown: going to close listening sockets...
2014-11-05T19:26:53.215-0200 [initandlisten] shutdown: going to flush diaglog...
2014-11-05T19:26:53.215-0200 [initandlisten] shutdown: going to close sockets...
2014-11-05T19:26:53.215-0200 [initandlisten] shutdown: waiting for fs preallocator...
2014-11-05T19:26:53.215-0200 [initandlisten] shutdown: lock for final commit...
2014-11-05T19:26:53.215-0200 [initandlisten] shutdown: final commit...
2014-11-05T19:26:53.215-0200 [initandlisten] shutdown: closing all files...
2014-11-05T19:26:53.216-0200 [initandlisten] closeAllFiles() finished
2014-11-05T19:26:53.216-0200 [initandlisten] dbexit: really exiting now
Or run mongod
$ mongod --config /etc/mongod.conf &
[1] 3568 2014-11-05T19:28:16.867-0200 SEVERE: Failed global initialization: FileNotOpen Failed to open "/var/log/mongodb/mongod.log"
EDIT
I am no longer using MEAN.js, but I continue my studies with MongoDB and node.js
The problem was that I did not know how to start mongodb to connect to the database
This year I learned a lot about mongodb and node.js and I understood what I needed to do.
With your help I have three steps:
Open a console with mongod --dbpath /project/path-do-database/
I connected my API to mongodb, passing the right port
Open a second console by starting the node.js API with node ornodemon and passing the project's main javascript file.

Basically, you need to have 2 terminals running at the same folder.
One for mongoDB, one for your application.
On the first terminal,
1>> npm install -g meanjs
1>> yo meanjs
then fill up your application name, author name, etc.
1>> npm install
Open up a 2nd terminal to start the mongodb server
2>> mongod
Go back to the first terminal and run it
1>> npm start

You are starting your meanjs app on the same port as your mongodb server (27017). If you do not specify a port for meanjs, it will start on 3000 in a development environment.

I know it is probably late, but error logs says it all:
ERROR: dbpath (/data/db) does not exist.
I belive you need to create /data/db folders or it was created somewhere else.
It is already answered here:
mongodb Mongod complains that there is no /data/db folder

Related

Impossible to connect to mongo after server shutdown

I am running a mongo cluster (v 3.4.1) on a remote Ubuntu server. This server had an unexpected shutdown yesterday. Since that, I cannot connect to mongo.
I connected by ssh to the server to check if mongo was running. It seems to be the case because if I use the mongo command, I get the following output:
MongoDB shell version v3.4.2
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.1
Server has startup warnings:
2017-09-28T08:01:43.906+0000 I STORAGE [initandlisten]
2017-09-28T08:01:43.906+0000 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2017-09-28T08:01:43.906+0000 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/prodnotes-filesystem
2017-09-28T08:01:44.204+0000 I CONTROL [initandlisten]
2017-09-28T08:01:44.204+0000 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2017-09-28T08:01:44.204+0000 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2017-09-28T08:01:44.204+0000 I CONTROL [initandlisten]
Now if I run "mongod" I get the exception:
...
2017-09-28T10:10:26.065+0200 I STORAGE [initandlisten] exception in initAndListen: 20 Attempted to create a lock file on a read-only directory: /data/db, terminating
2017-09-28T10:10:26.065+0200 I NETWORK [initandlisten] shutdown: going to close listening sockets...
...
I tried in sudo mode, I got:
...
2017-09-28T10:12:21.857+0200 E NETWORK [initandlisten] listen(): bind() failed Address already in use for socket: 0.0.0.0:27017
2017-09-28T10:12:21.857+0200 E NETWORK [initandlisten] addr already in use
2017-09-28T10:12:21.857+0200 E NETWORK [initandlisten] Failed to set up sockets during startup.
2017-09-28T10:12:21.857+0200 E STORAGE [initandlisten] Failed to set up listener: InternalError: Failed to set up sockets
2017-09-28T10:12:21.857+0200 I NETWORK [initandlisten] shutdown: going to close listening sockets...
...
Which would mean that an instance of mongo is already running...
Then I tried to kill mongo process, either with sudo service mongod stop or sudo pkill mongod or sudo kill <PID> after listing processes with ps -ef | grep mongo. But the process does not disappear from the list (seems that it restarts immediately).
So if I try to run sudo mongod --dbpath /data/db --repair (there is a mongod.lock file in the directory), I still get the error "Address already in use".
Any help would be really appreciated!

Mongodb won't start - reason: errno:111 Connection refused

All of a sudden I can't get mongodb to work.
Usually I will start with
mongo
however I get this when trying to start.
MongoDB shell version: 3.0.6
connecting to: test
2016-10-11T15:36:56.462+0100 W NETWORK Failed to connect to 127.0.0.1:27017, reason: errno:111 Connection refused
2016-10-11T15:36:56.464+0100 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 haven't updated anything on the server or even logged into it until the website broke. Any ideas why this is happening? I have tried to comment out the bind_ip but I don't get why it would just stop working and failing to start.
I have also tried
sudo service mongo start
mongod start/running, process 1529
Then when I tried -
sudo service mongod status
mongod stop/waiting.
I can't start my nodejs app because it returns
/node_modules/mongoose/node_modules/mongodb/lib/server.js:22‌​8 process.nextTick(function() { throw err; }) ^ Error: connect ECONNREFUSED at exports._errnoException (util.js:746:11) at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1010:19)
I think that is because mongodb isn't starting correctly but not sure.
The error produces this
2016-10-11T15:40:30.400+0100 E JOURNAL [initandlisten] Insufficient free space for journal files
2016-10-11T15:40:30.400+0100 I JOURNAL [initandlisten] Please make at least 3379MB available in /var/lib/mongodb/journal or use --smallfiles
2016-10-11T15:40:30.400+0100 I JOURNAL [initandlisten]
2016-10-11T15:40:30.402+0100 I STORAGE [initandlisten] exception in initAndListen: 15926 Insufficient free space for journals, terminating
2016-10-11T15:40:30.402+0100 I CONTROL [initandlisten] now exiting
2016-10-11T15:40:30.402+0100 I NETWORK [initandlisten] shutdown: going to close listening sockets...
2016-10-11T15:40:30.402+0100 I NETWORK [initandlisten] removing socket file: /tmp/mongodb-27017.sock
2016-10-11T15:40:30.402+0100 I NETWORK [initandlisten] shutdown: going to flush diaglog...
2016-10-11T15:40:30.402+0100 I NETWORK [initandlisten] shutdown: going to close sockets...
2016-10-11T15:40:30.402+0100 I STORAGE [initandlisten] shutdown: waiting for fs preallocator...
2016-10-11T15:40:30.402+0100 I STORAGE [initandlisten] shutdown: final commit...
2016-10-11T15:40:30.402+0100 I STORAGE [initandlisten] shutdown: closing all files...
2016-10-11T15:40:30.402+0100 I STORAGE [initandlisten] closeAllFiles() finished
2016-10-11T15:40:30.402+0100 I CONTROL [initandlisten] dbexit: rc: 100
2016-10-11T15:45:36.614+0100 I CONTROL ***** SERVER RESTARTED *****
2016-10-11T15:45:36.642+0100 E NETWORK [initandlisten] Failed to unlink socket file /tmp/mongodb-27017.sock errno:1 Operation not permitted
2016-10-11T15:45:36.642+0100 I - [initandlisten] Fatal Assertion 28578
2016-10-11T15:45:36.642+0100 I - [initandlisten]
***aborting after fassert() failure
2016-10-11T15:46:21.636+0100 I CONTROL ***** SERVER RESTARTED *****
2016-10-11T15:46:21.663+0100 E NETWORK [initandlisten] Failed to unlink socket file /tmp/mongodb-27017.sock errno:1 Operation not permitted
2016-10-11T15:46:21.663+0100 I - [initandlisten] Fatal Assertion 28578
2016-10-11T15:46:21.663+0100 I - [initandlisten]
I broswed to that dir and saw this!!! Why are those files so huge?
It seems that your error is similar to this
Removing the /tmp/mongodb-27017.sock file should do the trick.
Also check your disk space, as it seems the process is not starting because you are missing space for the journal files (check #dave-v comment below)
i open file /etc/mongodb.conf and put a # in begin of line bind_ip = 127.0.0.1 (#bind_ip = 127.0.0.1) .Then restart mongodb, it's work fine again !
Warning: If you use mongodb in online server, please don't do that. Hacker can attack for this port !
I faced same issue , solution is here
1) Once the mongo installation done by downloading or yum install
2) Create folder db under /home/scc-dev/data/db/
3)Goto mongo folder under bin , open terminal run the below command
sudo ./mongod dbpath /home/scc-dev/data/db
4)Check few lines command running in the console
5)open new terminal under bin run the below command
sudo ./mongo
6) type command -> show dbs (it should show default dbs like local..) then it is working fine
7) if you get error like Failed to connect to 127.0.0.1:27017, reason: errno: 61 Connection refused then
8) restart the linux m/c command - > /sbin/reboot (do the steps from 3 - issue will be resolved )
To open the any ports in linux m/c
sudo iptables -I INPUT -p tcp -m tcp --dport 27017 -j ACCEPT
sudo iptables-save

nodejs application - mongodb connection fails with error "ECONNREFUSED"

Background Information
I'm playing around with my first nodejs test app that is attempting to connect to a remote mongodb.
Problem:
The connection is failing with the following message:
admin#testdev:~/Documents/nodejs_tests$ sudo nodejs index.js
Server has started.
Request for / received
About to route a request for: /
inside db connect method
Request for /favicon.ico received
About to route a request for: /favicon.ico
inside db connect method
MongoError: connect ECONNREFUSED
null
MongoError: connect ECONNREFUSED
null
Here's the logic to connect:
function connect_nimble() {
console.log("inside db connect method");
MongoClient.connect("mongodb://10.1.1.1:27017/test", function(err,db) {
console.log(err);
console.log(db);
if(!err) {
console.log("booya! Connected to mongo");
return true;
}
});
}
What I've checked so far:
I've made sure that the database server is using port 27017. Here's what I see in the mongodb logs when i restart the database:
admin#mongotest:~$ tail -f /var/log/mongodb/mongod.log
2015-07-21T09:52:41.452-0500 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2015-07-21T09:52:41.452-0500 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2015-07-21T09:52:41.452-0500 I CONTROL [initandlisten]
2015-07-21T09:52:41.452-0500 I CONTROL [initandlisten] db version v3.0.4
2015-07-21T09:52:41.452-0500 I CONTROL [initandlisten] git version: 0481c958daeb2969800511e7475dc66986fa9ed5
2015-07-21T09:52:41.452-0500 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.1f 6 Jan 2014
2015-07-21T09:52:41.452-0500 I CONTROL [initandlisten] build info: Linux ip-10-45-73-23 3.13.0-24-generic #46-Ubuntu SMP Thu Apr 10 19:11:08 UTC 2014 x86_64 BOOST_LIB_VERSION=1_49
2015-07-21T09:52:41.452-0500 I CONTROL [initandlisten] allocator: tcmalloc
2015-07-21T09:52:41.452-0500 I CONTROL [initandlisten] options: { config: "/etc/mongod.conf", net: { bindIp: "127.0.0.1" }, storage: { dbPath: "/var/lib/mongodb" }, systemLog: { destination: "file", logAppend: true, path: "/var/log/mongodb/mongod.log" } }
**2015-07-21T09:52:41.470-0500 I NETWORK [initandlisten] waiting for connections on port 27017**
I've made sure I have a database called "test" by starting "mongo"... and then switching in and out of the test database using the "use test" / "use admin" commands.
I don't believe I have authorization turned on. This is what I have in the mongo.conf file:
# Turn on/off security. Off is currently the default
#noauth = true
#auth = true
And to prove it, in order to get into the test database on the server via command line, I just have to type "mongo" and then by default, I'm in the test database. So I think I can safely assume I don't have any authentication going on.
Just in case, I also checked netstat for the port number:
admin#mongotest:~$ sudo netstat -taupen | grep mongo
tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN 117 6465150 18792mongod
admin#mongotest:~$
I'm currently reading the manual to see if there's anything else I should be checking. I'm also reading through the various stackoverflow posts that are similar, but I haven't found me an answer yet.
Any suggestions?
I can see in your logs that Mongo is listening on 127.0.0.1. This is likely your problem. You will have to change the bind-ip address configuration setting to allow Mongo to listen to specific or all IP addresses. You can find how to do this here: Cannot connect to mongodb using machine ip
Open a terminal.
Type mongod & and check the stacktrace.
This is an example of a failing start
I STORAGE [initandlisten] exception in initAndListen: 29 Data directory /data/db not found., terminating
I NETWORK [initandlisten] shutdown: going to close listening sockets...
I NETWORK [initandlisten] shutdown: going to flush diaglog...
I CONTROL [initandlisten] now exiting
To fix this, Create the folder /data/db
In the terminal, type mkdir /data/db
Type againmongod
It works !!
[initandlisten] ** WARNING: Access control is not enabled for the database.
I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
I CONTROL [initandlisten]
I CONTROL [initandlisten]
I CONTROL [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000

Failed to connect to 127.0.0.1:27017, reason: errno:111 Connection refused

while trying this mongo command in ubuntu
I am getting this error.
ritzysystem#ritzysystem-Satellite-L55-A:~$ mongo
MongoDB shell version: 2.6.1
connecting to: test
2014-10-06T12:59:35.802+0530 warning: Failed to connect to 127.0.0.1:27017, reason: errno:111 Connection refused
2014-10-06T12:59:35.802+0530 Error: couldn't connect to server 127.0.0.1:27017 (127.0.0.1), connection attempt failed at src/mongo/shell/mongo.js:146
exception: connect failed
how can I rectify this is anyone had the same problem.
Run the following command :
sudo rm /var/lib/mongodb/mongod.lock
sudo service mongod restart
Connection refused to MongoDB errno 111
MacOS:
rm /usr/local/var/mongodb/mongod.lock
sudo service mongod restart
I had the same problem on Mac OS
Try to run sudo mongod and in a new terminal tab run mongo
thank you everyone I tried running mongo it was not running finally I found there was no space in my hard disc I emptied some of the space and finally re installed it the same installation procedure as followed to install as given in mongodb docs finally restarted the server and it is running.
https://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu
In my case the error was due to missing /data/db folder which the mongodb uses to store your data. Please type this command $sudo mongod in your terminal. If the error message is something like :
missing data/db folder error
Simply create the folder and you are good to go.
First,
Start MongoDB:
sudo service mongod start
Then, Run:
mongo
Probably you don't have space in your hard drive.
Check it by typing in the promt df -h
Please note that mongo might fail even with 3gb available in the corresponding partition. For details, you might want to check the log: cat /var/log/mongodb/mongod.log
Make sure you have run the MongoDB's Service before you want to connect the console.
run and start the server:
$ sudo mongod
then:
$ mongo
...
>
I had the same problem on my Mac, and I installed mongodb via homebrew. I solve this problem by the homebrew services command.
First start mongodb service:
$ brew services start mongodb
Start mongodb terminal
$ mongo
Stop mongodb service:
$ brew services stop mongodb
Do this:
sudo rm /var/lib/mongodb/mongod.lock
sudo service mongod restart
If you are on Ubuntu 16.04 which you can figure out by runnign this:
lsb_release -a
You need to Create a new file at /lib/systemd/system/mongod.service with the following contents:
[Unit]
Description=High-performance, schema-free document-oriented database
After=network.target
Documentation=https://docs.mongodb.org/manual
[Service]
User=mongodb
Group=mongodb
ExecStart=/usr/bin/mongod --quiet --config /etc/mongod.conf
[Install]
WantedBy=multi-user.target
In my case the problem was caused due to an apparent lost of permission over mongodb.lock file. I could solve the problem changing the permission with the following command :
sudo chown mongodb:mongodb /var/lib/mongodb/mongodb.lock
There follows my investigation:
I think except for disk space issuse, you should check the log in /var/log/mongodb to know the details for why mongodb start failed.
cat /var/log/mongodb/mongod.log
2016-06-26T15:26:26.642+0800 I CONTROL [main] ***** SERVER RESTARTED *****
2016-06-26T15:26:26.649+0800 I CONTROL [initandlisten] MongoDB starting : pid=8130 port=27017 dbpath=/var/lib/mongodb 64-bit host=hadoop-master
2016-06-26T15:26:26.649+0800 I CONTROL [initandlisten] db version v3.2.7
2016-06-26T15:26:26.649+0800 I CONTROL [initandlisten] git version: 4249c1d2b5999ebbf1fdf3bc0e0e3b3ff5c0aaf2
2016-06-26T15:26:26.649+0800 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.1f 6 Jan 2014
2016-06-26T15:26:26.649+0800 I CONTROL [initandlisten] allocator: tcmalloc
2016-06-26T15:26:26.649+0800 I CONTROL [initandlisten] modules: none
2016-06-26T15:26:26.649+0800 I CONTROL [initandlisten] build environment:
2016-06-26T15:26:26.649+0800 I CONTROL [initandlisten] distmod: ubuntu1404
2016-06-26T15:26:26.649+0800 I CONTROL [initandlisten] distarch: x86_64
2016-06-26T15:26:26.649+0800 I CONTROL [initandlisten] target_arch: x86_64
2016-06-26T15:26:26.649+0800 I CONTROL [initandlisten] options: { config: "/etc/mongod.conf", net: { bindIp: "127.0.0.1,192.168.3.10", port: 27017 }, storage: { dbPath: "/var/lib/mongodb", journal: { enabled: true } }, systemLog: { destination: "file", logAppend: true, path: "/var/log/mongodb/mongod.log" } }
2016-06-26T15:26:26.678+0800 E NETWORK [initandlisten] Failed to unlink socket file /tmp/mongodb-27017.sock errno:1 Operation not permitted
2016-06-26T15:26:26.678+0800 I - [initandlisten] Fatal Assertion 28578
2016-06-26T15:26:26.678+0800 I - [initandlisten]
***aborting after fassert() failure
So, here I need to rm all the files in the /tmp. That works fine for me.
For mongo v3.6.3+ (or 2019 versions)
rm /var/lib/mongodb/mongod.lock
service mongodb restart
I understand the question is with regards to Ubuntu.
But if you're getting the same error on a Mac and don't want $ sudo mongod running on a separate tab, you can do the following to fix the connection error, if you have Homebrew -
brew services start mongodb
Based on the answer from this thread - Cannot connect to mongodb errno:61 Connection refused
In my case bind_ip was not 127.0.0.1 in /etc/mongodb.conf file so change bind_ip to 127.0.0.1(it may be comma separated values so make sure 127.0.0.1 is one of them) Then restart your system to take effect.
Restart only to the those who are facing
$sudo service mongod restart
Failed to restart mongod.service: Unit mongod.service not found.
Follow this simple steps; (Works on MAC OS too)
Open terminal and run sudo mongod
Open a new terminal tab(Don't close step 1 tab) and run sudo mongo
That's all
is very simple, only delete a file /var/lib/mongodb/mongodb.lock. after only execute: mongo. finished
It is possible that the data directory for mongo specified in /etc/mongod.conf is not a valid path.
Try rechecking the path if it really exists sudo vi /etc/mongod.conf and check dbPath.
Just some thoughts on my case.
If you have changed the dbPath and logPath dirs to your custom values (say /data/mongodb/data, /data/mongodb/log), you must chown them to mongodb user, otherwise, the non-existent /data/db/ dir will be used.
sudo chown -R mongodb:mongodb /data/mongodb/
sudo service mongod restart
I have the same error with you, this is my case:
~# mongod
2018-07-15T05:27:08.265+0000 I JOURNAL [initandlisten] journal dir=/data/db/journal
2018-07-15T05:27:08.265+0000 I JOURNAL [initandlisten] recover : no journal files present, no recovery needed
2018-07-15T05:27:08.301+0000 I JOURNAL [durability] Durability thread started
2018-07-15T05:27:08.301+0000 I JOURNAL [journal writer] Journal writer thread started
2018-07-15T05:27:08.302+0000 I CONTROL [initandlisten] MongoDB starting : pid=26796 port=27017 dbpath=/data/db 64-bit host=ubuntu-s-2vcpu-4gb-sfo2-01
2018-07-15T05:27:08.302+0000 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2018-07-15T05:27:08.302+0000 I CONTROL [initandlisten]
2018-07-15T05:27:08.302+0000 I CONTROL [initandlisten]
2018-07-15T05:27:08.302+0000 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2018-07-15T05:27:08.302+0000 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2018-07-15T05:27:08.302+0000 I CONTROL [initandlisten]
2018-07-15T05:27:08.302+0000 I CONTROL [initandlisten] db version v3.0.6
2018-07-15T05:27:08.302+0000 I CONTROL [initandlisten] git version: 1ef45a23a4c5e3480ac919b28afcba3c615488f2
2018-07-15T05:27:08.302+0000 I CONTROL [initandlisten] build info: Linux build6.ny.cbi.10gen.cc 2.6.32-431.3.1.el6.x86_64 #1 SMP Fri Jan 3 21:39:27 UTC 2014 x86_64 BOOST_LIB_VERSION=1_49
2018-07-15T05:27:08.302+0000 I CONTROL [initandlisten] allocator: tcmalloc
2018-07-15T05:27:08.302+0000 I CONTROL [initandlisten] options: {}
2018-07-15T05:27:08.308+0000 I NETWORK [initandlisten] waiting for connections on port 27017
I type mongod to start the server, and I type control + c to exit to shell
then I type mongo and I got
~# mongo
MongoDB shell version: 3.0.6
connecting to: test
2018-07-15T05:05:02.738+0000 W NETWORK Failed to connect to 127.0.0.1:27017, reason: errno:111 Connection refused
2018-07-15T05:05:02.739+0000 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
As you can see, my error info is same with you.
This is case mongod doesn't start with backend process, when I type control + c I quit mongod.
We can add --fork args to make the process daemon process.
# mongod --logpath /usr/local/mongodb/log.txt --fork
you have to set --logpath if you want to use --fork
then you will success to connect to mongo
I faced the same problem, so please check is there mongodb folder, firstly try to remove that folder
rm -rf /var/lib/mongodb/*
Now try to start mongo and if same problem then its mongodb lock file which prevent to start mongo, so please delete the mongo lock file
rm /var/lib/mongodb/mongod.lock
For forcefully rm -rf /var/lib/mongodb/mongod.lock
service mongodb restart if already in sudo mode
otherwise you need to use like sudo service mongod start
or you can start the server using fork method
mongod --fork --config /etc/mongod.conf
and for watching the same is it forked please check using the below command
ps aux | grep mongo
Mongo 3.*.* - OSX - 2017
From README
RUNNING
Change directory to mongodb-osx-x86_64-3.*.*/bin
To run a single server database:
$ mkdir /data/db
$ ./mongod
Go to new Terminal
$ # The mongo javascript shell connects to localhost and test database
$ # by default -Run the following command in new terminal
$ ./mongo
> help
I solved that simply by entering sudo mongo after mongod command.
I just removed mongo following the instructions here: https://docs.mongodb.com/manual/tutorial/install-mongodb-on-ubuntu/ And then I installed again following the instructions in the given link. Everything worked smoothly!
There are changes in mongod.conf file in the latest MongoDB v 3.6.5 +
Here is how I fixed this issue on mac os High Sierra v 10.12.3
Note: I assume that you have installed/upgrade MongoDB using homebrew
mongo --version
MongoDB shell version v3.6.5
git version: a20ecd3e3a174162052ff99913bc2ca9a839d618
OpenSSL version: OpenSSL 1.0.2o 27 Mar 2018
allocator: system modules: none build environment:
distarch: x86_64
target_arch: x86_64
find mongod.conf file
sudo find / -name mongod.conf`
/usr/local/etc/mongod.conf > first result .
open mongod.conf file
sudo vi /usr/local/etc/mongod.conf
edit in the file for remote access under net: section
port: 27017
bindIpAll: true
#bindIp: 127.0.0.1 // comment this out
restart mongodb
if you have installed using brew than
brew services stop mongodb
brew services start mongodb
otherwise, kill the process.
sudo kill -9 <procssID>
Issue was with the connection to the database, it could be due to several reasons.
For me it was low disk space on my server machine, and when mongo tries to connect it refuse because of low disk, check your server machine local storage with du -sh if you have low storage space, check your logs size and take step accordingly, issue may exist because of dbpath contradict in /etc/mongod.conf and server actually running dbpath with mongod
If you are facing such type of issue, please check my answer in the given link below.
https://stackoverflow.com/a/53057695/8247133
For those with Windows. I had the same problem on Windows.
Restarting Mongodb service solves this.
Following are the steps to restart on Windows:
i). Go To "Services" on Windows.
ii). Search for "MongoDB" in the list of services.
iii). Right click "start"/"restart" depending on your status.
iv). After the status turns to "running" check if issue is resolved by running "Mongo" or connecting your Mongo client.
For macOS many of the answers are already outdated according to official docs. Somehow the brew has changed and how we should install MongoDB, firstly uninstall it from your macOS (even though it might be not necessary) and then install it using these steps:
brew tap mongodb/brew
brew install mongodb-community#4.4
brew services start mongodb-community#4.4
Watch out for the 4.4 part, it'll change. If in the future it would break then refer to the official docs and install the version, which is suggested in the linked tutorial.

Mongo daemon doesn't run by service mongod start

Recently I installed mongodb package by reading this page (http://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/).
I use docker and ubuntu 14.04 image to deploy my server.
The problems start by first running the mongod service:
# service mongod start
I got the following:
Rather than invoking init scripts through /etc/init.d, use the service(8)
utility, e.g. service mongod restart
Since the script you are attempting to invoke has been converted to an
Upstart job, you may also use the stop(8) and then start(8) utilities,
e.g. stop mongod ; start mongod. The restart(8) utility is also available.
I've tried this:
# start mongod
But got no output.
Next I wanted to check the log, but there is no log!
ls /var/log/mongodb -a #empty
Ok, next I tried to start mongo shell:
# mongo
2014-08-12T17:42:44.431+0000 warning: Failed to connect to 127.0.0.1:27017, reason: errno:111 Connection refused
2014-08-12T17:42:44.432+0000 Error: couldn't connect to server 127.0.0.1:27017 (127.0.0.1), connection attempt failed at src/mongo/shell/mongo.js:146
exception: connect failed
Ok, after googling I checked (applaied) all answers from Mongodb in linux server and https://wiki.archlinux.org/index.php/MongoDB (section Troubleshooting), but still get nothing.
The mongo shell works only when I run mongod directly in background:
mongod --verbose &
[DataFileSync] BackgroundJob starting: DataFileSync
shardKeyTest passed
isInRangeTest passed
shardObjTest passed
[initandlisten] MongoDB starting : pid=451 port=27017 dbpath=/data/db 64-bit host=a9d816faea4c
[initandlisten] db version v2.6.4
[initandlisten] git version: 3a830be0eb92d772aa855ebb711ac91d658ee910
[initandlisten] build info: Linux build7.nj1.10gen.cc 2.6.32-431.3.1.el6.x86_64 #1 SMP Fri Jan 3 21:39:27 UTC 2014 x86_64 BOOST_LIB_VERSION=1_49
[initandlisten] allocator: tcmalloc
[initandlisten] options: { systemLog: { verbosity: 1 } }
[initandlisten] flushing directory /data/db
[initandlisten] journal dir=/data/db/journal
[initandlisten] recover : no journal files present, no recovery needed
[initandlisten] flushing directory /data/db/journal
[initandlisten] flushing directory /data/db/journal
[initandlisten] opening db: local
[initandlisten] enter repairDatabases (to check pdfile version #)
[initandlisten] local
[initandlisten] done repairDatabases
[initandlisten] opening db: admin
[initandlisten] query admin.system.roles planSummary: EOF ntoreturn:0 ntoskip:0 keyUpdates:0 numYields:0 locks(micros) W:119 r:106 nreturned:0 reslen:20 0ms
[ClientCursorMonitor] BackgroundJob starting: ClientCursorMonitor
[PeriodicTaskRunner] BackgroundJob starting: PeriodicTaskRunner
[TTLMonitor] BackgroundJob starting: TTLMonitor
[initandlisten] fd limit hard:1048576 soft:524288 max conn: 419430
[IndexRebuilder] BackgroundJob starting: IndexRebuilder
[IndexRebuilder] opening db: local
[initandlisten] create collection local.startup_log { size: 10485760, capped: true }
[initandlisten] command local.$cmd command: create { create: "startup_log", size: 10485760, capped: true } ntoreturn:1 keyUpdates:0 numYields:0 reslen:75 0ms
[initandlisten] insert local.startup_log ninserted:1 keyUpdates:0 numYields:0 0ms
[initandlisten] waiting for connections on port 27017
[IndexRebuilder] checking complete2014-08-12T17:48:29.837+0000 [DataFileSync] BackgroundJob starting: DataFileSync
Now I have the following:
/var/lib/mongodb (mongodb:mongodb) empty
/var/log/mongodb (mongodb:nogroup) empty
/data/db (mongo:nogroup) #useless
# mongod.conf
dbpath=/var/lib/mongodb
logpath=/var/log/mongodb/mongod.log
logappend=true
port = 27017
bind_ip = 0.0.0.0
...
What's going on here? I'm absolutely confused (
Docker containers typically does not have a full init system and interaction with upstart will not work inside a docker container. (In theory it is possible, but it defeats the purposes of having light weight stack)
What this implies is that you start a docker container, it would run a single command "/usr/bin/mongod"
Example of running mongodb inside a docker container:
https://docs.docker.com/samples/library/mongo/
Also since you are running installation commands using an interactive docker container, your shell interpreter is the single command as far as docker is considered. Once in interactive session, you can run mongod in background (As you did) and start mongo client session.
Another way would be to run these instructions as part of Dockerfile. You can refer to mongodb example.
You might also want to consider some of the official mongo db images already published in docker hub:
https://registry.hub.docker.com/_/mongo/

Resources