Introduction
In certain scenarios, you might find the need to change both the data and logs directory where MongoDB stores its data and log files. This could be due to various reasons, such as running out of disk space or simply wanting to organize your file structure. In this guide, we’ll walk through the process of moving both the MongoDB data and logs directory on an Ubuntu system.
Stop MongoDB
Before making any changes to the data directory, it’s crucial to ensure that MongoDB is stopped. Use the following command to stop the MongoDB service
sudo service mongod stop
Move Data and Logs Directories
Now that MongoDB is stopped, you can move the existing data and logs directories to new locations of your choice. Replace /path/to/new/data/directory
and /path/to/new/logs/directory
with the actual paths where you want to store the MongoDB data and logs
sudo mv /var/lib/mongodb /path/to/new/data/directory
sudo mv /var/log/mongodb /path/to/new/logs/directory
Update MongoDB Configuration
Next, you need to update the MongoDB configuration file to reflect the new data and logs directories. Open the configuration file using a text editor. In this example, I will use nano
sudo nano /etc/mongod.conf
Find the storage.dbPath
line and update it with the path to your new data directory, and add or update the systemLog.path
line with the path to your new logs directory
storage:
dbPath: /path/to/new/data/directory
systemLog:
path: /path/to/new/logs/directory
Save and exit the text editor.
Start MongoDB
With the data directory moved and the configuration updated, you can now start the MongoDB service
sudo systemctl start mongod
MongoDB should now be using the new data and logs directories specified in the configuration file. Be sure to replace /path/to/new/data/directory
and /path/to/new/logs/directory
with the actual paths you've chosen for your MongoDB data and logs storage.