Skip to content

Instantly share code, notes, and snippets.

@mramirid
Last active January 30, 2023 03:00
Show Gist options
  • Save mramirid/6d0e27ddc1d375b4d7d4046ee938bcc7 to your computer and use it in GitHub Desktop.
Save mramirid/6d0e27ddc1d375b4d7d4046ee938bcc7 to your computer and use it in GitHub Desktop.
Enabling MongoDB replication to solve this error when running transactions: "MongoServerError: Transaction numbers are only allowed on a replica set member or mongos".

Enabling MongoDB Replication

  1. Stop Mongod Service/Daemon/Process.

  2. Find & open the mongod.conf.

    • Windows: <MongoDB-Server-Bin-Path>/mongod.conf
    • Linux: /etc/mongod.conf
  3. Add the following to the File:

replication:
   oplogSizeMB: 5000 # <-- Usual Value could be: 5% of Free-Disk-Space | Min: 990 | Max: 50000 
   replSetName: local-rs
   enableMajorityReadConcern: true
  1. Start the Mongod Service/Daemon/Process.

  2. Run MongoDB Shell using mongosh.

  3. Run:

rs.initiate()
  1. Now when connecting to your mongod instance make use of the replSet parameter:
    • Local MongoDB Compass Example: mongodb://localhost:27017/?replicaSet=local-rs&readPreference=primary
    • mongosh seems to work without that option.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment