Changing the Default DB

I’ve updated the dbpath in my config file, as well as added the user to the admin db. However, once I am finished doing so, and run the validation script, I get the Mongod was not started by the correct user - remember NOT to run mongod as root.

Is there a way to “sign-in” and run the mongod without using a root user? I had to use sudo mongod --config config.txt because I got an "IllegalOperation: Attempted to create a lock file on a read-only directory: /var/mongodb/db" without the sudo before my command.

I’m also unable to shut down the server to restart as it states “there are no users authenticated”, code 13, unauthorized.

By starting with sudo you explicitly ask to run as root. By default you will start mongod as the current user. But you must ensure that the current user has read permission to all the directories and files.

options: { config: "config.conf.txt", net: { bindIp: "localhost,192.168.103.100", port: 27000 }, security: { authorization: "enabled" }, storage: { dbPath: "/var/mongodb/db" } }
2019-09-18T16:12:24.572+0000 I STORAGE [initandlisten] exception in initAndListen: IllegalOperation: Attempted to create a lock file on a read-only directory: /var/mongodb/db, terminating
2019-09-18T16:12:24.572+0000 I CONTROL [initandlisten] now exiting
2019-09-18T16:12:24.572+0000 I CONTROL [initandlisten] shutting down with code:100

I tried chown’ing the /var folder to allow for read/write, but that didn’t work.

vagrant@m103:~$ chown vagrant:vagrant /var/mongodb/db
chown: changing ownership of '/var/mongodb/db': Operation not permitted
vagrant@m103:~$ chown vagrant:vagrant /var
chown: changing ownership of '/var': Operation not permitted

Any ideas?

Since you started once with sudo, the files and directories are owned by root. To change the owner back to vagrant, you use sudo to do chown. That way you chown will run as root and only root is allowed to change ownership of the file it owns.

3 Likes

Thanks Steeve! Your ideas totally helped. Cheers!

Alec

HI, i have change the permission at /var/mongodb/db, but the error is the same
I STORAGE [initandlisten] exception in initAndListen: IllegalOperation: Attempted to create a lock file on a read-only directory: /var/mongodb/db, terminating

drwxrwxrwx 15 vagrant vagrant 4096 Sep 18 17:17 var
drwxrwxrwx 3 vagrant vagrant 4096 Sep 18 17:17 mongodb
drwxrwxrwx 4 vagrant vagrant 4096 Sep 18 17:43 db

Where is the error?

Thx

The output of your ls command looks funny, as if all the directories were at the same level under the current directory.

Can you do?

ls -ld /var
ls -ld /var/mongodb
ls -ld /var/mongodb/db

1 Like

I have paste only the output of the three command
The directory tree is correct: /var/mongodb/db

Hi @Marco_Crisma_74791,

There can be a possibility that the issue is not with the directory structure or permissions.

Can you please check the lock file once? The lock file seems to be in contention and you might need to clear the file and re-launch your process to get it going.
Also, since you changed the directory permissions later, there is also a chance that the file is owned by root. So you might need to remove it using sudo.

Please let me know if the issue still persists.

Thanks,
Muskan
Curriculum Support Engineer

Thx Muskan
I have resolved. I have cange the ownership to root at var directory with command chown -R and replace the ownership only at the db directory

Now it’s ok

Thx

1 Like