Run mongodb as a replica set member by default when Window starts

Hi, I am a Windows user and I am using transactions with mongodb and I have to stop the mongodb service by:
net stop mongodb
and run as a replica set member like this:
mongod --replSet rs0
mongod is an alias for my file path to mongod.exe

I was just wondering whether there is a more efficient way to start mongodb service as a replica set member by default without being required to stop and run again because I use transactions quite offenly.

I tried to edit the mongod.cfg by adding the following lines which I found in the docs:

replication:
  replSetName: rs0

but that did not work at all. When I edit this, I have to restart my PC because mongod also stops working after I edit this, and when I restart my PC, it gets fixed by itself.

Really stuck!!

You will need to share the logs so that we can help. There is not enough information in your current post.

1 Like

Basically there is no issue in my current flow but I just want to make things more efficient. All I want to do is basically want to start the mongodb server as a replica set always so that I don’t have to net stop mongodb and mongod --replSet rs0 every time I turn my machine on!

@Tayyab_Ferozi

You want to enable mongodb to run as a service when you install. Then the mongod.cfg in the install path will be used. If you did not do this you may be able to create your own with sc.exe.

Starting in version 4.0, you can install and configure MongoDB as a Windows Service during the install, and the MongoDB service is started upon successful installation. MongoDB is configured using the configuration file <install directory>\bin\mongod.cfg .

1 Like

Yes I enabled that option but still having issues. This is my mongodb.cfg:

# mongod.conf


# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/

# Where and how to store data.
storage:
  dbPath: C:\Program Files\MongoDB\Server\4.4\data
  journal:
    enabled: true
#  engine:
#  mmapv1:
#  wiredTiger:

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path:  C:\Program Files\MongoDB\Server\4.4\log\mongod.log

# network interfaces
net:
  port: 27017
  bindIp: 127.0.0.1


#processManagement:

#security:

#operationProfiling:

#replication:

replication:
  replSetName: rs0

#sharding:

## Enterprise-Only Options:

#auditLog:

#snmp:

Please can you help me?? It doesn’t start correctly

What are the issues? We cannot help without more details.

1 Like

Sure. But if your mongod is not running as a service and/or referencing this configuration then no amount of changing this file is going to make a difference.

Get the configuration of the service with sc.exe qc MongoDB :

PS C:\Users\admin\Downloads> sc.exe qc MongoDB
[SC] QueryServiceConfig SUCCESS

SERVICE_NAME: MongoDB
        TYPE               : 10  WIN32_OWN_PROCESS
        START_TYPE         : 2   AUTO_START
        ERROR_CONTROL      : 1   NORMAL
        BINARY_PATH_NAME   : "C:\Program Files\MongoDB\Server\4.4\bin\mongod.exe" --config "C:\Program Files\MongoDB\Server\4.4\bin\mongod.cfg" --service
        LOAD_ORDER_GROUP   :
        TAG                : 0
        DISPLAY_NAME       : MongoDB Server (MongoDB)
        DEPENDENCIES       :
        SERVICE_START_NAME : NT AUTHORITY\NetworkService

This setting will not be conducive to a working replicaset as other members will not be able to connect.

1 Like