Posts Tagged ‘mongo’

MAC OS 下快速安装mongo数据库

January 8th, 2010

1. 最便捷的方式是采用port安装,
只需运行

sudo port install mongodb

port会自动将mongo,mongd等命令添加到你的系统路径中

2. 源代码安装

# make default directory for data
$ mkdir -p /data/db

# using curl, get the pre-built distro
$ curl -O http://downloads.mongodb.org/osx/mongodb-osx-x86_64-latest.tgz

# unpack
$ tar xzf mongodb-osx-x86_64-latest.tgz

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

使用Mongodb php驱动

January 8th, 2010

1. 首先在mongo官网上下载mongodb数据库

http://www.mongodb.org/display/DOCS/Downloads

2. 安装PHP驱动
不同操作系统的安装方法

http://www.mongodb.org/display/DOCS/PHP+Language+Center

在linux下只需要跑下面的命令就安装完成了

sudo pecl install mongo

在php.ini中添加扩展模块,然后查看phpinfo,是否已经启用了mongo扩展。

Mongo 配置参数
名称 默认值 位置
mongo.default_host “localhost” PHP_INI_ALL
mongo.default_port 27017 PHP_INI_ALL
mongo.auto_reconnect true PHP_INI_SYSTEM
mongo.allow_persistent true PHP_INI_SYSTEM
mongo.max_persistent -1 PHP_INI_SYSTEM
mongo.max_connections -1 PHP_INI_SYSTEM
mongo.chunk_size 262144 PHP_INI_SYSTEM
mongo.cmd “$” PHP_INI_ALL

3. 编写测试脚本

// connect
$m = new Mongo();
// select a database
$db = $m->comedy;
$collection = $db->cartoons;
// add an element
$obj = array( "title" => "Calvin and Hobbes", "author" => "Bill Watterson" );
$collection->insert($obj);

// add another element, with a different "shape"
$obj = array( "title" => "XKCD", "online" => true );
$collection->insert($obj);

// find everything in the collection
$cursor = $collection->find();
// iterate through the results
foreach ($cursor as $obj) {
    echo $obj["title"] . "\n";
}

// disconnect
$m->close();

运行上面的脚本输出

Calvin and Hobbes
XKCD

就说明你已经成功安装了mongo db和php驱动