Change the data directory of the MongoDB

Sometimes, we need to change the default database data directory to some other path in the system. There could be many scenarios you may need to change the data directory, Your operating system will run on medium performance disk and you want your database to run on high-performance disk.

Sometimes your OS disk will be from the volume which has de-deuplication enabled but you don’t want that for your database disk.

Read more about changing the data directory of MySQL / MariaDB Database Server here

To install MongoDB in AWS Linux check here

In this post will see about changing the data directory of the MongoDB

MongoDB Directories

If you install MongoDB server using the package manager / followed my earlier post, your MongoDB data directory will be on /var/lib/mongodb and the log directory /var/log/mongodb

User and UserGroup

By default, MongoDB runs using the mongodb user account, if you change the configuration you must also modify the permission to the data directory and the log directories to give this user access to these directories

Configuration File

The official MongoDB package includes a configuration file (/etc/mongod.conf). These settings (such as the data directory and log directory specifications) take effect upon startup. That is, if you change the configuration file while the MongoDB instance is running, you must restart the instance for the changes to take effect.

Change the data directory

Set the permissions

ubuntu@ip-172-31-91-114:~$ sudo chown mongodb /datadir/

Read here on how to mount an another EBS volume to your EC2 instance

Edit the /etc/mongod.conf file

ubuntu@ip-172-31-91-114:~$ sudo nano /etc/mongod.conf

Change the dbPath value

# Where and how to store data.
storage:
  dbPath: /datadir/
  journal:
    enabled: true


Also published on Medium.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from

Subscribe now to keep reading and get access to the full archive.

Continue reading