Most of the Sysadmins don’t prefer to use / directory to store their files or databases. So if they have installed MongoDB database server, by default it stored all data in /var/lib/mongo (version/os specific). In this tutorial, we will change the MongoDB default data path to other directories where we have attached a new disk (EBS volume in AWS).

Advertisement

mongodb-medium-logo

Instruction’s to Change MongoDB Default Data Path:

1. Before making any changes, stop mongodb service

sudo systemctl stop mongod.service

2. Now change the location mongo directory to elsewhere on the filesystem as per need. For this tutorial, create a data directory under /home and sync directory /var/lib/mongo there using rsync.

After that create a symbolic link to a new directory to the original mongo directory location.

#### Copy mongo directory to new directory: 
sudo mkdir /home/data/
sudo rsync -av /var/lib/mongo /home/data/

#### Rename old directory for backup: 
sudo mv /var/lib/mongo /var/lib/mongo.bak

#### Create symbolic link to the new location:
sudo ln -s /home/data/mongo /var/lib/mongo

Update: These steps are suggested by our reader in comments and I have also tested on CentOS 8 system. Thank You Mohamed-yassine BELATAR,

3. Finally, start the MongoDB service using the following command. Now MongoDB will start using new directory (/home/data/mongo) as default data directory.

sudo systemctl start mongod.service

All done.

Share.

4 Comments

  1. Mohamed-yassine BELATAR on

    #Stop MongoDB Server
    service mongod stop

    # Copy mongo directory to new directory:
    rsync -av /var/lib/mongo /home/data/

    # Rename old directory
    mv /var/lib/mongo /var/lib/mongo.bak

    #symlink to the new location
    ln -s /home/data/mongo /var/lib/mongo

    #Start MongoDB Server
    service mongod start

Leave A Reply


Exit mobile version