1个分片服务器是否可以设置副本,而其他服务器则没有

时间:2013-10-25 02:38:19

标签: mongodb sharding

以下是否对MongoDB分片有效?

sh.addShard( "shard01.local:27017" );
sh.addShard( "shard02.local:27017" );
sh.addShard( "rs0/shard03.local:27017,shard04.local:27017,shard05.local:27017" );

意思是,我正在设置3个分片,但只有第三个分片是副本集。它不起作用,因为configsrv不理解rs0。

MongoDB会处理这个吗?或者如果其中一个分片是副本集,则所有分片都需要进行副本设置。

1 个答案:

答案 0 :(得分:1)

虽然这不是一个好主意(每个非复制的分片都是单点故障,所以您也可以将它们全部创建为非复制的),您可以混合使用复制和非复制的分片。下面的最小工作示例。我的猜测是你的配置中的其他地方有错误。

# Create some directories
mkdir -p ./s0/ ./s1/rs0 ./s1/rs1 ./s1/rs2 ./cfg/

# Start first shard 
mongod --logpath "s0.log" --dbpath ./s0/ --port 37017 --fork --shardsvr

# Start second shard
mongod --replSet s1 --logpath "s1-r0.log" --dbpath ./s1/rs0 --port 47017 --fork --shardsvr
mongod --replSet s1 --logpath "s1-r1.log" --dbpath ./s1/rs1 --port 47018 --fork --shardsvr
mongod --replSet s1 --logpath "s1-r2.log" --dbpath ./s1/rs2 --port 47019 --fork --shardsvr

# Start config server and mongos
mongod --logpath "cfg.log" --dbpath ./cfg/ --port 57040 --fork --configsvr
mongos --logpath "mongos.log" --configdb localhost:57040 --fork


# Configure rs
mongo --port 47017 << 'EOF'
rs.initiate(
    { _id: "s1", members:[
        { _id : 0, host : "localhost:47017" },
        { _id : 1, host : "localhost:47018" },
        { _id : 2, host : "localhost:47019" }]
    });
EOF


# Configure sharding
mongo <<'EOF'
db.adminCommand( { addshard : "localhost:37017" } );
db.adminCommand( { addshard : "s1/"+"localhost:47017,localhost:47018,localhost:47019" } );
db.adminCommand({enableSharding: "test"})
db.adminCommand({shardCollection: "test.foo", key: {bar: 1}});
EOF