MongoDB 快速入门及windows平台下的mongodb安装教程

官方教程(for windows):http://www.mongodb.org/display/DOCS/Quickstart+Windows

对 MongoDb 文档数据库感兴趣,要安装认识下。

到官方下载 win 版 http://www.mongodb.org/display/DOCS/Downloads 目前最新稳定版是 1.2.4。解压到 f:/sofr/mongodb-win32-i386-1.2.4。

1.启动 mongod 服务端

F:\soft\mongodb>bin\mongod.exe –dbpath=f:/mongodb
Mon Mar 08 11:13:17 Mongo DB : starting : pid = 0 port = 27017 dbpath = f:/mongodb master = 0 slave
= 0 32-bit

** NOTE: when using MongoDB 32 bit, you are limited to about 2 gigabytes of data
** see http://blog.mongodb.org/post/137788967/32-bit-limitations for more

Mon Mar 08 11:13:18 db version v1.2.4, pdfile version 4.5
Mon Mar 08 11:13:18 git version: 5cf582d3d96b882c400c33e7670b811ccd47f477
Mon Mar 08 11:13:18 sys info: windows (5, 1, 2600, 2, ‘Service Pack 3’) BOOST_LIB_VERSION=1_35
Mon Mar 08 11:13:18 waiting for connections on port 27017

–dbpath 指定数据库的目录,默认是 /data/db,win 没有 /data/db 目录,所以直接双击 mongod 是启动不了。默认端口是 27017

下面来将MongoDB加入到Windows服务(http://www.mongodb.org/display/DOCS/Windows+Service):

F:/soft/mongodb/bin>mongod.exe --dbpath=F:/mongodb/data --logpath=F:/mongodb/logs/logs.txt
--logappend --bind_ip 127.0.0.1 --directoryperdb --install

参数:

–dbpath   这个指定数据库的存放路径
–logpath   指定日志文件
–logappend   追加的方式
–bind_ip   绑定IP地址
–directoryperdb   每个数据库单独存放在一个文件夹内
–install   安装到Windows服务,默认服务名为 MongoDB

You may use the following options with –install, –reinstall and –remove: in the examples, {arg} is any text. Use quotes around the text if it includes spaces.

–serviceName {arg} Specify the service name (used with “net start/stop”, and usually does not include spaces)
–serviceDisplayName {arg} Specify the service’s display name
–serviceDescription {arg} Specify the service’s description
–serviceUser {arg} Specify login account for running the service (account must have the “Log on as a service” right)
–servicePassword {arg} Specify password for login account

成功之后可以看到类似下面的信息:

all output going to: F:/mongodb/logs/logs.txt
Creating service MongoDB.
Service creation successful.
Service can be started from the command line via ‘net start “MongoDB”‘.

注意:路径路径要使用绝对路径,不能使用相对路径.否则会出错!

启用MongoDB服务

f:/soft/mongodb/bin>net start  MongoDB

2.启动客户端

服务器启动后,用客户端试用下,它自带有个客户端(MongoDB shell)。bin/mongo.exe

F:\soft\mongodb-win32-i386-1.2.4>bin\mongo.exe
MongoDB shell version: 1.2.4
url: test
connecting to: test
type “exit” to exit
type “help” for help
> use test
switched to db test
> db.foo.save({a:1})
> db.foo.find()
{ “_id” : ObjectId(“4b946bc03f78000000001542”), “a” : 1 }
>

mongod.exe 后端可以看到日志。

对于php操作Mongodb教程:http://blog.haohtml.com/archives/5815

//============ python ==================

MongoDB 客户端丰富,我用 python 试下(安装请看:http://api.mongodb.org/python/1.4%2B/installation.html)。安装 pymongo

easy_install pymongo

  1. from pymongo import Connection
  2. connection = Connection()
  3. db = connection.test
  4. for f in db.foo.find():
  5. print “a=%s, _id=%s” % (f[‘a’], f[‘_id’])

可以看到结果。a=1.0, _id=4b946bc03f78000000001542。_id 是mongo 内部的字段。

它的像 sql 的查询比较吸引人 ……

window下安装mongodb的php客户端:http://www.php.net/manual/en/mongo.installation.php

参考:http://www.mongodb.org/display/DOCS/Quickstart