• Home
  • Ubuntu 18.04
    • Whats New?
    • Upgrade Ubuntu
    • Install Java
    • Install Node.js
    • Install Docker
    • Install Git
    • Install LAMP Stack
  • Tutorials
    • AWS
    • Shell Scripting
    • Docker
    • Git
    • MongoDB
  • Funny Tools
  • FeedBack
  • Submit Article
  • About Us
TecAdmin
Menu
  • Home
  • Ubuntu 18.04
    • Whats New?
    • Upgrade Ubuntu
    • Install Java
    • Install Node.js
    • Install Docker
    • Install Git
    • Install LAMP Stack
  • Tutorials
    • AWS
    • Shell Scripting
    • Docker
    • Git
    • MongoDB
  • Funny Tools
  • FeedBack
  • Submit Article
  • About Us

How To Change MongoDB Default Data Path in Linux

Written by Rahul, Updated on November 1, 2019

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.

Share it!
Share on Facebook
Share on Twitter
Share on LinkedIn
Share on Reddit
Share on Tumblr
Share on Whatsapp
Rahul
Rahul
Connect on Facebook Connect on Twitter

I, Rahul Kumar am the founder and chief editor of TecAdmin.net. I am a Red Hat Certified Engineer (RHCE) and working as an IT professional since 2009..

4 Comments

  1. Avatar Damien ALENGRIN Reply
    June 12, 2019 at 1:14 pm

    don’t forget :

    chown mongod:mongod -R /mydirectory
    chmod 750 -R /mydirectory

  2. Avatar Jonatan Duttweiler Reply
    October 24, 2018 at 1:27 pm

    Thanks a lot pal, great article!

  3. Avatar Mohamed-yassine BELATAR Reply
    June 27, 2018 at 11:18 am

    #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

    • Avatar Andrey Reply
      October 31, 2019 at 6:37 pm

      Only Mohamed answer works! All the other have permissions issues.

Leave a Reply Cancel reply

Popular Posts

  • How To Install Python 3.9 on Ubuntu 20.04 5
  • How to Install Python 3.9 on CentOS/RHEL 7 & Fedora 32/31 0
  • How To Install VNC Server on Ubuntu 20.04 1
  • How To Install NVM on macOS with Homebrew 0
  • (Solved) apt-add-repository command not found – Ubuntu & Debian 0
© 2013-2020 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy