当前位置:Gxlcms > 数据库问题 > MongoDB的分片(9)

MongoDB的分片(9)

时间:2021-07-01 10:21:17 帮助过:2人阅读

 

 

以shop.user表为例

1: sh.shardCollection(‘shop.user’,{userid:1}); //user表用userid做shard key

 

2: for(var i=1;i<=40;i++) { sh.splitAt(‘shop.user‘,{userid:i*1000}) } // 预先在1K 2K...40K这样的界限切好chunk(虽然chunk是空的), 这些chunk将会均匀移动到各片上.

 

3: 通过mongos添加user数据. 数据会添加到预先分配好的chunk上, chunk就不会来回移动了.

 

 

在生成环境中我们使用replcation set与shard分片结合使用

 

1:在3台独立服务器上,分别运行 27017,27018,27019实例, 互为副本集,形成3套repl set

 

2: 在3台服务器上,各配置config server, 运行27020端口上

 

3: 配置mongos

./bin/mongos --port 30000 \

 --dbconfig 192.168.1.201:27020,192.168.1.202:27020,192.168.1.203:27020

 

4:连接路由器

./bin/mongo --port 30000

 

5: 添加repl set为片

>sh.addShard(‘192.168.1.201:27017’);

>sh.addShard(‘192.168.1.202:27017’);

>sh.addShard(‘192.168.1.203:27017’);

 

6: 添加待分片的库

>sh.enableSharding(databaseName);

 

7: 添加待分片的表

>sh.shardCollection(‘dbName.collectionName’,{field:1});

MongoDB的分片(9)

标签:

人气教程排行