mongod --dbpath d:\mongodb\db\ --port 27021
mongod --dbpath c:\mongodb\db\ --port 27020
mongod --configsvr --dbpath e:\mongodb\db\ --port 27022
启动mongos的时候应将其连接到1个配置服务器上:
mongos --port 30000 --configdb 127.0.0.1:20000 --logpath "F:\mongo\logs\mongos\MongoDB.txt"
启动mongos的时候应将其连接到3个配置服务器上:
mongos --port 30000 --configdb 127.0.0.1:20000,127.0.0.1:20001,127.0.0.1:20002 --logpath "F:\mongo\logs\mongos\MongoDB.txt"
添加sharding,maxsize单位是M,此处设置比较小的数值只为演示sharding效果
db.runCommand({"addshard":"127.0.0.1:27018",name:"shard1",maxsize:2048,"allowLocal":true})
db.runCommand({"addshard":"127.0.0.1:27019",name:"shard2",maxsize:2048,"allowLocal":true})
注:如果要移除sharding,可用下面写法
db.runCommand( { removeshard : "localhost:10000" } );
查看shard节点列表
db.runCommand({listshards:1});
接下来创建相应数据库并设置其"可以sharding",新建自动切片的库user001:
config = connect("127.0.0.1:27020")
config = config.getSisterDB("config")
qnlbs=db.getSisterDB("qnlbs");
use admin
db.runCommand({enablesharding:"qnlbs"})
注:一旦enable了个数据库,mongos将会把数据库里的不同数据集放在不同的分片上。除非数据集被分片(下面会设置),否则一个数据集的所有数据将放在一个分片上。
db.printShardingStatus();
db.runCommand( { enablesharding : "qnmsg" } );
db.runCommand({ shardcollection: "qnlbs.user", key: { userID:1 }})
db.runCommand({ shardcollection: "qnmsg.user", key: { _id:1 }})
db.runCommand({"shardcollection":"test.refactor","key":{"name":1}})
db.pal.ensureIndex({ "pk" : 1},{ "pal" : "pk_1", "unique" : true });
db.user.ensureIndex({"userID":1});
db.runCommand({ shardcollection: "qnmsg.user", key: { _id:1 }})
db.runCommand( { shardcollection : "qnmsg.pal" } )
printShardingStatus()
db.pal.ensureIndex({"pal":1});
删除片操作
db.runCommand({"removeshard" : "10.0.0.11:27011"});
mongod --dbpath c:\mongodb\db\ --port 27020
mongod --configsvr --dbpath e:\mongodb\db\ --port 27022
启动mongos的时候应将其连接到1个配置服务器上:
mongos --port 30000 --configdb 127.0.0.1:20000 --logpath "F:\mongo\logs\mongos\MongoDB.txt"
启动mongos的时候应将其连接到3个配置服务器上:
mongos --port 30000 --configdb 127.0.0.1:20000,127.0.0.1:20001,127.0.0.1:20002 --logpath "F:\mongo\logs\mongos\MongoDB.txt"
添加sharding,maxsize单位是M,此处设置比较小的数值只为演示sharding效果
db.runCommand({"addshard":"127.0.0.1:27018",name:"shard1",maxsize:2048,"allowLocal":true})
db.runCommand({"addshard":"127.0.0.1:27019",name:"shard2",maxsize:2048,"allowLocal":true})
注:如果要移除sharding,可用下面写法
db.runCommand( { removeshard : "localhost:10000" } );
查看shard节点列表
db.runCommand({listshards:1});
接下来创建相应数据库并设置其"可以sharding",新建自动切片的库user001:
config = connect("127.0.0.1:27020")
config = config.getSisterDB("config")
qnlbs=db.getSisterDB("qnlbs");
use admin
db.runCommand({enablesharding:"qnlbs"})
注:一旦enable了个数据库,mongos将会把数据库里的不同数据集放在不同的分片上。除非数据集被分片(下面会设置),否则一个数据集的所有数据将放在一个分片上。
db.printShardingStatus();
db.runCommand( { enablesharding : "qnmsg" } );
db.runCommand({ shardcollection: "qnlbs.user", key: { userID:1 }})
db.runCommand({ shardcollection: "qnmsg.user", key: { _id:1 }})
db.runCommand({"shardcollection":"test.refactor","key":{"name":1}})
db.pal.ensureIndex({ "pk" : 1},{ "pal" : "pk_1", "unique" : true });
db.user.ensureIndex({"userID":1});
db.runCommand({ shardcollection: "qnmsg.user", key: { _id:1 }})
db.runCommand( { shardcollection : "qnmsg.pal" } )
printShardingStatus()
db.pal.ensureIndex({"pal":1});
删除片操作
db.runCommand({"removeshard" : "10.0.0.11:27011"});