简体   繁体   中英

MongoDB only works when run as root on Ubuntu - data directory issue

I installed MongoDB with the official packages (mongodb-stable), and followed the Quickstart guide which includes:

By default MongoDB will store data in /data/db , but it won't automatically create that directory. To create it, do:

 $ sudo mkdir -p /data/db/ $ sudo chown `id -u` /data/db 

You can also tell MongoDB to use a different data directory, with the --dbpath option.

MongoDB will only start if I run sudo mongod - if I try and run just mongod I get the error:

Mon Mar 14 15:27:07 [initandlisten] couldn't open /data/db/test.ns errno:13 Permission denied
Mon Mar 14 15:27:07 [initandlisten]   couldn't open file /data/db/test.ns terminating
Mon Mar 14 15:27:07 dbexit:

What gives?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM