MongoDB only works when run as root on Ubuntu - data directory issue
You created /data/db
as root so it has those permissions. You can change the permissions to your user account, or whatever you have mongo running as.
chown -R username /data/db
or /data
You can also set a group
chown -R username.groupname
The -R
does it recursively, so it will affect all the files you've created running mongoDB as root already.
I just had the same issue on RHEL 8, in year 2021.
I could run sudo mongod
with sudo privileges but could not start it as a service with: sudo systemctl start mongod
.
I tried many solutions and tested after each attempt, what ultimately solved it for me is changing ownership of /data/db/
, /var/lib/mongo/
, and /var/log/mongodb/
to mongod
.
exact commands:
sudo chown -R mongod:mongod /var/lib/mongo/sudo chown -R mongod:mongod /var/log/mongodb/sudo chown -R mongod:mongod /data/