Facebook X (Twitter) Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook X (Twitter) Instagram
    TecAdmin
    You are at:Home»Databases»MongoDB»How To Change MongoDB Default Data Path in Linux

    How To Change MongoDB Default Data Path in Linux

    By RahulNovember 1, 20192 Mins Read

    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.

    configuration dbpath mongo mongodb
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    Create and Manage Users in MongoDB

    How to Install and Secure MongoDB on Ubuntu 22.04

    How to Connect MongoDB Database in Python

    View 4 Comments

    4 Comments

    1. Damien ALENGRIN on June 12, 2019 1:14 pm

      don’t forget :

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

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

      Thanks a lot pal, great article!

      Reply
    3. Mohamed-yassine BELATAR on June 27, 2018 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

      Reply
      • Andrey on October 31, 2019 6:37 pm

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

        Reply

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • Using .env Files in Django
    • Using .env File in FastAPI
    • Setting Up Email Notifications for Django Error Reporting
    • How to Enable Apache Rewrite (mod_rewrite) Module
    • What are Microservices?
    Facebook X (Twitter) Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.