mongoDB学习笔记一

Windows上安装mongoDB

下载地址:https://www.mongodb.org/downloads

双击安装-->next-->next-->custom-->设置安装目录(h:\MongoDB\)

 

运行mongonDB,根目录下创建\data\db目录

Cmd 进入安装根目录,执行如下命令

mongod.exe --dbpath h:\MongoDB\data

出现如下错误:

exception in initAndListen: 28663 Cannot start server. The default  is not available with this build of mongod. Please specify a different storage engine explicitly, e.g. --storageEngine=mma

 

执行如下命令:

mongod.exe --dbpath h:\MongoDB\data --storageEngine=mmapv1

看到如下结果:

H:\MongoDB\Server\3.2\bin>mongod.exe --dbpath h:\MongoDB\data --storageEngine=mmapv1

2016-11-20T09:08:10.655+0800 I CONTROL  [main]

2016-11-20T09:08:10.655+0800 W CONTROL  [main] 32-bit servers don't have journaling enabled by default. Please use --journal if you want durability.

2016-11-20T09:08:10.655+0800 I CONTROL  [main]

2016-11-20T09:08:10.671+0800 I CONTROL  [main] Hotfix KB2731284 or later update is not installed, will zero-out data files

2016-11-20T09:08:10.671+0800 I CONTROL  [initandlisten] MongoDB starting : pid=6232 port=27017 dbpath=h:\MongoDB\data 32-bit host=retacn

2016-11-20T09:08:10.671+0800 I CONTROL  [initandlisten] targetMinOS: Windows Vista/Windows Server 2008

2016-11-20T09:08:10.671+0800 I CONTROL  [initandlisten] db version v3.2.10

2016-11-20T09:08:10.671+0800 I CONTROL  [initandlisten] git version: 79d9b3ab5ce20f51c272b4411202710a082d0317

2016-11-20T09:08:10.671+0800 I CONTROL  [initandlisten] allocator: tcmalloc

2016-11-20T09:08:10.671+0800 I CONTROL  [initandlisten] modules: none

2016-11-20T09:08:10.671+0800 I CONTROL  [initandlisten] build environment:

2016-11-20T09:08:10.671+0800 I CONTROL  [initandlisten]     distarch: i386

2016-11-20T09:08:10.671+0800 I CONTROL  [initandlisten]     target_arch: i386

2016-11-20T09:08:10.671+0800 I CONTROL  [initandlisten] options: { storage: { dbPath: "h:\MongoDB\data", engine: "mmapv1" } }

2016-11-20T09:08:10.777+0800 I CONTROL  [initandlisten]

2016-11-20T09:08:10.777+0800 I CONTROL  [initandlisten] ** WARNING: This 32-bit MongoDB binary is deprecated

2016-11-20T09:08:10.778+0800 I CONTROL  [initandlisten] ** NOTE: This is a 32-bit MongoDB binary running on a 64-bit operating

2016-11-20T09:08:10.778+0800 I CONTROL  [initandlisten] **      system. Switch to a 64-bit build of MongoDB to

2016-11-20T09:08:10.779+0800 I CONTROL  [initandlisten] **      support larger databases.

2016-11-20T09:08:10.779+0800 I CONTROL  [initandlisten]

2016-11-20T09:08:10.779+0800 I CONTROL  [initandlisten]

2016-11-20T09:08:10.780+0800 I CONTROL  [initandlisten] ** NOTE: This is a 32 bit MongoDB binary.

2016-11-20T09:08:10.780+0800 I CONTROL  [initandlisten] **       32 bit builds are limited to less than 2GB of data (or less with --journal).

2016-11-20T09:08:10.780+0800 I CONTROL  [initandlisten] **       Note that journaling defaults to off for 32 bit and is currently off.

2016-11-20T09:08:10.782+0800 I CONTROL  [initandlisten] **       See http://dochub.mongodb.org/core/32bit

2016-11-20T09:08:10.784+0800 I CONTROL  [initandlisten]

2016-11-20T09:08:10.790+0800 I INDEX    [initandlisten] allocating new ns file h:\MongoDB\data\local.ns, filling with zeroes...

2016-11-20T09:08:11.211+0800 I STORAGE  [FileAllocator] allocating new datafile h:\MongoDB\data\local.0, filling with zeroes...

2016-11-20T09:08:11.213+0800 I STORAGE  [FileAllocator] creating directory h:\MongoDB\data\_tmp

2016-11-20T09:08:11.573+0800 I STORAGE  [FileAllocator] done allocating datafile h:\MongoDB\data\local.0, size: 64MB,  took 0.356 secs

2016-11-20T09:08:11.577+0800 I NETWORK  [HostnameCanonicalizationWorker] Starting hostname canonicalization worker

2016-11-20T09:08:11.577+0800 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory 'h:/MongoDB/data/diagnostic.data

'

2016-11-20T09:08:11.583+0800 I NETWORK  [initandlisten] waiting for connections on port 27017

 

MongoDB的默认使用27017端口

 

重新启动一个mongoDB,运行mongo.exe

H:\MongoDB\bin>mongo.exe

2016-11-20T09:31:58.985+0800 I CONTROL  [main] Hotfix KB2731284 or later update is not installed, will zero-out data files

MongoDB shell version: 3.2.11

connecting to: test

#插入两件记录

> db.foo.insert({test:1})

WriteResult({ "nInserted" : 1 })

> db.foo.insert({test:2})

WriteResult({ "nInserted" : 1 })

#查询记录

> db.foo.find()

{ "_id" : ObjectId("5830fd65465a96c3b983ef1d"), "test" : 1 }

{ "_id" : ObjectId("5830fd6d465a96c3b983ef1e"), "test" : 2 }

#移除一条记录

> db.foo.remove({test:1})

WriteResult({ "nRemoved" : 1 })

#查询记录

> db.foo.find()

{ "_id" : ObjectId("5830fd6d465a96c3b983ef1e"), "test" : 2 }

 

 

创建mongoDB服务

 

方法一:

Cmd-->ctrl+shiift+enter(以管理员身份运行)

创建数据和日志服务

创建配置文件

H:\MongoDB>mkdir h:\MongoDB\data\db

H:\MongoDB>mkdir h:\MongoDB\data\log

 

创建配置文件

根目录下创建mongod.cfg文件,添加如下内容:

systemLog:

    destination: file

    path: H:\MongoDB\data\log\mongod.txt

storage:

    dbPath: H:\MongoDB\data\db

 

安装服务

mongod.exe --config h:\MongoDB\mongod.cfg --install --serviceName MongoDB

 

 

第二种方法

sc.exe create MongoDB binPath= "\"h:\MongoDB\bin\mongod.exe\" --service --config=\"h:\MongoDB\mongod.cfg\"" DisplayName= "MongoDB" start= "auto"

 

开启服务

Net start MondoDB

 

关闭服务

Net stop MonDB

 

删除服务

方式一:

mongod.exe --remove  

方式二:

sc.exe delete MongoDB


posted @ 2016-11-20 11:40  retacn_yue  阅读(179)  评论(0编辑  收藏  举报