Create data directory
MongoDB stores the data directory under the db directory. But this data directory will not be created actively, we need to create it after the installation is complete. Note that the data directory should be placed in the root directory (eg: C:\ or D:\ etc.).
In this tutorial, we have installed mongodb on C: drive, now let's create a data directory and then create the db directory inside the data directory.
c:\>cd c:\ c:\>mkdir data c:\>cd data c:\data>mkdir db c:\data>cd db c:\data\db>
You can also create these directories through Windows Explorer, not necessarily through the command line.
Run the MongoDB server from the command line
In order to run the MongoDB server from the command prompt, you must execute the mongod.exe file from the bin directory of the MongoDB directory.
mongod.exe --dbpath c:\data\db
If the execution is successful, the following information will be output:
2015-09-25T15:54:09.212+0800 I CONTROL Hotfix KB2731284 or later update isnot installed, will zero-out data files 2015-09-25T15:54:09.229+0800 I JOURNAL [initandlisten] journal dir=c:\data\db\j ournal 2015-09-25T15:54:09.237+0800 I JOURNAL [initandlisten] recover :no journal fil es present,no recovery needed 2015-09-25T15:54:09.290+0800 I JOURNAL [durability]Durability thread started 2015-09-25T15:54:09.294+0800 I CONTROL [initandlisten]MongoDB starting : pid=2488 port=27017 dbpath=c:\data\db 64-bit host=WIN-1VONBJOCE882015-09-25T15:54:09.296+0800 I CONTROL [initandlisten] targetMinOS:Windows7/W indows Server2008 R2 2015-09-25T15:54:09.298+0800 I CONTROL [initandlisten] db version v3.0.6……
Run the MongoDB server as a Windows service
Note that you must have administrative privileges to run the commands below. Execute the following command to run the MongoDB server as a Windows service:
The following table describes the parameters of mongodb startup:
Parameter Description--bind_ip | Bind the service IP. If you bind to 127.0.0.1, you can only access the local machine. Do not specify all the default local IPs. |
--logpath | Specify the MongoDB log file, note that the specified file is not a directory |
--logappend | write log using append |
--dbpath | Specify the database path |
--port | Specify the service port number, the default port is 27017 |
--serviceName | Specify service name |
--serviceDisplayName | Specify the service name, executed when there are multiple mongodb services. |
--install | Specifies to install as a Windows service. |
MongoDB background management shell
If you need to enter the background management of MongoDB, you need to open the bin directory under the mongodb installation directory first, and then execute the mongo.exe file. MongoDB Shell is the interactive Javascript shell that comes with MongoDB, which is used to operate and manage MongoDB. type environment.
When you enter the mongoDB background, it will link to the test document (database) by default:
> mongo MongoDB shell version:3.0.6 connecting to: test ……
Since it's a JavaScript shell, you can run some simple arithmetic operations:
>2+24>
The db command is used to view the document (database) of the current operation:
> db test >
Insert some simple record and look it up:
> db.runoob.insert({x:10})WriteResult({"nInserted":1})> db.runoob.find(){"_id":ObjectId("5604ff74a274a611b0c990aa"),"x":10}>
The first command inserts the number 10 into the x field of the runoob collection.