MongoDB及其php扩展安装

2014-11-24 17:55:28 · 作者: · 浏览: 0

#数据目录
dbpath = /home/qmhball/mongo/db
port = 9304
bind_ip = 10.1.146.163
#日志目录
logpath = /home/qmhball/mongo/log/mongo.log
logappend = true
#以后台Daemon形式运行服务
fork = true


执行


cd /home/qmhball/mongo/bin
./mongod --config mongod.conf


./mongo --host 10.1.146.163 --port 9304


进入交互模式


extension=/usr/local/lib/php/extensions/no-debug-non-zts-20090626/mongo.so


< php
$user = array(
'first_name' => 'MongoDB',
'last_name' => 'Fan',
'tags' => array('developer','user')
);
// Configuration
$dbhost = '10.1.146.163:9304';
$dbname = 'test';
// Connect to test database
$m = new Mongo("mongodb://$dbhost");
$db = $m->$dbname;
// Get the users collection
$users = $db->users;
//Insert this new document into the users collection
$res = $users->save($user);
var_dump($res);
$data = $users->findOne();
var_dump($data);


执行,得到


bool(true)

array(4) {

["_id"]=>

object(MongoId)#7 (1) {

["$id"]=>

string(24) "519cf324876d75714cb4e973"

}

["first_name"]=>

string(7) "MongoDB"

["last_name"]=>

string(3) "Fan"

["tags"]=>

array(2) {

[0]=>

string(9) "developer"

[1]=>

string(4) "user"

}

}