bindIP on CentOS for MongoDB process exited with error code - mongodb

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.

Related

Why Mongo Auto Restart after Shutdown

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

How to execute 'service mongod start' using additional parameters in CentOS?

I unable to start MonogoDB service after adding users into admin db as well as my db.
I setup MonogoDB and started service using following command i.e.
service mongod start
Using command prompt, I added few users like dbOwner, clusterAdmin, readWrite, read roles base users. Along with that I also changed configuration from /etc/mongod.conf. in that file, I changed port number, IP addresses, dbPath, and security.authorization: enabled.
Then I restarted mongod service using following command.
service mongod restart
After ran this command, mongod service stopped successfully, but failed to start with only 'FAILED' message.
I tried execute following command i.e.
mongod --port 27123 --dbpath /path/to/db --auth
It is working.
Question: How to execute 'service mongod start' using additional parameters in CentOS?
MonogoDB: 3.4
OS: CentOS 7
I got solution i.e.
mongod --config /etc/mongod.conf
Referred: https://docs.mongodb.com/manual/reference/configuration-options/#use-the-configuration-file
It starts child process and also I can stop mongod service using service mongod stop command.
But I don't know whether it is correct or not.
I can't certify exactly where the script that "service" command uses on CentOS 7, but in Ubuntu 18.04 mongod service script file is in
/lib/systemd/system/mongod.service
There you can change the user who executes the process and add any parameters you want, like --auth.
Said that, if you ever executed mongod as root, some files on where you store the db data will have the owner as root, making the database fail to start as another user. The fix I found for that is to manually chown to mongodb:mongodb (or the user you want to use) all the files that are owned by root inside the database.
Hope this helps.
mongod.service file from mongodb github

Cannot start mongodb back up stop: Unknown instance:

So I am trying to restart mongodb 3.2 and enable authorization, so i edit the /etc/mongod.conf file and added
security:
authorization:enabled
then i saved the file, and typed in
sudo service mongod restart
which showed that it restarted correctly, but when I looked at the processes running, mongod is not one of them.
And now I cant restart it at all.
Also, there was already a database with information in monogodb before i enabled authorization. Im not sure if that is important to know.
I checked out the solutions in here Stop: Unknown instance mongodb (Ubuntu)
but i dont have a " fork = true" statement anywhere in /etc/mongod.conf
Okay, so strange enough, I did the following
1) went back to the /etc/mongod.conf file and commented out the authorization configuration.
2) typed in sudo service mongod start and gave me back the the job is already running.
3) checked the the running processes and mongodb was now running properly! I even accessed it through the monoShell
4) then i stopped the service sudo service mongod stop
5) I went back and add the following exact syntax
security
authorization: enabled
6) saved the file and sudo service mongod start
and waaar'yaaa know. its working.....
can someone explain ? that would be helpful

Docker container mongod error when starting via ssh

I have installed mongodb on a docker container together with openssh on ubuntu 14.04. The container is running with ssh but when I ssh into the container I get the following error when trying to start mongod.
root#430f9502ba2d:~# service mongod start
Rather than invoking init scripts through /etc/init.d, use the service(8)
utility, e.g. service mongod start
Since the script you are attempting to invoke has been converted to an
Upstart job, you may also use the start(8) utility, e.g. start mongod
Also start mongod does not affect anything.
Tried looking at this also Mongo daemon doesn't run by service mongod start without it helping.
mongod --config /your/path/to/mongod.conf doesn't seem to work also, just locks up.
The error below is standard as of course there is no mongod server running.
root#430f9502ba2d:/# mongo
MongoDB shell version: 2.6.9
connecting to: test
2015-05-07T20:49:56.213+0000 warning: Failed to connect to 127.0.0.1:27017, reason: errno:111 Connection refused
2015-05-07T20:49:56.214+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
The problem here is your approach. Docker does not have an init system like you are used to on traditional systems. What docker does is replace PID 1 with the process you specify in the CMD or ENTRYPOINT Dockerfile commands. For now, ignore ENTRYPOINT, because it replaces what your CMD is run with (normally, it's /bin/sh -c). You need to instruct docker to start your mongod service in your Dockerfile with the CMD command, like:
CMD usr/bin/mongod
And when you run your container, mongod will be your PID 1. Now, you're probably wondering at this point "But what about my SSH server?" and the answer is: Don't run an SSH server on your docker containers. There are some use cases where running an SSH server is okay, but almost all of the "normal" reasons (debug, C&C, etc) are nullified with the "best practice" for getting a shell on your container:
docker exec -it myContainer /bin/bash
This will drop you into a shell on your running container. The recommendation here for managing configuration and changes in your docker container is to use something like Ansible. However, remember that docker containers are ephemeral, and you shouldn't be restarting services and changing configuration state on them. If you need a config change, change the Dockerfile or config data, and then start a new container. Good luck! Here is a little more information on Dockerizing MongoDB, but keep in mind that the method described there alters the ENTRYPOINT in the Dockerfile, which is a little more involved and requires a better understanding of what's going on in Dockerfiles.
This is really helpful. I was trying to make old Ansible playbooks work with Docker by creating several blank containers and let Ansible do the rest.
It works through command
mongod --dbpath /var/lib/mongodb --smallfiles

How do I make it so Mongo runs automatically all the time on my Azure server?

I have two Azure virtual machines. On one I have a Mongo server, on the other I just have a service I created which listens to Twitters streaming API and filters tweets.
Neither of these two services work unless I manually activate them and keep my console window open. For example, to run Mongo I need to ssh into my virtual machine and type: mongod --config /etc/mongod.conf. This starts the Mongo server successfully, but if at anytime I close my browser the service stops.
I believe the reason this is occurring is because when I login the system is allocating me a process by which I can navigate around the system and perform commands. When I type mongod --config /etc/mongod.conf I believe I am using that process to run Mongo. I am not sure how to make Mongo run without doing this though.
How do I make it so Mongo runs automatically all the time on my Azure server?
EDIT:
I tried running Mongo as a daemon but I receive an error:
$ mongod --fork --logpath /var/log/mongodb.log
>>>about to fork child process, waiting until server is ready for connections.
>>>forked process: 63470
>>>ERROR: child process failed, exited with error number 1
This issue has nothing to do with Azure; it's all about how you install MongoDB.
If you install mongodb as a service, via apt-get (or whatever other means your version of linux requires), then it will run independent of you being logged in. You shouldn't be running an always-on service through your command shell.
Here are instructions for installing under Ubuntu. You'll see that, once you set up the prerequisite public key and list file, you then run:
sudo apt-get install -y mongodb-org
You can then start and stop the service via
sudo service mongod start
and
sudo service mongod stop
You can enable mongo to autostart on boot by typing this command in your console:
sudo systemctl enable mongod
Then test it by this command:
sudo service mongod status