##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]
To find which version of Windows you are running, enter the following command in the Command Prompt:
c:\> wmic os get osarchitecture
More download options are given in official website of MongoDB.
##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.
It’s recommended to add d:/mongodb/bin to Windows environment variable, so that you can access the MongoDB’s commands in command prompt directly.
Also, please create following directories inside d:/mongodb
- D:\mongodb\data
- D:\mongodb\log
##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.
1 | ##Which IP address(es) mongod should bind to. |
##Start/Shutdown the MongoDB Server
To start the MongoDB server, use below command in command prompt:
mongod.exe –config d:\mongodb\mongo.config
1
2
3
4 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
1
2
3
4
5
6
7
8
9
10
11
12
13
14 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 http://docs.mongodb.org/
Questions? Try the support group http://groups.google.com/group/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 http://dochub.mongodb.org/core/32bit
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()
1
2
3
4
5
6
7
8
9
10
11
12
13 > 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>
##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:
1 | D:\mongodb\bin>mongod --config D:\mongodb\mongo.config --install |
##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.
##Verify MongoDB Installation
To verify that MongoDB has been installed and working properly, execute below program:
1 | package examples.mongodb.install; |
That’s all for MongoDB installation, startup and shutdown operations. Next, we will learn about some CRUD operations. Follow me to stay tuned.