三月 25th, 2010

mongodb + php 的应用

版本:centos 5.3, mongodb-linux-x86_64-1.2.4 , php-5.2.6
大家能看到这篇文章,估计也晓得mongodb是个什么东西了,no sql 大家熟悉吧,应该是no关系数据库。它和ttserver便是代表。它和ttserver都是各有优势,2个现在我都在用。
安装:
1.安装php驱动

wget http://pecl.php.net/get/mongo-1.0.5.tgz
tar xzvf mongo-1.0.5.tgz
cd mongo
/usr/local/php/bin/phpize
ls
./configure --with-php-config=/usr/local/php/bin/php-config
make
make install

2.下载mongodb,这个不用自己编译。

wget http://downloads.mongodb.org/linux/mongodb-linux-x86_64-1.2.4.tgz
tar xzvf mongodb-linux-x86_64-1.2.4.tgz
mv mongodb-linux-x86_64-1.2.4  /usr/local/mongodb

3.启动

  # 工具
  bin/mongod            - MongoDB server
  bin/mongo             - MongoDB client
 
  bin/mongodump         - MongoDB dump tool - for backups, snapshots, etc..
  bin/mongorestore      - MongoDB restore a dump
  bin/mongoexport       - Export a single collection to test (json,csv)
  bin/mongoimportjson   - Import a json file into a collection
 
  bin/mongofiles        - Utility for putting and getting files from MongoDB gridfs
 
  /usr/local/mongodb/bin/mongod --dbpath=/data/mongodb

4.php 使用,官方手册:http://www.php.net/manual/en/class.mongo.php

  $m = new Mongo('localhost:27017');
  $collection = $collection = $m->selectDB('foo')->selectCollection('bar');
  $person = array("name" => "joe");
 
  $collection->insert($person);
  $joe = $collection->findOne();
  print_r($joe);

测试:
我用5个进程,同时运行100w条数据,建唯一索引,数据完全没有出错,速度不错,大家可以试试。
在我的应用上,mongodb(10000wr/秒)的速度的确是比不上memcahce,也比ttserver差一些,但是胜在他能满足我的一些需求。

One Response to “mongodb + php 的应用”

  1. nosql 从09年出来后,比较火。不过他也不是灵药。

Write A Comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">