Skip to content

How To Change MongoDB Default Data Path in Linux

How to change MongoDB default Data Path or Directory in Linux. Some times it required to do when we don't have enough space on current path or directory

Category
Databases
Covers
MongoDB
Versions & support
About MongoDB →

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).

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.

Rahul

Rahul Kumar is the founder and chief editor of TecAdmin.net. A Red Hat Certified Engineer (RHCE), he has worked in IT since 2009, with hands-on experience in Linux system administration, DevOps, web hosting, cloud platforms and automation. He started TecAdmin in 2013 to share practical, step-by-step guides, and leads its editorial work with one aim: making technical learning clear and useful for sysadmins, developers and beginners alike.

4 Comments

  1. #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

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


Back to top