0

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.

2 Answers2

1
  1. You should install mongodb as windows service. Read the guide from the official documentationenter image description here
  2. 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.
mtkachenko
  • 5,389
  • 9
  • 38
  • 68
  • I did both, I know how to make it to start as replica set problem is now that the config file stay the same. replication is marked with # even when i start it with --replset and even when the replica set is already running and it has 3 instances in it. i followed this https://stackoverflow.com/questions/2438055/how-can-i-run-mongodb-as-a-windows-service in order to start the mongod as repl set and service but again the config file stay as default. – python maniac Feb 08 '21 at 11:09
  • @pythonmaniac Config file and command line arguments are two different ways to run mongodb. If you want to provide all parameters manually - use command line arguments, if you want to use config file - you should do something like `mongod --config /etc/mongod.conf`. **Configuration file won't be modified automatically.** Uncomment and set replication section in config file and run mongod process with `config` parameter, if you want to do it through config file. But I see you already did all staff through command line. So you don't need config file at all. – mtkachenko Feb 08 '21 at 11:14
  • Thanks for the help, didn't know that once I run all through cmd it won't take effect on the cfg file. in case I do want to modify the cfg file what the minimal things i need to do on the replication section? just replSet: replication_name? or I also need to mention the members here? and if i do modify it on the cfg file and also run all through the cmd (like i did) will it still work? (its like the cfg file is modifed but i don't use it but will it work and won't cause errors? – python maniac Feb 08 '21 at 11:44
1

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>

Wernfried Domscheit
  • 54,457
  • 9
  • 76
  • 110