多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
[TOC] > [参考](https://www.ibm.com/developerworks/cn/opensource/os-mongodb-sharded-cluster/index.html) ## 概述 接下来我们开始准备部署一个具有 2 个 shard(3 节点副本集)加 1 个 config server(3 节点副本集)加 2 个 mongos 的分片集群 ### 创建配置副本集(>=1 台) ``` mongod --port=30000 --configsvr --replSet rep_confsvr --dbpath "D:\Program Files (x86)\mongodb-win32-x86_64-2012plus-4.2.2\shard\config" --bind_ip_all mongod --port=30001 --configsvr --replSet rep_confsvr --dbpath "D:\Program Files (x86)\mongodb-win32-x86_64-2012plus-4.2.2\shard\config1" --bind_ip_all mongod --port=30002 --configsvr --replSet rep_confsvr --dbpath "D:\Program Files (x86)\mongodb-win32-x86_64-2012plus-4.2.2\shard\config2" --bind_ip_all ``` 选择其中一个连接 ``` > mongo --port 30000 rs.initiate( { _id: "rep_confsvr", configsvr: true, members: [ { _id : 0, host : "192.168.0.130:30000" }, { _id : 1, host : "192.168.0.130:30001" }, { _id : 2, host : "192.168.0.130:30002" }, ] } ) //后期,添加成员 rs.add("192.168.0.130:30003") ``` ### 添加分片服务器(>=1 台)-shard1 ``` mongod --port 27002 --shardsvr --replSet shard1 --dbpath "D:\Program Files (x86)\mongodb-win32-x86_64-2012plus-4.2.2\data2" --bind_ip_all mongod --port 27003 --shardsvr --replSet shard1 --dbpath "D:\Program Files (x86)\mongodb-win32-x86_64-2012plus-4.2.2\data3" --bind_ip_all mongod --port 27004 --shardsvr --replSet shard1 --dbpath "D:\Program Files (x86)\mongodb-win32-x86_64-2012plus-4.2.2\data4" --bind_ip_all ``` 选择其中一个连接 ``` > mongo --port 27002 // 初始化 rs.initiate( { _id : "shard1", members: [ { _id : 0, host : "192.168.0.130:27002" }, { _id : 1, host : "192.168.0.130:27003" }, { _id : 2, host : "192.168.0.130:27004" } ] } ) //添加成员 // rs.add("192.168.0.130:27005") //删除成员,在集群启动时也可删除 // rs.remove("192.168.0.130:27005") ``` ### 添加分片服务器(>=1 台)-shard2 ``` mongod --port 27005 --shardsvr --replSet shard2 --dbpath "D:\Program Files (x86)\mongodb-win32-x86_64-2012plus-4.2.2\data11" --bind_ip_all mongod --port 27006 --shardsvr --replSet shard2 --dbpath "D:\Program Files (x86)\mongodb-win32-x86_64-2012plus-4.2.2\data12" --bind_ip_all mongod --port 27007 --shardsvr --replSet shard2 --dbpath "D:\Program Files (x86)\mongodb-win32-x86_64-2012plus-4.2.2\data13" --bind_ip_all ``` 选择其中一个连接 ``` mongo --port 27005 // 初始化 > rs.initiate( { _id : "shard2", members: [ { _id : 0, host : "192.168.0.130:27005" }, { _id : 1, host : "192.168.0.130:27006" }, { _id : 2, host : "192.168.0.130:27007" } ] } ) ``` ### 创建 mongos路由(可创建多个) ``` mongos --port 30010 --configdb rep_confsvr/192.168.0.130:30000,192.168.0.130:30001,192.168.0.130:30002 --bind_ip_all ``` 连接 mongos ``` mongo --port 30010 >sh.addShard("shard1/192.168.0.130:27002,192.168.0.130:27003,192.168.0.130:27004") > sh.addShard("shard2/192.168.0.130:27005,192.168.0.130:27006,192.168.0.130:27007") > sh.enableSharding("notice_set") //指定分片数据库 > sh.shardCollection("notice_set.test_shard",{"age": "hashed"}) // 第二参数 {"age":`1} ,表示根据范围 > sh.status() // 查看集群状态 ``` ### 连接mongodb 连接的端口和ip 为 mongos 的端口与ip ### 在集群中已有数据时,新增分片 ``` //检查 balancer 是否开启 mongos> sh.getBalancerState() // true //开启balancer mongos > sh.startBalancer(); mongos > sh.addShard("shard1/192.168.0.130:27002"); ``` > 新增分片的数据会自动迁移,原分片数据,需要过一会才能显示正确迁移后数据 ## 测试数据 根据范围的测试 ``` for (i = 1; i <= 20000; i++) db.test_shard.insert({age:(i%100), name:"user"+i, create_at:new Date()}) ``` 根据hash的测试 ``` for (i = 1; i <= 20000; i++) db.test_shard.insert({age:(i), name:"user"+i, create_at:new Date()}) ```