If you’re like me and you’re building non-trivial software and you use MongoDB as a datastore you’d know that it can be a pain in the ass to work around the fact that MongoDB doesn’t support transactions when running it locally, so you might opt to use a shared instance on MongoDB Altas or look for a docker compose file that spins up a replica set, but that’s not ideal because it’s an extra thing you have to setup.
Setup
You can run MongoDB as a single-node replica-set on your local machine. Depending on how you installed MongoDB this setup might be different. I’m currently on Ubuntu 20.04.4 LTS, and I’m using the apt package manager. Installing mongodb using apt will create a daemon that runs mongodb in the background.
The Solution
According to MongoDB’s documentation, a mongod standalone instance can be converted to a replica set by passing a cli flag with the replica set name.
The permission settings on /var/lib/mongodb and /tmp/mongodb-27017.lock are wrong, so you will have to change the owner to monogdb. You can read more about it here and here.
There’s a known issue with the rs cli tool where mongod would fail to start, the fix is delete the data folder for that distribution. Haha, I know it’s not elegant but the tool has served it’s purpose.
if you want you can use rs to download and run different mongodb distributions if that’s something you want to do.