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:
--replSet
optionmongod --port 27017 --dbpath /var/lib/mongodb --replSet rs0 --bind_ip localhost
rs.initiate()
inside the ShellI'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?
When running the mongod as a service, use /etc/monogd.conf
to set the configuration. Note that the location or name of this file might have been changed in the mongod.service file in your system.
See replication options for how to set that in the file.