How To Install MongoDB On Windows

Let's install MongoDB on a Windows machine. 
 

Find the Current Stable Release of the Community Server, and select the latest 64-bit / 32-bit version in the Windows column. Download, then run the MSI installer.
 
2. MongoDB is typically installed in C:\Program Files\MongoDB. Search for Environment Variables on the desktop and add the MongoDB binaries path to the PATH variable. For example, you might find the binaries at C:\Program Files\MongoDB\Server\3.4\bin on your machine.
3. Create MongoDB data and log directories on a disk (such as drive F:) you created in the preceding steps.
 
From Start, select Command Prompt to open a command prompt window.
 
Type:

  1. C:\> F:  
  2.   
  3. F:\> mkdir \MongoData  
  4.   
  5. F:\> mkdir \MongoLogs  

To run the database move to the C:\Program Files\MongoDB\Server\3.4\bin path, run:

C:\Program Files\MongoDB\Server\3.4\bin> mongod --dbpath F:\MongoData\ --logpath F:\MongoLogs\mongolog.log

All log messages are directed to the F:\MongoLogs\mongolog.log file as mongod.exe server starts and preallocates journal files. It may take several minutes for MongoDB to preallocate the journal files and start listening for connections. The command prompt stays focused on this task while your MongoDB instance is running.

4. To start the MongoDB administrative shell, open another command window from Start and type the following commands,
  1. C:\> cd \my_mongo_dir\bin   
  2.   
  3. C:\my_mongo_dir\bin> mongo   
  4.   
  5. >db   
  6.   
  7. test  
  8.   
  9. > db.foo.insert( { a : 1 } )   
  10.   
  11. > db.foo.find()   
  12.   
  13. { _id : ..., a : 1 }   
  14.   
  15. > show dbs   
  16.   
  17. ...   
  18.   
  19. > show collections   
  20.   
  21. ...   
  22.   
  23. > help    

The database is created by the insert.

Alternatively, you can install exe as a service:

  1. C:\> mongod --dbpath F:\MongoData\ --logpath F:\MongoLogs\mongolog.log --logappend --install  

A service is installed named MongoDB with a description of "Mongo DB". The --logpath option must be used to specify a log file, since the running service does not have a command window to display output. The --logappend option specifies that a restart of the service causes output to append to the existing log file. The --dbpath option specifies the location of the data directory. For more service-related command-line options, see Service-related command-line options.

5. To start the service, run this command:

C:\> net start MongoDB

Now MongoDB is installed and running.

Happy coding.


Similar Articles