To start working with MongoDB, we need to first understand the mongodb structure, where it maintains the data and importantly the logs.
Just complete the installation process, simply following the instructions. You may leave everything as its default. I generally prefer installing it out of Program files, like D:\ etc.
By default, on a Windows system, the installation process creates its data directory as C:\data. But it's all under our control, how we want to install its service.
Let's define some directories..
conf: for configuration files
data: for data storage location
Now under data, we need to create db and log directories.
db: where the actual database is stored
log: mongodb log file location
Either we can directly add this parameter while creating the MongoDb service or we can rather create a configuration file and use the same while creating Service.
net.bindIp: This config doesn't actually matter on a developer machine it should be set to localhost (127.0.0.1). On server machines, if we want to access the DB from any other machines, then we can either make it public (0.0.0.0) or some specific VLANs.
security.authorization: Either enabled or disabled. Let's keep it disabled for now. We'll see later how to create admin user and enable authorization. Learn more about it at How to Create User in MongoDB
This configuration file must not have any Tab space.
Now, let's create the MongoDB service using our configuration file
For removing the service:
Service name on command prompt is case-insensitive.
Installation
Setting mongodb on Windows based PC is quite simple. Just install the desired version from MongoDB Website. For development and learning, we should use Community edition.Just complete the installation process, simply following the instructions. You may leave everything as its default. I generally prefer installing it out of Program files, like D:\ etc.
Directory Setup
Let's assume you installed it in M:\mongodbBy default, on a Windows system, the installation process creates its data directory as C:\data. But it's all under our control, how we want to install its service.
Let's define some directories..
conf: for configuration files
data: for data storage location
Now under data, we need to create db and log directories.
db: where the actual database is stored
log: mongodb log file location
Service Setup
At this point, we are done defining the directories. Now, we need to tell MongoDB service about these custom directories. There are two ways to do that.Either we can directly add this parameter while creating the MongoDb service or we can rather create a configuration file and use the same while creating Service.
Define Configuration file
As we've kept M:\mongodb\conf for configuration files, let's create a file mongod.conf under the directory. A typical structure would contain following things:systemLog:
destination: file
path: M:\mongodb\data\log\mongod.log
storage:
dbPath: M:\mongodb\data\db
net:
bindIp: 127.0.0.1
security:
authorization: disabled
net.bindIp: This config doesn't actually matter on a developer machine it should be set to localhost (127.0.0.1). On server machines, if we want to access the DB from any other machines, then we can either make it public (0.0.0.0) or some specific VLANs.
security.authorization: Either enabled or disabled. Let's keep it disabled for now. We'll see later how to create admin user and enable authorization. Learn more about it at How to Create User in MongoDB
This configuration file must not have any Tab space.
Now, let's create the MongoDB service using our configuration file
mongod --config "M:\mongodb\conf\mongod.cfg" --serviceName "MongoDB" --serviceDisplayName "MongoDB" --serviceDescription "MongoDB Server Instance" --install
For removing the service:
mongod --remove
Start MongoDB Service
If the above command runs successfully, you should be able see a new service under Windows Services. Start it either from console or command prompt (admin mode)START: net start MongoDB
STOP: net stop MongoDB
Service name on command prompt is case-insensitive.
Connect to MongoDB
Add mongo to your System Path. Run below commands on a command window, opened as Admin#Add MONGO_HOME variable
SETX MONGO_HOME "M:\mongodb" /M
#Add MONGO_HOME variable to System Path
SETX PATH "%%MONGO_HOME%%\bin\;%PATH%;" /M
Open new cmd window and use mongo command to connect to DB.
Comments
Post a Comment