I installed MongoDB on my local computer as a service. I can open Compass and access databases from there. But I have a couple of question that are really confusing for me:
- Why is there
mongod
command? What does it do? I found it in documentation and it says
mongod is the primary daemon process for the MongoDB system. It handles data requests, manages data access, and performs background management operations.
But it is confusing, because I accessed server via Compass, without running mongod
. I can even create databases and collections from there. Then what is the purpose of mongod
?
- Why does
mongod
demandC:\data\db
folder? There is already a data folder inC:\Program Files\MongoDB\Server\5.0\data
.
What is the difference between these two folders?
Again, in the official docs it says that it is needed for storing data.
But, I think I was in C:\Program Files\MongoDB\Server\5.0\data
when I accessed database via Compass without mongod
(because obviously the first one does not exist yet). Why would I create C:\data\db
?
CodePudding user response:
1) Why is there mongod command? What does it do?
When we run mongod we are essentialy starting up a new database.
Since we should not interact with the mongod process directly we use a database client that is programmed to communicate with mongod.
Here, when we issue database commands like create or update this client takes care of communicating with mongod to execute these commands.
That's why we are using MongoDB Clients. If mongod process is not up and running this clients cannot communicate with mongod.
Thats why we are issuing sudo systemctl start mongod
Examples of MongoDB Clients:
- mogosh
- MongoDB Compass (GUI)
- Drivers (Node.js, Java, C/C )
2) On Windows, the dbpath is on the drive from which you start mongod process.
So looks like you started mongod process from the drive C:\Program Files\MongoDB\Server\5.0\data or you passed --dbpath flag
Otherwise it would have been C:\data\db
CodePudding user response:
But it is confusing, because I accessed server via Compass, without running
mongod
.
No, you cannot! MongoDB is installed as service which runs mongod
automatically at boot time.
Folder \data\db
(on the current drive) is the default folder used when no dbPath
is provided - neither in configuration file nor as command line parameter.
Looks like sometimes you start mongod
directly without any parameters, then above default applies. And sometimes mongod
is started as service where storage.dbPath: C:\Program Files\MongoDB\Server\5.0\data
is configured.
See also What is the default database path for MongoDB?
Update
My preferred way for Windows installation is this one:
Run the
.msi
installer or unzip the.zip
file. Uncheck "Install MongoD as a Service" optionCreate your personal
mongod.cfg
file according to your preferences (see sample below)Install MongoDB as service by running
mongod.exe --config <your mongod.cfg file> --install
Note, dbPath
folder needs to be created manually before you start the service. Ensure user of mongod process has write permissions to it.
Sample mongod.cfg file:
# Where and how to store data.
storage:
dbPath: C:\MongoDB\data\mongod
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: C:\MongoDB\log\mongod.log
# network interfaces
net:
port: 27017
bindIp: localhost
processManagement:
windowsService:
serviceName: MongoDB
displayName: MongoDB
description: MongoDB Server
security:
authorization: enabled
See mongoDB-oneClick