Change mongodb data path to portable hard drive

Background

I have a 1TB portable usb hard drive which I want to host mongodb data. The OS is ubuntu 15.04

 

Procedure

stop your mongodb service

sudo service mongod stop

 

edit `/etc/fstab` so that the computer automatically mounts your portable drive under “mongodb” user everytime the OS boots up:

UUID=018483db-1b1c-4dd2-a373-58a6aa5a10be /               ext4    errors=remount-ro 0       1
# swap was on /dev/sdb5 during installation
UUID=ade6aa02-8f10-4251-9804-fe81850451a7 none            swap    sw              0       0i

# This line is added by you
# You must mount the portable drive under /media/mongodb
UUID=324fa516-3dba-4537-8e82-2a74ea20c4c6 /media/mongodb ext4  defaults  0   0

 

move the old data directory to `/media/mongodb` using `cp …`

 

make sure the new data path has mongodb:mongodb as owner and group.

chown -R mongodb:mongodb /media/mongodb/your_data_dir

 

change mongodb conf file by `vim /etc/mongod.conf’. Set the data path to new path.

 

restart mongodb

sudo service mongod restart

 

check if everything works by either:

sudo service mongod status

or:

tail /var/log/mongodb/mongodb.log (mongodb log path)

 

Reference

http://stackoverflow.com/questions/24781972/changing-mongod-dbpath-raise-permission-error

Leave a comment

Your email address will not be published. Required fields are marked *