Home:ALL Converter>Starting MongoDB Replica Set

Starting MongoDB Replica Set

Ask Time:2020-08-22T03:40:29         Author:SnekNOTSnake

Json Formatter

So, I was making an app that needs to use MongoDB transactions. But the Mongoose documentation told me that "MongoDB currently only supports transactions on replica sets, not standalone servers." So I thought I basically need to switch my Standalone MongoDB instance to Replica Set (whatever that means).

The MongoDB documentation gave me the instruction of how to do this with a few steps:

  1. Shutdown the Standalone MongoDB instance
  2. Restart the instance using the --replSet option
mongod --port 27017 --dbpath /var/lib/mongodb --replSet rs0 --bind_ip localhost
  1. Connect the Mongo Shell
  2. Call rs.initiate() inside the Shell

I'm stuck at step 2. All I know, when you want to start using MongoDB, you have to start its daemon first, using sudo systemctl start mongod, and then start using it by connecting your app. but that step told me to use mongod command to start the Mongod daemon, instead of systemctl. I tried the command but got the following error:

DBException in initAndListen, terminating","attr":{"error":"IllegalOperation: Attempted to create a lock file on a read-only directory: /var/lib/mongodb"}}

At first, I thought it was some kind of a privilege issue, so I ran it again with sudo but then it ended up destroying my entire database and prevented me from starting the MongoDB the "normal way" with giving me errors that I cannot remember.

I just reinstalled the whole MongoDB to get it back to work fine. Now I'm at the same place as yesterday, unable to convert to replica set, only now my entire database is gone. What do I do to enable it?

Author:SnekNOTSnake,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/63529146/starting-mongodb-replica-set
yy