mongodb分片设置

2013-11-14 14:52 来源:www.chinab4c.com 作者:admin

   上一章节我们谈了mongodb的主从复制,这次我们聊mongodb分片设置,其实很容易。以下谈谈具体要求

mkdir -p /data/mongodb/data
mkdir -p /data/mongodb/logs
/usr/local/mongodb/bin/mongod --dbpath=/data/mongodb/data --logpath /data/mongodb/logs/log.log --port=5555 -fork

mkdir -p /data/mongodb/data2
mkdir -p /data/mongodb/logs2
/usr/local/mongodb/bin/mongod --dbpath=/data/mongodb/data2 --logpath /data/mongodb/logs2/log.log --port=4444  -fork 

mkdir -p /data/mongodb/data3
mkdir -p /data/mongodb/logs3
/usr/local/mongodb/bin/mongod --dbpath=/data/mongodb/data3 --logpath /data/mongodb/logs3/log.log --port=2222  -fork 


/usr/local/mongodb/bin/mongos --port 3333 --configdb=192.168.100.58:2222

mongo 192.168.100.58:3333/admin

db.runCommand({"addshard":"192.168.100.58:5555",allowLocal:true})
db.runCommand({"addshard":"192.168.100.58:4444",allowLocal:true})
db.runCommand({"enablesharding":"test2"})
db.runCommand({"shardCollection":"test2.goods","key":{"goods_id":1}})
use test2
for(var i=0;i<100000;i++){db.goods.insert({"goods_id":i,"age":i})}

db.printShardingStatus()

php连接mongodb查询

$conn = new  Mongo("mongodb://192.168.100.58:3333/test2");
$db = $conn->selectDB("test2");

$tab = $db->goods;
$goods_id =1;
$res = $tab->findOne(array('goods_id'=>$goods_id));

来源:http://www.chinab4c.com