0

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?

SnekNOTSnake
  • 1,157
  • 12
  • 24
  • Did you try changing the permissions for `/var/lib/mongodb/` as instructed in the solutions to [this](https://stackoverflow.com/questions/42446931/mongodb-exception-in-initandlisten-20-attempted-to-create-a-lock-file-on-a-rea) question? – Spack Jarrow Aug 21 '20 at 20:08
  • I did. But it still doesn't work, the error is as same as before. – SnekNOTSnake Aug 21 '20 at 20:22

1 Answers1

2

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.

Joe
  • 25,000
  • 3
  • 22
  • 44
  • I tried `mongod -f /etc/mongod.conf --port 27017 --dbpath /var/lib/mongodb/ --replSet rs0 --bind_ip localhost`, now it doesn't give me any output, and then tried to connect to it using `mongo "mongodb://127.0.0.1:27017/?replicaSet=rs0"` but got another error. – SnekNOTSnake Aug 21 '20 at 20:42
  • just edit mongod.conf to add the replica set name, then start with systemctl. – Joe Aug 21 '20 at 22:13
  • It looks like what I was dealing with is indeed a privilege issue. After some trial and thoughts, my conclusion was this: When I am running the `sudo mongod ...` command, it creates/overwrites files with a root ownership. I inspected the `/lib/systemd/system/mongod.service` file, it seems that it runs the process as *mongodb* instead of *root*, therefore, when the process tries to manipulate a file owned by root, it produces an error. – SnekNOTSnake Aug 21 '20 at 23:37
  • Also, thanks to your answer, I just edited my `/etc/mongo.conf` file with `replication: replSetName: rs0` and then tried again using the "normal way" `sudo systemctl start mongod`. I completed step 3 and 4, and ended up with `rs0:SECONDARY>` is this the so called "replica set"? Anyway, thanks for your time and answer, sir. You have my gratitude. – SnekNOTSnake Aug 21 '20 at 23:47
  • 1
    After a few moments, it should changer from `SECONDARY` to `PRIMARY`, and then you should be able to use change streams, transaction, etc. – Joe Aug 21 '20 at 23:48