Running mongos as background process - mongodb

How can I run mongos as background process in an EC2 machine?
I am trying to set up a sharded cluster on EC2 machines and I am able to run mongod as background service, but I am not able to run mongos as background service.

mongod --fork --logpath /var/log/mongod.log

To run a mongod process as a daemon (i.e. fork), and write its output to a log file, use the --fork and --logpath options. You must create the log directory; however, mongod will create the log file if it does not exist.
The following command starts mongod as a daemon and records log output to /var/log/mongodb.log.
mongod --fork --logpath /var/log/mongodb.log

You need to start mongos with --fork parameter. It
Enables a daemon mode that runs the mongos process in the background. By default mongos does not run as a daemon: typically you will run mongos as a daemon, either by using --fork or by using a controlling process that handles the daemonization process (e.g. as with upstart and systemd).
You can also try & with the command to run it as background. for ex- mongod &
If you have ran or want to make current process to background the press Ctrl+z and type bg.

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.

How to set mongo db path permanently

Is there a way to over-rite the mongodb default db path. Even after editing the storage path in mongod.conf to the custom directory path. Still it looks for /data/db, and not the custom path.
Since every time mongod path needs to be specified for the custom path.
mongod --dbpath /Users/customData
Is there a permanent way to deal this.
You can try to run it as a service, so that you don't need to run this command everytime you want to use it, and it runs it in the path you set it to
Here's how:
the link
from official mongodb website: the link
According to documentation of MongoDB
To run a mongod process as a daemon (i.e. fork), and write its output
to a log file, use the --fork and --logpath options. You must create
the log directory; however, mongod will create the log file if it does
not exist.
The following command starts mongod as a daemon and records log output
to /var/log/mongodb.log.
mongod --fork --logpath /var/log/mongodb.log

Mongodb How can I stop shutdown one of instances

I'm a newbie to Mongodb. I installed Mongodb 3.4 and setup two config files each config has different port and share a same ip address. How can I shut down instance in mongoA.conf file. Try to use command "sudo service mongod stop" But it doesn't know which config file to stop the service. Thanks in advance
start two instances Mongodb config files:
mongod --config /etc/mongoA.conf
mongod --config /etc/mongoB.conf
You can find PID of mongod port. Then kill process on that PID.
To find all PID mongod, type:
# ps -aux | grep mongod
You will see on console:
root 28035 0.9 18.5 5319648 2992608 ? Sl Apr06 1493:14 mongod --config /etc/mongoA.conf
Then kill process on this PID:
# sudo kill -9 NUMBER_PID
On above example, NUMBER_PID is 28035.
On OSX ps -alx will give the same list of processes.
If you allow kill to send the default TERM signal by not using -9 explicitly it will allow the database to catch the TERM signal and shutdown cleanly.

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

mongodb running only while ssh connection is kept

Hello I'm testing mongo experimental version, which doesn't have an installer and is ran from directory by putting ./mongod but the problem is that the process quits whenever I close my ssh connection. What I'm supposed to do to keep it running even after closing ssh connection?
To start the mongod process as a background daemon you should run it with the parameters --fork (run as a daemon) and --logpath (a filename for log output).
You probably want to specify the --dbpath as well (unless you are OK with the default of /data/db).
So putting that together:
$ mongod --fork --dbpath /path/to/data --logpath /path/to/mongod.log
about to fork child process, waiting until server is ready for connections.
forked process: 64043
all output going to: /path/to/mongod.log
child process started successfully, parent exiting
$
After the server starts you will be returned to a command prompt.
If you want to shut down the server gracefully, log in via the mongo shell and run db.shutdownServer().
For more information see Manage mongod Processes in the MongoDB Manual.
It looks like that it is not started as a daemon so when you close your ssh session it got HUP signal. You can avoid it by using:
nohup command-to-start-mongo &