摘抄mongodb官网:
Sharding is a method for distributing data across multiplemachines. MongoDB uses sharding to support deployments with very large datasets and high throughput operations.
Database systems with large data sets or high throughput applications canchallenge the capacity of a single server. For example, high query rates canexhaust the CPU capacity of the server. Working set sizes larger than thesystem’s RAM stress the I/O capacity of disk drives.
There are two methods for addressing system growth: vertical and horizontalscaling.
Vertical Scaling involves increasing the capacity of a single server, suchas using a more powerful CPU, adding more RAM, or increasing the amount ofstorage space. Limitations in available technology may restrict a singlemachine from being sufficiently powerful for a given workload. Additionally,Cloud-based providers have hard ceilings based on available hardwareconfigurations. As a result, there is a practical maximum for vertical scaling.
Horizontal Scaling involves dividing the system dataset and load overmultiple servers, adding additional servers to increase capacity as required.While the overall speed or capacity of a single machine may not be high, eachmachine handles a subset of the overall workload, potentially providing betterefficiency than a single high-speed high-capacity server. Expanding thecapacity of the deployment only requires adding additional servers as needed,which can be a lower overall cost than high-end hardware for a single machine.The trade off is increased complexity in infrastructure and maintenance forthe deployment.
MongoDB supports horizontal scaling throughsharding.
MongoDB分片主要有如下所述三个主要组件:
- Shard:
用于存储实际的数据块,实际生产环境中一个shard server角色可由几台机器组个一个replica set承担,防止主机单点故障
- Config Server:
mongod实例,存储了整个 ClusterMetadata,其中包括 chunk信息。
- Query Routers:
前端路由,客户端由此接入,且让整个集群看上去像单一数据库,前端应用可以透明使用。
config server: 27020,27021,27022
routers process:27023
(由于mongodb3.2开始,config servers for sharded clusters can bedeployed as a replica set.所以此处设置了3个config server做复制集)
1.启动shard server:
cd mogod目录
./bin/mongod --dbpath /mongodb/data/17 --logpath /mongodb/log/17.log --fork --smallfiles --port 27017 --shardsvr
./bin/mongod --dbpath /mongodb/data/18 --logpath /mongodb/log/18.log --fork --smallfiles --port 27018--shardsvr
2.启动config server:
./bin/mongod --dbpath /mongodb/data/20 --logpath /mongodb/log/20.log --fork --smallfiles --port 27020 --replSet rs1 --configsvr
./bin/mongod --dbpath /mongodb/data/21 --logpath /mongodb/log/21.log --fork --smallfiles --port 27021--replSet rs1 --configsvr
2.1./bin/mongo --port 27020
use admin;
var rsConf={_id:"rs1",members:[{_id:0,host:"ip:27020"},{_id:1,host:"ip:27021"},{_id:2,host:"ip:27022"}]}
rs.initiate(rsConf)
3.启动路由
./bin/mongos --logpath /mongodb/log/m23.log --port 27023 --fork --configdb rs1/ip:27020,ip:27021,ip:27022 --chunkSize 200
(mongos启动参数中,chunkSize这一项是用来指定chunk的大小的,单位是MB,默认大小为64MB.)
4.配置Sharding
./bin/mongo --port 27023
添加shard
sh.addShard("ip:27017")
sh.addShard("ip:27018")
sh.addShard("ip:27019")
设置分片存储的数据库test
sh.enableSharding("test")
设置分片的文档
sh.shardCollection("test.Log",{id:1})
添加数据测试:
use test
连接17,18,19 查看文档的数据