---
title: "Windows平台下安装MongoDB"
author: "cakin24"
date: "2017-07-25T21:56:25Z"
tags: ["MongoDB", "Windows", "安装"]
categories: ["编程语言"]
slug: "windows-mongodb"
draft: false
lastmod: "2017-07-25T21:56:25Z"
id: 2386833
url: "https://cakin24.iteye.com/blog/2386833"
---

**一 MongoDB下载**

1、到MongoDB官网下载安装包。

2、MongoDB官网下载地址：https://www.mongodb.com/download-center#community

3、笔者下载的版本是：mongodb-win32-x86\_64-2008plus-ssl-3.2.10-signed.msi

4、下载后双击该文件，按操作提示安装即可。

笔者安装路径为：D:\\MongoDB

5、在path环境变量中加入：D:\\MongoDB\\Server\\3.2\\bin

 

**二 创建数据目录**

D:\\data\\db

 

**三 命令行下运行 MongoDB 服务器**

mongod.exe --dbpath d:\\data\\db

启动成功则提示如下：

2017-06-08T20:46:13.306+0800 I CONTROL  \[initandlisten] MongoDB starting : pid=204948 port=27017 dbpath=d:\\data\\db 64-bit host=DESKTOP-MNG19L3

2017-06-08T20:46:13.309+0800 I CONTROL  \[initandlisten] targetMinOS: Windows 7/Windows Server 2008 R2

2017-06-08T20:46:13.310+0800 I CONTROL  \[initandlisten] db version v3.2.10

2017-06-08T20:46:13.310+0800 I CONTROL  \[initandlisten] git version: 79d9b3ab5ce20f51c272b4411202710a082d0317

2017-06-08T20:46:13.310+0800 I CONTROL  \[initandlisten] OpenSSL version: OpenSSL 1.0.1t-fips  3 May 2016

2017-06-08T20:46:13.311+0800 I CONTROL  \[initandlisten] allocator: tcmalloc

2017-06-08T20:46:13.311+0800 I CONTROL  \[initandlisten] modules: none

2017-06-08T20:46:13.311+0800 I CONTROL  \[initandlisten] build environment:

2017-06-08T20:46:13.311+0800 I CONTROL  \[initandlisten]     distmod: 2008plus-ssl

2017-06-08T20:46:13.311+0800 I CONTROL  \[initandlisten]     distarch: x86\_64

2017-06-08T20:46:13.311+0800 I CONTROL  \[initandlisten]     target\_arch: x86\_64

2017-06-08T20:46:13.311+0800 I CONTROL  \[initandlisten] options: { storage: { dbPath: "d:\\data\\db" } }

2017-06-08T20:46:13.315+0800 I -        \[initandlisten] Detected data files in d:\\data\\db created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.

2017-06-08T20:46:13.316+0800 I STORAGE  \[initandlisten] wiredtiger\_open config: create,cache\_size=1G,session\_max=20000,eviction=(threads\_max=4),config\_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file\_manager=(close\_idle\_time=100000),checkpoint=(wait=60,log\_size=2GB),statistics\_log=(wait=0),

2017-06-08T20:46:15.185+0800 I NETWORK  \[HostnameCanonicalizationWorker] Starting hostname canonicalization worker

2017-06-08T20:46:15.185+0800 I FTDC     \[initandlisten] Initializing full-time diagnostic data capture with directory 'd:/data/db/diagnostic.data'

2017-06-08T20:46:15.202+0800 I NETWORK  \[initandlisten] waiting for connections on port 27017

 

**四 连接MongoDB数据库**

mongo -u &lt;user&gt; -p &lt;pass&gt; --host &lt;host&gt; --port 28015

例如：

C:\\Users\\lenovo&gt;mongo --host 127.0.0.1 --port 27017

MongoDB shell version: 3.2.10

connecting to: 127.0.0.1:27017/test

**五 MongoDB后台管理 Shell**

1、当你进入mongoDB后台后，它默认会链接到 test 文档（数据库）

C:\\Users\\lenovo&gt;mongo

MongoDB shell version: 3.2.10

connecting to: test

2、由于它是一个JavaScript shell，您可以运行一些简单的算术运算

&gt; 2+2

4

3、db命令用于查看当前操作的文档（数据库）

&gt; db

test

4、插入一些简单的记录并查找它：

&gt; db.runoob.insert({x:10})

WriteResult({ "nInserted" : 1 })

&gt; db.runoob.find()

{ "\_id" : ObjectId("5939485fae09c56bd9c1d374"), "x" : 10 }

&gt;

以上第一个命令将数字 10 插入到 runoob 集合的 x 字段中。

5、打开或创建文档（数据库）students

&gt; use students

switched to db students

6、向students集合（表）中插入一条记录并查找它。

&gt; zhangsan={'name':'zhangsan','age':18,'sex':'male'}

{ "name" : "zhangsan", "age" : 18, "sex" : "male" }

&gt; db.students.insert(zhangsan)

WriteResult({ "nInserted" : 1 })

&gt; db.students.find()

{ "\_id" : ObjectId("59394a87ae09c56bd9c1d375"), "name" : "zhangsan", "age" : 18, "sex" : "male" }

7、向当前系统查询所有文档（数据库）

&gt; show dbs

local     0.000GB

students  0.000GB

test      0.000GB

&gt;