Start Mongo with a flag - mongodb

Is it possible to start mongod (using systemctl) with --master flag?
I want to use the oplog file to watch for data changes. I know this should be used with replicas set, but on my development machine I don't need any replicas set.

Yes, there is an option to enable oplog on standalone mongod. All you need to do is start mongod with --master option. Or if you have a config file add the entry master=true.
This option will ensure that oplog will be created, but unlike a replica set there will not be any secondary instances of mongod which will read it and apply it to their local databases.

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.

MongoDB replica set initialisation

I am trying to create a replica set with two replicas and one arbiter. All members are launched using:
mongod --port 25670 --dbpath /data --replSet rs0
The data directory is an empty directory.
On one of the replicas I run rs.initiate but I get the error
replSetReconfig should only be run on PRIMARY, but my state is STARTUP2
I have tried waiting for a PRIMARY to be elected but even after two minutes, I see no indication that an election has taken place.
How do I force the replica set to elect a PRIMARY? Or is there a way to predict the PRIMARY?
The only solution that I found to work consistently is as follows:
Shutdown all servers.
Launch the first replica server.
rs.initiate the replica set with only the first server.
Launch the second replica.
Add it to the replica set.
Finally launch and add the arbiter. For the arbiter it did not matter if I did addArb even before launching the arbiter instance.

Start mongod with overrided configuration for replSet

I have few mongoDB instances running as replication set. For each of them, I have prepared configuration file with replSetName, so starting mongo by mongod without -replSet is still adding it to replication set. But, to maintenance instance, I need to run it with replSet disabled - is it possible to override replSetName from configuration by some mongod parameter (like mongod -port, which overrides port: from conf file)
Simple answer is no...
When I need to start node to maintenance mode, I have "ready made string" on my "notepad++" what includes all needed parameters as command line parameters (of course different port number) to bring node up without --replSet..

MongoDB error . rs.initate(config); Was displaying error

I am new to mongodb and am looking into the concept of replication .
Can u please help if u are aware of this error.
The error I got
The config file I am using
Thanks in advance,
Tarun
Its because you're passing in config to the rs.initiate function.
Before you run the initiate you need to start the different mongod instances which will make up the replicaset. Then run rs.initiate() on one of the nodes (without and args). You can then do rs.add(<host:port>) to add the nodes making up the rs.
There are some excellent tutorials available for setting up replicasets. This one is a good starting point:
https://docs.mongodb.com/manual/tutorial/deploy-replica-set/
It provides step-by-step instructions for starting the necessary mongod instances, and linking them as a replicaset in the mongoshell
you need to start all mongod instances with following command which include replica set name, other way is to create entries in config file and pass it to mongod process :
mongod --replSet "Tarun"
then you can initiate : rs.initiate() with your config file

oplog enable on standalone mongod not for replicaset

I am running mongod as standalone server .now i want to enable oplog for it ,i want to know whether it is possible or not ?
I knew I can do this by creating single node ReplicaSet ,but I want to do this without replica set .
I got a reply from the MongoDb Team, and it's working fine.
To enable oplog on a standalone server, start mongo with master options.
On command line start mongod --master.
For conf file append the following line:
master=true
More details here.
For mongo 2.6 or greater the /etc/mongod.conf now uses a yaml format.
To enable the oplog add something similar to the below, note the replication: line may already exist, be sure to use spaces.
replication:
replSetName: rs0
oplogSizeMB: 100
Full documentation here:
https://docs.mongodb.com/manual/reference/configuration-options/#replication-options