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.
In this post will see about changing the data directory of the MongoDB
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
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/
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.