Tuesday, January 20, 2015

Changing the Data Path in MongoDB

Changing the dbPath

Changing the dbPath for MongoDB can be an exercise in frustration if permissions are not managed correctly.

A common error:
 craig@U14BASE01:~$ mongo  
 MongoDB shell version: 2.6.7  
 connecting to: test  
 2015-01-20T13:54:49.274-0800 warning: Failed to connect to 127.0.0.1:27017, reason: errno:111 Connection refused  
 2015-01-20T13:54:49.276-0800 Error: couldn't connect to server 127.0.0.1:27017 (127.0.0.1), connection attempt failed at src/mongo/shell/mongo.js:146  
 exception: connect failed  


1. Stopping the Service

The first step is to stop the MongoDB service:
sudo service mongod stop


2. Editing the Configuration

Then edit the MongoDB configuration file:
sudo gedit /etc/mongod.conf
I recommend commenting out the dbPath variable and adding a new one.


3. Setting Permissions

We have to give the user "mongodb" permission to use this folder.  To check the current permission set use:
ls -l <dir>
I would expect to see "mongodb nogroup" in the file permission set.

If these do not exist, add them using this command:
sudo chown -R mongodb /path/to/db
Likewise, make sure that the parent directories have the same permission.

For example:
sudo chown -R mongodb /path/to/



4. Starting and Connecting

Once complete, you should be able to start the service and connect to MongoDB using this command sequence:
sudo service mongod start
mongo


Application


After stepping through these instructions to add a new hard drive to an Ubuntu Virtual Box instance, I followed the steps above to configure a path on that drive for MongoDB.

My directory permissions look like:
  craig@U14BASE01:~$ ls -l -R /media/craig/  
 /media/craig/:  
 total 4  
 drwxr-xr-x 4 mongodb root 4096 Jan 20 13:54 mongo  
 /media/craig/mongo:  
 total 20  
 drwxr-xr-x 4 mongodb root 4096 Jan 20 13:54 db  
 drwx------ 2 mongodb root 16384 Jan 20 13:38 lost+found  
 /media/craig/mongo/db:  
 total 81932  
 drwxr-xr-x 2 mongodb nogroup   4096 Jan 20 13:54 journal  
 -rw------- 1 mongodb nogroup 67108864 Jan 20 13:54 local.0  
 -rw------- 1 mongodb nogroup 16777216 Jan 20 13:54 local.ns  
 -rwxr-xr-x 1 mongodb nogroup    5 Jan 20 13:54 mongod.lock  
 drwxr-xr-x 2 mongodb nogroup   4096 Jan 20 13:54 _tmp  
 /media/craig/mongo/db/journal:  
 total 3145740  
 -rw------- 1 mongodb nogroup 1073741824 Jan 20 13:54 j._0  
 -rw------- 1 mongodb nogroup 1073741824 Jan 20 13:54 prealloc.1  
 -rw------- 1 mongodb nogroup 1073741824 Jan 20 13:54 prealloc.2  
 /media/craig/mongo/db/_tmp:  
 total 0  
 ls: cannot open directory /media/craig/mongo/lost+found: Permission denied  

Note the presence of the "mongodb" username in the user permissions column.

It's important to keep an eye on the log file.

Generally, when I received a "connection refused" error it was due to incorrect permissions. However, once the permissions were set correctly, I had to type "mongo" on the terminal line twice. The first time was a failure due to the lack of journal files; on the second time the command succeeded, and I gained access to the shell.

In another instance, I cloned the VM that held MongoDB.  When the clone started, I was unable to connect to the MongoDB shell with the same error (connection refused).  By restarting the service, and typing "mongo" twice on the terminal, access was restored.


References

  1. Setting Permissions
    1. If I change the dbPath to another directory on the same drive, this solution works for me.
  2. Changing Permissions on the Parent Directory
    1. This is the post that put me over the top.  
    2. The parent directory that held the mongo directory did not have the correct permissions.
  3. Changing the Default Path
    1. This is a detailed post.  
    2. While the solution did nothing for me, it was helpful to walk through a detailed approach that worked for someone else.

5 comments:

  1. Thank you very much for the post.

    ReplyDelete
  2. In case someone is working on azure and is running out of disk space because of mongodb, the following document would help in adding a new data disk - https://github.com/Azure/azure-content/blob/master/articles/virtual-machines/virtual-machines-linux-how-to-attach-disk.md

    ReplyDelete
  3. Thanks for sharing such a great information..Its really nice and informative.


    MongoDB Training Centers in Chenai

    ReplyDelete