Skip to main content

How to setup MongoDB on Windows

To start working with MongoDB, we need to first understand the mongodb structure, where it maintains the data and importantly the logs.

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:\mongodb

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

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

Popular posts from this blog

MongoDB BulkWrite Java API

Since version 3.2, MongoDB has introduced Bulk Update methods. In context of RDBMS, it's like SQL Batch Jobs, where SQL Statements are prepared in different chunks and a batch of statements are submitted to DB for update/insert. Here are some important points about MongoDB Bulk Write operation.. Useful in case you've huge data to update/insert. Mongo automatically prepares batches (of 1000 default) and start execution in an ordered/unordered manner. This drastically reduce DB trip time. Let's say there are 50 thousand records to update, now instead of 50k round trips to DB from your app server, using Bulk Update it would be reduced to just 50 round trips. Let's see an example below: List<WriteModel<Document>> updateDocuments = new ArrayList<WriteModel<Document>>(); for ( Long entityId : entityIDs ) { //Finder doc Document filterDocument = new Document (); filterDocument . append ( "_id" , ent...

How to create users in MongoDB

Assuming you've already completed the basic setup process mentioned in this blog . In this example, we'll see how we can create admin user and enable authorization. Connect to admin database On a cmd window, connect to MongoDB by hitting command " mongo ". By default, it connects to " test " database. Once connection is successful, switch to admin db #mongo #use admin Create Role After switching to admin db, create executiveFunction role db . runCommand ({ createRole : "executeFunctions" , privileges : [ { resource : { anyResource : true }, actions : [ "anyAction" ] } ], roles : [], writeConcern : { w : "majority" , wtimeout : 5000 } }); Create admin user In this step, we'll create admin user mongoadmin with password mongo123 db . runCommand ( { "createUser" : "mongoadmin" , "pwd" : "mongo123...

MongoDB Backup and Restore

It's a general need in MongoDB development, to take backups or restore DB with an old backup. DB backup can be done at both DB level and individual Collection level. Let's see how we can perform different backup/restore operations.. DB Backup Complete DB Assuming database to backup is "mydb", the best thing is to back it as gzip to save some space on your Server. mongodump -- archive = mydb . 2017 - 09 - 21.gz -- gzip -- db mydb This will create the archive in the directory where you're executing the command. Another way is to take backup as bson documents. mongodump -h localhost -p 27017 -d mydb -o C:\mongobackup\20170921 A directory with db name will be created under  C:\mongobackup\20170921 In case you have multiple instances running on same server, you can use --host and --port options. Individual Collection mongodump -- collection myCollection -- db mydb This will create backup with same name as that of collection (myCollection in this...