2011/07/16 11:37
文章作者:Enjoy 转载请注明原文链接。
官网:http://www.mongodb.org/
下载:http://www.mongodb.org/downloads
PHP扩展:http://cn.php.net/mongo
我选择了windows的安装包,下载了Windows 32-bit的1.8.2版本,以下是安装记录:
解压到了D:\www\mongodb
cd d:\www
bin\mongod.exe --dbpath=d:/www/mongodb/data
Sat Jul 09 09:03:28 [initandlisten] db version v1.8.2, pdfile version 4.5
引用
Sat Jul 09 09:03:28 [initandlisten] git version: 433bbaa14aaba6860da15bd4de8edf6
00f56501b
Sat Jul 09 09:03:28 [initandlisten] build sys info: windows (5, 1, 2600, 2, 'Ser
vice Pack 3') BOOST_LIB_VERSION=1_35
Sat Jul 09 09:03:28 [initandlisten] waiting for connections on port 27017
Sat Jul 09 09:03:28 [websvr] web admin interface listening on port 28017
00f56501b
Sat Jul 09 09:03:28 [initandlisten] build sys info: windows (5, 1, 2600, 2, 'Ser
vice Pack 3') BOOST_LIB_VERSION=1_35
Sat Jul 09 09:03:28 [initandlisten] waiting for connections on port 27017
Sat Jul 09 09:03:28 [websvr] web admin interface listening on port 28017
这样mongodb就在运行了,但DOS操作还不能关闭,一关闭就停了。因此需要将其设为系统服务:
D:\www\mongodb>bin\mongod.exe --dbpath=d:/www/mongodb/data --logpath=d:/www/mongodb/mongodb.log --install
引用
all output going to: d:/www/mongodb/mongodb.log
Creating service MongoDB.
Service creation successful.
Service can be started from the command line via 'net start "MongoDB"'.
Creating service MongoDB.
Service creation successful.
Service can be started from the command line via 'net start "MongoDB"'.
这样就可以在系统服务(services.msc)里就多了个MongoDB的服务,可以设置成 自动 来让windows每次启动时自动启动Mongo DB。
下面看其自带的客户端,仍然在其bin目录下,文件名为mongo.exe
bin\mongo.exe
引用
MongoDB shell version: 1.8.2
connecting to: test
connecting to: test
在http://cn.php.net/mongo下载mongodb的php扩展,PHP 5.2 VC6 Thread-Safe Mongo extension。
5.2版只有5.2.13的mongodb.dll。本地php重装了5.2.13,可以用了。
主要代码:
$m = new Mongo(); // 默认连接本机的27017端口
$mdb = $m -> hx; // 选择hx数据库,如果以前没有,则会自动创建,也可以用$m->selectDB("hx");
$collection = $mdb->hx_site; //选择hx里面的hx_site集合,相当于取hx库里的hx_site表,也可以使用$mdb->selectCollection("hx_site");
$mdb = $m -> hx; // 选择hx数据库,如果以前没有,则会自动创建,也可以用$m->selectDB("hx");
$collection = $mdb->hx_site; //选择hx里面的hx_site集合,相当于取hx库里的hx_site表,也可以使用$mdb->selectCollection("hx_site");
批量插入:
for($i=53;$i<84;$i++){
$start = $i * 10000;
$end = ($i+1) * 10000;
$sql = "SELECT * FROM hx_site WHERE id > $start AND id <= $end";
$tmp = $db->query($sql);
$arrs = array();
while ($arr = $db->fetch_array($tmp)) {
$arrs[] = $arr;
}
$collection->batchInsert($arrs);
}
$start = $i * 10000;
$end = ($i+1) * 10000;
$sql = "SELECT * FROM hx_site WHERE id > $start AND id <= $end";
$tmp = $db->query($sql);
$arrs = array();
while ($arr = $db->fetch_array($tmp)) {
$arrs[] = $arr;
}
$collection->batchInsert($arrs);
}
用php将本地有83.6w条数据的mysql转成mongodb,每次插入1w条,一次需要109~125ms。
/*
mysql语句对应的mongodb语法:http://www.itlearner.com/article/4843
取pagerank=5,按id倒序,取30条
类似于 SELECT * FROM hx_site WHERE pagerank = 5 ORDER BY id DESC LIMIT 30
*/
$cursor = $collection->find(array('pagerank'=>'5'))->sort(array('id'=>-1))->limit(30);
foreach ($cursor as $obj) { //遍历所有集合中的文档
echo $obj["id"].' '.$obj["domain"] . "<br>";
}
没索引的情况下取一条记录,656ms,mysql需要0.7s,基本上没区别。
count(*)某条件,800多ms,加索引后100多ms
参数文档:
mysql语句对应的mongodb语法
http://www.mongodb.org/display/DOCS/Quickstart+Windows
http://blog.chenlb.com/2010/03/mongodb-quick-start.html
http://blog.izhoufeng.com/posts/208.html

sphinx用c写的扩展性能还不如php写的api?
你见过ORDER BY -title DESC这种用法吗?

