ryer.io

Running a Single-Node MongoDB Replica Set Locally

TL;DR

  • Enterprise Edition is required for client-side field-level encryption via libMongoCrypt.
  • A local replica set is what makes database transactions available — they don’t work standalone.
  • Stop the service, add ?replicaSet=rs0 to the connection string, restart with --replSet rs0.
  • Then rs.initiate() in mongosh, verify with rs.status().
  • No Docker needed for this one.

Setting up MongoDB locally to mirror production as closely as possible. The motivation is transactions — they require a replica set, so a standalone local instance can’t exercise the code paths that matter.

Why Enterprise Edition

We need client-side field-level encryption, which depends on the libMongoCrypt library. Community Edition has some encryption capability, but custom encryption via CSFLE is Enterprise-only, and that’s what our setup requires.

Setup

Stop what’s running — both the app server and MongoDB. On macOS:

1
brew services stop mongodb-enterprise

Or find and kill it directly:

1
2
ps -aux | grep mongod
sudo kill -9 [PID]

Update the connection string in your environment variable to declare the replica set:

?replicaSet=rs0

rs0 is just a name; anything works as long as it matches what you start the server with.

Restart with replication enabled:

1
mongod --replSet rs0

Adjust paths and options for your local configuration.

Initialise it

In mongosh:

1
rs.initiate();

Confirm it took:

1
rs.status();

Once that reports healthy, exit and start the application server with the updated connection string.

Result

A functioning single-node replica set locally. That’s enough to make transactions work and to exercise the same code paths production uses, which is the whole point — testing transactional logic against a standalone instance tells you nothing about whether it’ll behave when deployed.