How to restart/delete an instance of mongod? - mongodb

This has been covered in other SO posts, but unfortunately those solutions are not working.
How does one restart/delete a mongod instance?
Entering
$ mongod
I get the following error:
2016-01-24T09:49:19.557-0500 I CONTROL [initandlisten] MongoDB starting : pid=43675 port=27017 dbpath=/data/db 64-bit host=NAME-MacBook-Pro.local
2016-01-24T09:49:19.558-0500 I CONTROL [initandlisten] db version v3.0.8
2016-01-24T09:49:19.558-0500 I CONTROL [initandlisten] git version: 83d8cc25e00e42856924d84e220fbe4a839e605d
2016-01-24T09:49:19.558-0500 I CONTROL [initandlisten] build info: Darwin NAME-MacBook-Pro.local 15.2.0 Darwin Kernel Version 15.2.0: Fri Nov 13 19:56:56 PST 2015; root:xnu-3248.20.55~2/RELEASE_X86_64 x86_64 BOOST_LIB_VERSION=1_49
2016-01-24T09:49:19.558-0500 I CONTROL [initandlisten] allocator: system
2016-01-24T09:49:19.558-0500 I CONTROL [initandlisten] options: {}
2016-01-24T09:49:19.558-0500 E NETWORK [initandlisten] listen(): bind() failed errno:48 Address already in use for socket: 0.0.0.0:27017
2016-01-24T09:49:19.558-0500 E NETWORK [initandlisten] addr already in use
2016-01-24T09:49:19.558-0500 I STORAGE [initandlisten] exception in initAndListen: 98 Unable to create/open lock file: /data/db/mongod.lock errno:13 Permission denied Is a mongod instance already running?, terminating
2016-01-24T09:49:19.558-0500 I CONTROL [initandlisten] dbexit: rc: 100
I explicitly gave myself permissions with
sudo chown -R <USERNAME> /data/db
This didn't work.
I tried to remove the directory entirely
sudo rm -rf /data/db
and recreate it
mkdir -p /data/db
but unfortunately this doesn't work.
I thought the solution would be to remove the file /data/db/mongod.lock but it doesn't exist:
rm /data/db/mongod.lock
Anything else I should try?
EDIT: After running with the different port using mongod --port 12345, when I use use admin and then db.shutdownServer(), I get the following error:
2016-01-24T10:32:25.039-0500 I NETWORK trying reconnect to 127.0.0.1:27017 (127.0.0.1) failed
2016-01-24T10:32:25.039-0500 I NETWORK reconnect 127.0.0.1:27017 (127.0.0.1) ok
2016-01-24T10:32:25.040-0500 I NETWORK DBClientCursor::init call() failed
server should be down...
EDIT 2:
Restarting the machine does nothing. I've tried to access the default port with mongod --port 27017, and then
> use admin
> db.shutdownServer()
I get the output:
2016-01-25T12:05:32.163-0500 I NETWORK DBClientCursor::init call() failed
server should be down...
2016-01-25T12:05:32.166-0500 I NETWORK trying reconnect to 127.0.0.1:27017 (127.0.0.1) failed
2016-01-25T12:05:32.166-0500 W NETWORK Failed to connect to 127.0.0.1:27017, reason: errno:61 Connection refused
2016-01-25T12:05:32.166-0500 I NETWORK reconnect 127.0.0.1:27017 (127.0.0.1) failed failed couldn't connect to server 127.0.0.1:27017 (127.0.0.1), connection attempt failed

In the log it say:
2016-01-24T09:49:19.558-0500 E NETWORK [initandlisten] listen(): bind() failed errno:48 Address already in use for socket: 0.0.0.0:27017
2016-01-24T09:49:19.558-0500 E NETWORK [initandlisten] addr already in use
It means that the port is already in use either by another process or another mongo instance.
You should use another port for this instance.
Refer to this link

Related

Error: couldn't connect to server 127.0.0.1:27017 even though I am running mongod

