Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 358
YXD Avatar asked Mar 14 '11 15:03

YXD


People also ask

Where is mongodb data stored in Ubuntu?

By default, mongod sets the database location to /data/db/. For checking by yourself you should run ps -xa | grep mongod and if you don't see a --dbpath which explicitly tells mongod to look at that parameter for the db location and you don't have a dbpath in your mongodb.

Where does mongodb place its default database directory for data files?

The default location for the MongoDB data directory is c:\data\db. So you need to create this folder using the Command Prompt. Execute the following command sequence. Then you need to specify set the dbpath to the created directory in mongod.exe.

How do you change the default Datafile directory of mongodb?

Make sure that mongodb has privileges to read / write from that directory ( usually chown mongodb:mongodb -R /mnt/database/mongodb ) - thanks @DanailGabenski. Copy the data folder of your mongodb to the new location - cp -R /var/lib/mongodb/ /mnt/database/ Remove the old database folder - rm -rf /var/lib/mongodb/

Where does mongodb store data Linux?

The data path will either be the default of /data/db (if no config file is being used) or discoverable via db.


1 Answers

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.

like image 66
Joe Bowman Avatar answered Oct 13 '22 22:10

Joe Bowman