Why Mongo Auto Restart after Shutdown - mongodb

I tried to shut down my mongo process on a Linux server by running either mongod.sh stop or mongo --shutdown.
From the logs, I can see it shutting down with code:0
However, after that line, it logged **** SERVER RESTARTED ****, and then started a new process logging MongoDB starting: pid=xxxxx port=xxxxx dbpath=xxxxxxxxx 64-bit host=xxxxxxxx, which is not expected.
I tried on other instances using the same command, all of them were successfully shut down with only one line of **** SERVER RESTARTED ****, and nothing was logged after that.
Is there anyone knowing what possibly happened in this case? What can I do to disable this automatic restart after stopping the mongo process gracefully?

MongoDB is installed as a service.
For stop and disable use:
systemctl stop mongod
systemctl disable mongod

Related

Error while running MongoDB (i.e. the mongod process) manually as a background process

After installing mongodb using Homebrew I started it by running this in the terminal
brew services start mongodb-community#6.0
which worked just fine after stopping it and I further
tried to run MongoDB (i.e. the mongod process) manually as a background process, by running this
mongod --config /opt/homebrew/etc/mongod.conf --fork
and I got this error
about to fork child process, waiting until server is ready for connections.
forked process: 43989
ERROR: child process failed, exited with 14
To see additional information in this output, start without the "--fork" option.```

bindIP on CentOS for MongoDB process exited with error code

I'm editing a mongod.conf file to try and add a specific IP to be able to access the database with.
From what I've read I just need to edit this one file and add a second entry to bindIp
Like so:
net:
port: 27017
bindIp: 127.0.0.1, 11.222.333.44
Then save, close and run sudo systemctl restart mongod
Only when I run the restart I run into:
Job for mongod.service failed because the control process exited with error code. See "systemctl status mongod.service" and "journalctl -xe" for details.
But when I run mongo I'm able to connect to the mongo shell locally but unable to connect remotely as the IP binding failed.
If you are able to connect locally, you are most likely connecting to an old process which means either:
process restart didn't work in that it failed to terminate the old process
your configuration is invalid, and restart process identified this and stopped
you have a mongod process running on the default port that you started manually and systemd can't stop it
Verify:
your configuration is correct
you don't have any mongod processes running
Then (re)start again.
Also, review the logs using the commands indicated.

How to shut down mongos server?

I found out that mongod has --shutdown to cleanly shut down a server, what is the corresponding command for a mongos server?
The only way i found out was to simply find the PID for the server and kill -9 it, but it seems like this is not the smartest way to do it.
Using mongodb version 3.0 btw.
Try the following steps:
Login to mongos
switch to admin database
run db.shutdownServer()

Clean shutdown of the mongod process

My mongodb run under Linux 6. I use the command db.shutdownServer() to close the database but the mongod process does not stop.
Stopping mongo directly with service mongod stop do a clean shutdown?
Thanks for your help
Proper ways to shutdown mongod is described in the documentation. They are:
Use shutdownServer()
From the mongo shell
use admin
db.shutdownServer()
Use --shutdown
From the Linux command line
mongod --shutdown
Use CTRL-C
When running the mongod instance in interactive mode, issue Control-C
Use kill
From the Linux command line
kill mongoProcessID
kill -2 mongodProcessID
So you need to figure out how /etc/init.d/mongodb stop actually stops the process on your Linux distribution. For example, on Debian it uses the wrapper which behaves similar to killall which is a proper method.
As per the documentation of mongodb, mongod --shutdown will work on linux system, but, on Mac, the --shutdown switch is not recognised. However, per the documentation, Ctrl+C will cleanly shutdown the db server. When you do a Ctrl+C on the same terminal where the db server is running, it initiates a dozen or so signalProcessingThread which indicates that the shutdown is proper and smooth. At the end, you can see that the process exits with code:0. Per the convention, Ctrl+C is awkward, but is clean, although not seemingly graceful.

What are the practical differences between mongo and mongod?

Just finished installing mongodb, however, I have not been able to make complete sense of the difference between mongo vs mongod commands. Yes, I do understand that
mongod is the primary daemon process for the MongoDB system
and that
mongo is an interactive JavaScript shell interface to MongoDB
but what does that mean practically? I presume every time I want to use mongodb, I need to run mongod first. But then why am I able to run mongo without having started mongod first? Does mongo run mongod in the background automatically? Secondly, if I run mongod it eventually ends with something like
waiting for connections on port 27017
but then I can't type anything after that. Again, I presume that mongodb has been started in the background so I can safely close the terminal. But if I close the terminal by mistake (on a mac), how can I get that back up on the terminal? Also, how can I terminate the service for it to stop listening to the port?
So as you can I see, I have a bunch of simple questions... but most are related to the practical uses of when and when not to mongo or mongod. I can't seem to find anything online that will help me explain these in the practical sense.
As with most database software, Mongo is split into a server and client. The server is the main database component which stores and manages data. Clients come in various flavours and connect to the server to insert or query data.
mongod is the server portion. You start it, it runs, end of story.
mongo is a default command line client. You start it, you connect to a server, you enter commands, you quit it.
You have to run mongod first, otherwise you have no database to interact with. Simply running mongod on a command line will make it the frontmost running application, and it does not offer any interactivity. So yes, you'll just see something like "Waiting for connections...", and nothing more. You typically don't run mongod like that on the command line. You most typically create an init.d script or launchd file or however you manage your daemons, and have the system start it automatically at system boot time.
If you want to launch mongod as a one-off thing without having it permanently running on your systems, put it in the background:
$ mongod &
The & puts it in the background and you can continue to use your command line. You can see it and kill it like this:
~ deceze$ mongod &
[1] 1065
~ deceze$ jobs
[1]+ Running mongod &
~ deceze$ kill %1
[1]+ Done mongod
Once your server is running, start mongo, connect to the server, and interact with it. If you try to run mongo without a running server, it should complain that it's not able to connect:
~ deceze$ mongo
MongoDB shell version: 3.0.2
connecting to: test
2015-08-13T09:36:13.518+0200 W NETWORK Failed to connect to 127.0.0.1:27017, reason: errno:61 Connection refused
2015-08-13T09:36:13.521+0200 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
If your mongo shell does connect to something, you might unknowingly have another instance of mongod running on your system.
With mongodyou are starting the server on your machine. As you stated correctly, mongo is your client, your user interface, if you want to. Per default it connects to your local instance of MongoDB. If you start your client without a server instance running, you would have to 'tell' it, where it should connect to (e.g. a remote instance):
http://docs.mongodb.org/manual/reference/program/mongo/