MongoDB shell version v4.2.3
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
2020-05-02T19:00:36.477-0500 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:341:17
#(connect):2:6
2020-05-02T19:00:36.483-0500 F - [main] exception: connect failed
2020-05-02T19:00:36.483-0500 E - [main] exiting with code 1
That is the error that I am getting after running Mongo and this is the response that I get after running mongod
2020-05-02T19:00:34.303-0500 I CONTROL [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
2020-05-02T19:00:34.309-0500 I CONTROL [initandlisten] MongoDB starting : pid=3964 port=27017 dbpath=/data/db 64-bit host=Mateos-MBP.attlocal.net
2020-05-02T19:00:34.309-0500 I CONTROL [initandlisten] db version v4.2.3
2020-05-02T19:00:34.309-0500 I CONTROL [initandlisten] git version: 6874650b362138df74be53d366bbefc321ea32d4
2020-05-02T19:00:34.309-0500 I CONTROL [initandlisten] allocator: system
2020-05-02T19:00:34.309-0500 I CONTROL [initandlisten] modules: none
2020-05-02T19:00:34.309-0500 I CONTROL [initandlisten] build environment:
2020-05-02T19:00:34.309-0500 I CONTROL [initandlisten] distarch: x86_64
2020-05-02T19:00:34.309-0500 I CONTROL [initandlisten] target_arch: x86_64
2020-05-02T19:00:34.309-0500 I CONTROL [initandlisten] options: {}
2020-05-02T19:00:34.310-0500 I STORAGE [initandlisten] exception in initAndListen: NonExistentPath: Data directory /data/db not found., terminating
2020-05-02T19:00:34.310-0500 I NETWORK [initandlisten] shutdown: going to close listening sockets...
2020-05-02T19:00:34.313-0500 I - [initandlisten] Stopping further Flow Control ticket acquisitions.
2020-05-02T19:00:34.313-0500 I CONTROL [initandlisten] now exiting
2020-05-02T19:00:34.313-0500 I CONTROL [initandlisten] shutting down with code:100
I have looked at multiple other questions and still can't seem to figure it out any help would be appreciated.
Create a directory /data/db and give permission to the MongoDB user so that MongoDB can access it. To create the directory:
sudo mkdir -p /data/db
To change owner:
sudo chown -R $USER /data/db
I followed this simple steps
Open your task manager if on windows
go to services -> go to MongoDB on services list provided
left click to highlight then down below click
->open services
on the list provided find MongoDB server
left click to highlight
click on the green play button to start the mongoDbServer
wait for a while
run your command again it will work
Open your task manager>services>MongoDB>"right-click and start the server"
It's obvious, that the MongoDB startup failed.
2020-05-02T19:00:34.310-0500 I STORAGE [initandlisten] exception in initAndListen: NonExistentPath: Data directory /data/db not found., terminating
That's why you can't connect it. Because it's never startup. You can try to fix the MongoDB startup issue, then try to log in through CMD to make sure it's ok.

mongo local server uninstallition

I'm try to uninstall mongod but it's give
message of file / package missing
during installation it's show message
of no newly file install ...............
dz-gg-04#dz-gg-04:~$ sudo apt-get install -y mongodb
Reading package lists... Done
Building dependency tree
Reading state information... Done
mongodb is already the newest version (1:3.6.3-0ubuntu1).
0 upgraded, 0 newly installed, 0 to remove and 26 not upgraded.
dz-gg-04#dz-gg-04:~$ mongod
2018-10-19T15:25:00.599+0530 I CONTROL [initandlisten] MongoDB starting : pid=4627 port=27017 dbpath=/data/db 64-bit host=dz-gg-04
2018-10-19T15:25:00.599+0530 I CONTROL [initandlisten] db version v3.6.3
2018-10-19T15:25:00.599+0530 I CONTROL [initandlisten] git version: 9586e557d54ef70f9ca4b43c26892cd55257e1a5
2018-10-19T15:25:00.599+0530 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.1.0g 2 Nov 2017
2018-10-19T15:25:00.599+0530 I CONTROL [initandlisten] allocator: tcmalloc
2018-10-19T15:25:00.599+0530 I CONTROL [initandlisten] modules: none
2018-10-19T15:25:00.599+0530 I CONTROL [initandlisten] build environment:
2018-10-19T15:25:00.599+0530 I CONTROL [initandlisten] distarch: x86_64
2018-10-19T15:25:00.599+0530 I CONTROL [initandlisten] target_arch: x86_64
2018-10-19T15:25:00.599+0530 I CONTROL [initandlisten] options: {}
2018-10-19T15:25:00.600+0530 I STORAGE [initandlisten] exception in initAndListen: NonExistentPath: Data directory /data/db not found., terminating
2018-10-19T15:25:00.600+0530 I CONTROL [initandlisten] now exiting
2018-10-19T15:25:00.600+0530 I CONTROL [initandlisten] shutting down with code:100
dz-gg-04#dz-gg-04:~$ mongo
MongoDB shell version v3.6.3
connecting to: mongodb://127.0.0.1:27017
2018-10-19T15:26:04.016+0530 W NETWORK [thread1] Failed to connect to 127.0.0.1:27017, in(checking socket for error after poll), reason: Connection refused
2018-10-19T15:26:04.016+0530 E QUERY [thread1] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed :
connect#src/mongo/shell/mongo.js:251:13
#(connect):1:6
exception: connect failed
dz-gg-04#dz-gg-04:~$ sudo service mongod start
Failed to start mongod.service: Unit mongod.service not found.
dz-gg-04#dz-gg-04:~$ ^C
dz-gg-04#dz-gg-04:~$ ^C
dz-gg-04#dz-gg-04:~$ ^C
dz-gg-04#dz-gg-04:~$ ^C
dz-gg-04#dz-gg-04:~$ ^C
dz-gg-04#dz-gg-04:~$ sudo service mongod start
exception in initAndListen: NonExistentPath: Data directory /data/db not found., terminating
Create the directory /data/db

Why can't I start mongodb server?

I try to start mongodb server, but it exists immediatly. Is it because it tried to use a port already in use? How shall I run it correctly? Thanks.
I am on Ubuntu 14.04, and installed mongodb following the guideline in mongodb website.
$ mongod
2016-08-04T11:06:39.947-0400 I CONTROL [initandlisten] MongoDB starting : pid=15947 port=27017 dbpath=/data/db 64-bit host=ocean
2016-08-04T11:06:39.947-0400 I CONTROL [initandlisten] db version v3.2.8
2016-08-04T11:06:39.951-0400 I CONTROL [initandlisten] git version: ed70e33130c977bda0024c125b56d159573dbaf0
2016-08-04T11:06:39.952-0400 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.1f 6 Jan 2014
2016-08-04T11:06:39.952-0400 I CONTROL [initandlisten] allocator: tcmalloc
2016-08-04T11:06:39.952-0400 I CONTROL [initandlisten] modules: none
2016-08-04T11:06:39.952-0400 I CONTROL [initandlisten] build environment:
2016-08-04T11:06:39.952-0400 I CONTROL [initandlisten] distmod: ubuntu1404
2016-08-04T11:06:39.952-0400 I CONTROL [initandlisten] distarch: x86_64
2016-08-04T11:06:39.953-0400 I CONTROL [initandlisten] target_arch: x86_64
2016-08-04T11:06:39.953-0400 I CONTROL [initandlisten] options: {}
2016-08-04T11:06:40.015-0400 E NETWORK [initandlisten] listen(): bind() failed errno:98 Address already in use for socket: 0.0.0.0:27017
2016-08-04T11:06:40.016-0400 E NETWORK [initandlisten] addr already in use
2016-08-04T11:06:40.016-0400 E STORAGE [initandlisten] Failed to set up sockets during startup.
2016-08-04T11:06:40.016-0400 I CONTROL [initandlisten] dbexit: rc: 48
It tells you that Address already in use for socket: 0.0.0.0:27017.
Try on another port with mongod --port 27018.
To kill process that uses port 27017 on ubuntu you can use lsof -i :27017 to find PID of that process and then kill -9 <PID>.
The error is shown pretty clearly here:
listen(): bind() failed errno:98 Address already in use for socket: 0.0.0.0:27017
This is saying that the socket is already bound by something else - that is, there is another service already running on port 27017; most likely, an instance of your mongo server already ( only one process can bind to a port at a time )
Use this
ps aux | grep mongod
To find any mongod processes that are already running. Use kill <pid> to end the process.
If you want to start a second instance of mongo, then you need to have it bind to a new port, you can do this using
mongod --port 27015
Note, that if you start a second instance, you will also need to provide a second data directory - you should not have two running instances using the same database files.

MongoDB Issues on Yosemite

I typed in two commands, mongo and mongod and get the following results. Not sure how to fix this
mahtabalam$ mongo
MongoDB shell version: 3.0.6
connecting to: test
2015-09-19T18:59:35.315-0400 W NETWORK Failed to connect to 127.0.0.1:27017, reason: errno:61 Connection refused
2015-09-19T18:59:35.316-0400 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
mahtabalam$ mongod
2015-09-19T19:00:47.133-0400 E NETWORK [initandlisten] Failed to unlink socket file /tmp/mongodb-27017.sock errno:13 Permission denied
2015-09-19T19:00:47.134-0400 I - [initandlisten] Fatal Assertion 28578
2015-09-19T19:00:47.134-0400 I - [initandlisten]
***aborting after fassert() failure
Update 1:
After trying sudo mongod --dbpath="Users/mahtabalam/Sites/mongo/data/db and then pressing ctrl + c on my macbook pro, and then re-entering mongod has yielded the following:
2015-09-19T21:54:49.819-0400 I JOURNAL [initandlisten] journal dir=/data/db/journal
2015-09-19T21:54:49.820-0400 I JOURNAL [initandlisten] recover : no journal files present, no recovery needed
2015-09-19T21:54:49.836-0400 I JOURNAL [durability] Durability thread started
2015-09-19T21:54:49.836-0400 I CONTROL [initandlisten] MongoDB starting : pid=28979 port=27017 dbpath=/data/db 64-bit host=Mahtabs-MacBook-Pro.local
2015-09-19T21:54:49.836-0400 I JOURNAL [journal writer] Journal writer thread started
2015-09-19T21:54:49.836-0400 I CONTROL [initandlisten]
2015-09-19T21:54:49.836-0400 I CONTROL [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000
2015-09-19T21:54:49.837-0400 I CONTROL [initandlisten] db version v3.0.6
2015-09-19T21:54:49.837-0400 I CONTROL [initandlisten] git version: nogitversion
2015-09-19T21:54:49.837-0400 I CONTROL [initandlisten] build info: Darwin yosemitevm.local 14.5.0 Darwin Kernel Version 14.5.0: Wed Jul 29 02:26:53 PDT 2015; root:xnu-2782.40.9~1/RELEASE_X86_64 x86_64 BOOST_LIB_VERSION=1_49
2015-09-19T21:54:49.837-0400 I CONTROL [initandlisten] allocator: system
2015-09-19T21:54:49.837-0400 I CONTROL [initandlisten] options: {}
2015-09-19T21:54:49.850-0400 I NETWORK [initandlisten] waiting for connections on port 27017
mongod command starts the DB server & it expects an argument dbpath so the full command would be sudo mongod --dbath="dblocation/data/db" where /data/db is mandatory to start the server.
Whereas mongo opens a mongo db shell where you can execute your db statements

Mongodb wont start: addr already in use

when I try to start my database, mongodb is logging the following messages:
2014-08-12T08:42:17.522-0300 [initandlisten] journal dir=/www/mongodb/data/journal
2014-08-12T08:42:17.523-0300 [initandlisten] recover : no journal files present, no recovery needed
2014-08-12T08:42:17.564-0300 [initandlisten] ERROR: listen(): bind() failed errno:98 Address already in use for socket: 0.0.0.0:27017
2014-08-12T08:42:17.564-0300 [initandlisten] ERROR: addr already in use
2014-08-12T08:42:17.567-0300 [initandlisten] now exiting
2014-08-12T08:42:17.567-0300 [initandlisten] dbexit:
2014-08-12T08:42:17.567-0300 [initandlisten] shutdown: going to close listening sockets...
And it wont start.
But then I run the next command nothing is returned:
sudo netstat -ltnp | grep 27017
The server was manually shutdown (db.shutdownServer()) and it is a part of a replica set.
I noticed that the mongod.lock is created in the process, but removing it doesnt solve the problem.
So i guess the error message isnt quite right.
I'm using MongoDB shell version: 2.6.1
Anyone knows what could be the problem?
I had same issue.
In my config file in bindIp I had 2 ports specified, and for some reason mongo tried to run both of them simultaneously.
Leave only one and make sure it is in etc/hosts.