How to change the MongoDB data store directory? MongoDB stores data in the data directory of the installation folder. However, sometimes it may be necessary to change the MongoDB data store directory in Windows or Linux due to several factors, including a lack of available storage space, security worries, or preferable organization.
Following are the steps to change the MongoDB data store directory.
Create new directory
Set appropriate permissions to the directory for user and group
Stop the MongoDB service
Modify the MongoDB configuration file
Start the MongoDB service
Verify the modified data store directory
1. Create a new directory
The first step is to create a new directory where we want to store the MongoDB data before modifying the location. We can choose any directory on our local device or a distant server. Here, to demonstrate, we have created a new directory NewData which is located in the C:drive. As we can see in the image of our new directory NewData below in the Local Disk (C) of our system.
2. Stop the MongoDB service
Now, we move into our next step to change the MongoDB data store directory. Before changing the MongoDB data store directory, we need to stop the MongoDB service. To stop the MongoDB service, navigate the services application from the windows start icon.
Then, open it to find the MongoDB server. Click on the MongoDB server and there we can see the stop option. By pressing the stop option for the services will be stopped as indicated by the service status shown in the following image.
You can also stop the mongoDB services by visiting the following link.
3. Modifying the MongoDB configuration file
The next step is to modify the MongoDB configuration file to tell MongoDB to use the new data store directory MongoDB configuration file contains all the settings and options that MongoDB uses to run.
By default, it is located in C:Program FilesMongoDBServer6.0bin as mongod.cfg. We have to open the mongod.cfg file and scroll down to the storage: section where the dbPath is specified as shown. In general, the path to the data storage directory is specified by this option.
Now, we have made changes to the dbPath property to the path of the new data store directory we created in the first step. Then, close the file after saving the modifications. The new path C:NewData is specified to the dbPath property as can be seen in the image below.
4. Start the MongoDB service
After modifying the MongoDB configuration file, our next step is to again start the MongoDB service that we stopped before. Again, open the service application in our system and select the MongoDB server and press the start option to start the services of MongoDB services in the system. With the image, we can see the service status as Running.
5. Verify the modified data store directory
The main step is to verify that MongoDB is using the new data store directory. For this, we have to open the mongosh shell or any MongoDB shell where we can easily run the MongoDB query. Then, we have to switch to the admin database by typing use admin. Then, run the following query for verification of the new data store directory.
# Verifying the modified data store directory
db.runCommand({getCmdLineOpts: 1})
This command is useful for checking the current configuration of the MongoDB server. When we run this command in the MongoDB shell, it returns a document that contains various configuration options used by the mongod process. There, we can see the modified storage section with the data store new directory. The following images assure that MongoDB data is now stored in the specified directory.
6. Conclusion
In conclusion, changing the MongoDB data store directory in Windows is a straightforward process. By following all the steps mentioned in this article, we can place the MongoDB data in a new location on the system or a remote server. Make sure to stop the MongoDB service before modifying the configuration file and verifying the new data.
How to change the MongoDB data store directory? MongoDB stores data in the data directory of the installation folder. However, sometimes it may be necessary to change the MongoDB data store directory in Windows or Linux due to several factors, including a lack of available storage space, security worries, or preferable organization. Following are the Read More MongoDB