how to run mongod as daemon with replica configurations? - mongodb

I use to run the mongodb service with the command ( in CentOS)
sudo mongod --config /etc/mongod.conf
in mongod.conf file replica set information are there.
Now when my system is rebooted, I want automatically this service to run.
How can I do this ?

The /etc/rc.d/rc.local script is executed by the init command at boot time or when changing runlevels. Any command that you want to run after reboot can be added in this file. Just add the command at the end of the file /etc/rc.d/rc.local
You should also check the section Using a Basic Init Script in the following link: https://www.linode.com/docs/databases/mongodb/use-mongodb-to-store-application-data-on-centos-5/

Related

MongoDB replication as a service on windows

I am trying to configure a replica set on windows as a service, means that even the PC will restart the mongod will run again automatically.
problem is that I run the mongod like this:
mongod --dbpath "C:\Program Files\MongoDB\Server\4.2\data" --logpath "C:\Program Files\MongoDB\Server\4.2\log\mongod.log" --port 27017 --storageEngine=wiredTiger --journal --replSet test_replica
And once I close the CMD running this command the service is killed. How do I run it correctly then?
Also, currenctly the service is navigating to the default cfg file but I see the replication there is marked with # (so the service is running as standalone). and when I try to add to replication the replSet: test_replica it won't start anymore.
You should put all your parameters into a configuration file. Then you can create the service like this:
mongod.exe --config c:\MongoDB\config\mongod.cfg --install
For a replica set you typically create several services, not just one. It is possible to run a replica set with just one member, however this is quite useless. Of course, when you create several services then each one needs his own config file (and also his own dbPath, port, etc.)
The next time your PC will boot, the mongo service should also start. Or start it manually with command net start <mongo service name>
You should install mongodb as windows service. Read the guide from the official documentation
Setting --replSet from command line or replication:replSetName in configuration file is not enough. Read this guide, in short: after mongodb process is started in replica set mode, you should run rs.initiate() in mongo shell.

Need to launch mongod using launchctl

I have installed MongoDB using manual option.
When I fire mongod it will take parameters from somewhere else specially for dbPath.
I want to have my own dbPath without specifying it with --dbPath each time.
So I found option to provide mongod.conf file and call mongod --config option but still I need to pass path of .conf file.
My ultimate goal is to just launch mongod as service and it will read config file from path I configured. For this I tried LaunchAgent and LaunchDaemon with no luck. What I thought is that if I can configure LaunchAgent or Daemon then I will provide path to mongod of bin directory of MongoDB folder and provide --config and path for .conf file as argument and then I need to just launch mongod using launchctl with name mongod and it will start mongod --config <.conf file path>.
But I am not able to do so. I read nearly 15-16 posts on LaunchAgent and Daemon but did not get solution my problem. I tried to do so with making mongod.plist file at various places like /Library/LaunchDaemon, /Library/LaunchAgent and ~/Library/LaunchDaemon but I always get error "Path had bad ownership/permissions". I tried chown and chgrp but no luck.
I hope you got my point. I really need complete solution to achieve my goal.
Do you use linux or windows?
This is the default location of mongodb.conf in linux:
/etc/mongodb.conf
You can simply edit it with your parameters, and start the service using:
sudo service mongodb restart
If you are using windows, it depends on the version of MongoDB you are using, but check in:
C:\Program Files\MongoDB\Server\3.4\...
For the same file.

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

Start mongod service at system startup

I recently updated mongodb, and I run Linux Mint (an Ubuntu based system) and every time I start it up, i cannot use Robomongo because the service mongod is not automatically started; every time I need to open up a terminal and use
sudo service mongod start
Is there a way to start mongod automatically at system start?
Use the following command to autostart mongodb when system start.
systemctl enable mongod.service
You can either put the command in your /etc/bashrc script under and if condition i.e. if the mongod process is not already running, then start it.
Other way is to modify your /etc/rc.local and add the command to start mongod in that file. It will start at bootup.
Using crontab on Ubuntu 20 you can try this.
crontab -e
And input this as a crontab entry
#reboot service mongod start

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