日期:2014-05-16  浏览次数:20462 次

MongoDB2.0路由集群配置

?

?

?

参考

http://www.javabloger.com/article/mongodb-cluster.html

?


我们实验有shard1,shard2,configserver,mongosRoute?

?

?

Shard1启动脚本
mkdir  database\shared1 

title: MongoDB shared1  Start by xiaofancn

cls
REM shard1服务端口是8001
bin\mongod.exe --shardsvr --dbpath database\shared1 --port 8001 --rest
?

?

?

Shard2启动脚本
mkdir  database\shared2 

title: MongoDB shared2 Start by xiaofancn

cls
REM shard2服务端口是8002
bin\mongod.exe --shardsvr --dbpath database\shared2 --port 8002 --rest

?

?

configServer启动脚本
mkdir  database\config 

title: MongoDB Config Start by xiaofancn

Cls
REM configServer服务端口是8000
bin\mongod.exe --configsvr --dbpath database\config --port 8000 --rest

?

?

mongoDB路由器启动脚本。
title: MongoDB Route Start by xiaofancn
REM 路由器连接configServer
bin\mongos --configdb localhost:8000

?

配置自动分片(Auto-sharding)

?

?

D:\Program Files\mongodb>bin\mongo
MongoDB shell version: 2.0.0
connecting to: test
mongos> use admin
switched to db admin
mongos> db.runCommand({addshard:"localhost:8001"});
{ "shardAdded" : "shard0000", "ok" : 1 }
mongos> db.runCommand({addshard:"localhost:8002"});
{ "shardAdded" : "shard0001", "ok" : 1 }
mongos> db.runCommand({enableSharding:"test"});
{ "ok" : 1 }
mongos> db.runCommand({shardcollection:"test.person",key:{_id:1}});
{ "collectionsharded" : "test.person", "ok" : 1 }
mongos> db.runCommand({listshards:1})
{
        "shards" : [
                {
                        "_id" : "shard0000",
                        "host" : "localhost:8001"
                },
                {
                        "_id" : "shard0001",
                        "host" : "localhost:8002"
                }
        ],
        "ok" : 1
}

?

?

?

?

? ? 扩展? ? ? ? ? shard节点的master/slave配置

?

?

mkdir  database\shared3 

title: MongoDB shared3 Start by xiaofancn

cls
rem 默认
bin\mongod.exe --shardsvr --master --dbpath database\shared3 --port 8003 --rest

?

?

mkdir  database\shared3Slave 

title: MongoDB shared3 Start by xiaofancn

cls
rem 默认
bin\mongod.exe --slave --source localhost:8003 --dbpath database\shared3Slave --rest
1 楼 ch2004 2011-10-12  
我配置后,启动没有没问题,用java 连接后步伐添加文档

Mongo m = new Mongo("localhost", 8000);
DB db = m.getDB("test");

// 取得DBCollection
DBCollection coll = db.getCollection("person");
//
// System.out.println("---------------------------------------------");
// //mongodb存储JSON格式的文档,而在Java中表示这种数据格式的最简便的类就是Map了。MongoDB Java
// Driver中提供的BasicDBObject就是个Map(它继承自LinkedHashMap并实现DBObject接口),它会将Map中的数据转换成BSON格式传输到mongodb
BasicDBObject doc = new BasicDBObject();
doc.put("id", "1");
doc.put("age", 28);
doc.put("time", new Date());
coll.insert(doc);
System.out.println("---------------------------------------------");
// //find
Set<String> colls = db.getCollectionNames();
for (String s : colls) {
System.out.println("table :" + s);
}

是否还有其它设置地方,求解
2 楼 ch2004 2011-10-12  
ch2004 写道