In this MongoDB tutorial, I am listing down the steps to install MongoDB on windows machine. Also, we will learn about few basic operations related to start and shutdown of MongoDB.
Table of Contents 1) Download MongoDB 2) Install MongoDB 3) Create mongo.config Configuration File 4) Start/Shutdown the MongoDB Server 5) MongoDB Windows Service 6) Download/Use MongoDB Java Driver 7) Verify MongoDB Installation
1. Download MongoDB
There are three builds of MongoDB for Windows:
1) MongoDB for Windows Server 2008 R2 edition [Download link]
2) MongoDB for Windows 64-bit [Download link]
3) MongoDB for Windows 32-bit [Download link]
c:\> wmic os get osarchitecture
More download options are given in official website of MongoDB.
2. Install MongoDB
The given links above will download zip files which you extract directly onto any place in your system of your choice. I have extracted them in “d:/mongodb
“. So, all code samples will in this post as well as future posts will refer to this location.
Also, please create following directories inside d:/mongodb
- D:\mongodb\data
- D:\mongodb\log
3. Create mongo.config Configuration File
This is important step before marching ahead. Create a normal text file in location d:/mongodb
and save it with name mongo.config
.
Now place the below configuration options in file. You can change the option’s values at your will.
##Which IP address(es) mongod should bind to. bind_ip = 127.0.0.1 ##Which port mongod should bind to. port = 27017 ##I set this to true, so that only critical events and errors are logged. quiet = true ##store data here dbpath=D:\mongodb\data ##The path to the log file to which mongod should write its log messages. logpath=D:\mongodb\log\mongo.log ##I set this to true so that the log is not overwritten upon restart of mongod. logappend = true ##log read and write operations diaglog=3 ##It ensures write durability and data consistency much as any journaling scheme would be expected to do. ##Only set this to false if you don’t really care about your data (or more so, the loss of it). journal = true
4. Start/Shutdown the MongoDB Server
To start the MongoDB server, use below command in command prompt:
mongod.exe –config d:\mongodb\mongo.config
D:\mongodb\bin>mongod --config D:\mongodb\mongo.config --journal 2014-05-25T16:51:18.433+0530 warning: --diaglog is deprecated and will be removed in a future release 2014-05-25T16:51:18.434+0530 diagLogging level=3 2014-05-25T16:51:18.435+0530 diagLogging using file D:\mongodb\data/diaglog.5381d22e
To connect to MongoDB from command prompt, use below command:
d:\mongodb\bin>mongo
D:\mongodb\bin>mongo MongoDB shell version: 2.6.1 connecting to: test Welcome to the MongoDB shell. For interactive help, type "help". For more comprehensive documentation, see https://docs.mongodb.com/ Questions? Try the support group http://groups.google.com/forum/#!forum/mongodb-user Server has startup warnings: 2014-05-25T16:52:09.158+0530 [initandlisten] 2014-05-25T16:52:09.158+0530 [initandlisten] ** NOTE: This is a 32 bit MongoDB binary. 2014-05-25T16:52:09.158+0530 [initandlisten] ** 32 bit builds are limited to less than 2GB of data (or less with --jour nal). 2014-05-25T16:52:09.158+0530 [initandlisten] ** See https://docs.mongodb.com/manual/faq/fundamentals/ 2014-05-25T16:52:09.158+0530 [initandlisten]
To shutdown the MongoDB server, you must be authorized user. So after getting auth complete, use below command in command prompt:
db.shutdownServer()
> use admin switched to db admin > db.shutdownServer() 2014-05-25T19:55:25.221+0530 DBClientCursor::init call() failed server should be down... 2014-05-25T19:55:25.224+0530 trying reconnect to 127.0.0.1:27017 (127.0.0.1) failed 2014-05-25T19:55:26.225+0530 warning: Failed to connect to 127.0.0.1:27017, reason: errno:10061 No connection could be made b ecause the target machine actively refused it. 2014-05-25T19:55:26.225+0530 reconnect 127.0.0.1:27017 (127.0.0.1) failed failed couldn't connect to server 127.0.0.1:27017 ( 127.0.0.1), connection attempt failed > quit() D:\mongodb\bin>
5. MongoDB Windows Service
To install the window service, use below command:
mongod –config D:\mongodb\mongo.config –install
Start the windows service from command prompt:
net start MongoDB
Stop the windows service from command prompt:
net stop MongoDB
Remove the windows service
mongod –remove
Sample run of all above four commands is below:
D:\mongodb\bin>mongod --config D:\mongodb\mongo.config --install 2014-05-25T20:07:41.191+0530 warning: --diaglog is deprecated and will be removed in a future release 2014-05-25T20:07:41.192+0530 diagLogging level=3 2014-05-25T20:07:41.193+0530 diagLogging using file D:\mongodb\data/diaglog.53820035 D:\mongodb\bin>net start MongoDB The MongoDB service was started successfully. D:\mongodb\bin>net stop MongoDB System error 109 has occurred. The pipe has been ended. D:\mongodb\bin>mongod --remove 2014-05-25T20:09:32.514+0530 2014-05-25T20:09:32.515+0530 warning: 32-bit servers don't have journaling enabled by default. Please use --journal if you wa nt durability. 2014-05-25T20:09:32.515+0530 2014-05-25T20:09:32.518+0530 Trying to remove Windows service 'MongoDB' 2014-05-25T20:09:32.520+0530 Service 'MongoDB' removed
6. Download/Use MongoDB Java Driver
Download the MongoDB java driver (mongo-java-driver-2.9.3.jar) from this download link. It’s a jar file you need to include in your classpath/ copy in lib folder in project where you want to use MongoDB.
7. Verify MongoDB Installation
To verify that MongoDB has been installed and working properly, execute below program:
package examples.mongodb.install; import java.net.UnknownHostException; import java.util.List; import com.mongodb.MongoClient; public class VerifyMongoDBInstallation { public static void main(String[] args) throws UnknownHostException { MongoClient mongo = new MongoClient("localhost", 27017); List<String> dbs = mongo.getDatabaseNames(); for (String db : dbs) { System.out.println(db); } } } Output: local admin
That’s all for MongoDB windows installation, startup and shutdown operations. Next, we will learn about some CRUD operations. Follow me to stay tuned.
Happy Learning !!
Dimple Aggarwal
hey can anyone help me out in this error..
C:\Windows\system32>mongod.exe –config d:\Mongodb\mongo.config
Error parsing INI config file: the options configuration file contains an invali
d line ‘∩╗┐’
try ‘mongod.exe –help’ for more information
i’m not understanding the error..
rajul
excellent post….
but in java code
it is
import com.mongodb.Mongo;
Rest all working…..
Samir
When I try to install windows service it runs properly but when I try to run net start MongoDB command it says service name is invalid. I checked in services there are no services related to mongodb
Joe
be careful if you are calling mongod.exe , not mongo.exe, there is big difference
Ashish
Hi Lokesh,
I am stuck at #4 of this installation document.
1) mongod.exe –config C:\***\Software\mongodb-2.6.1\mongo.config
When I execute this command, I get the following output
Invalid command: ΓÇôconfig
2) when I just try to run the mongo
I get the following output
MongoDB shell version: 2.6.1
connecting to: test
2014-06-10T15:13:43.160-0400 warning: Failed to connect to 127.0.0.1:27017, reas
on: errno:10061 No connection could be made because the target machine actively
refused it.
2014-06-10T15:13:43.193-0400 Error: couldn’t connect to server 127.0.0.1:27017 (
127.0.0.1), connection attempt failed at src/mongo/shell/mongo.js:146
exception: connect failed
What should be correct IP and port to be used
Please help.
Thanks
Ashish
Lokesh Gupta
Make sure you are not pasting any invalid character in command. Best way is to type the command yourself.
Ravi Kant Gaur
Hi , Lokesh
Thanks for this tutorial.
I am very interested to more about mangoDB.
I tried the installation. But the command which you have told for installation mongod –config D:\mongodb\mongo.config –install , for me on run of this command , it is giving me mangod.exe is not recognized as a internal or external command eventhough I have setted the path till D:\MangoDB\mongodb-win32-x86_64-2.6.1\bin in my path variable of environment variables.
And in mango.config file in following fileds
##Which IP address(es) mongod should bind to.
bind_ip = 127.0.0.1
##Which port mongod should bind to.
port = 27017
bind_ip will contain my machine IP address or something else , what port no you have given, is it by default port no. for mango db like other relational database oracle port 1521
Lokesh Gupta
Strange !! Anyways, it is definitely related to
PATH
only. You can try two options here:1) After updating the PATH variable in windows environment upto “d:/mongodb/bin” folder, Open a new command prompt window. This is necessary because new paths will be accessible in new command prompts only, not in old command windows.
2) Navigate in command prompt upto bin folder i.e. prompt should be “d:/mongodb/bin> “. Now execute install command.
Joe
be careful that you are calling mongod,exe. NOT mongo.exe
it give you the error
Lokesh Gupta
A real good suggestion.
Karthick
Command should be like: mongod –config D:\mongodb\mongo.config –install
Note the double hyphen.
Karthick
Strange.. the post is not showing two hyphens.. Make sure you have 2 hyphen symbol before config and install.
Lokesh Gupta
In command run example it is shown correctly already as “D:mongodbbin>mongod –config D:mongodbmongo.config –journal”
Yes, in bold section it is missing, and I am correcting it.
Lokesh Gupta
Hy Karthick, thanks for raising the question. In fact, It turned out that I had written the correct commands in post content, rather it was a known bug in wordpress which was converting double dashes to single dash.
https://wordpress.org/support/topic/issue-with-double-dash/