Introduction: Large data volume and throughput of the database will be a single-machine performance of a large pressure, a large number of data query will be a single CPU exhausted, the large amount of data on the single-machine storage pressure, and eventually exhaust the system's memory, the pressure transferred to disk IO.
Pros: 1. Using shards reduces the number of requests that each shard needs to process. 2. Using sharding reduces the data stored for each shard.
The composition of the MongoDB Shard cluster: Shard (shard server), config server (configuration servers), route (routing server)
Shard Server: Storing data
Configure server: Manage a shard server
Routing server: As a front-end route
Install mongodb3.2:
Install Openssl-devel and unzip to/opt
Easy to manage MONGO and mongod, make a link
yum install openssl-devel -ytar zxvf mongodb-linux-x86_64-3.2.1.tgz -C /opt/mv mongodb-linux-x86_64-3.2.1/ /usr/local/mongodbln -s /usr/local/mongodb/bin/mongo /usr/bin/mongoln -s /usr/local/mongodb/bin/mongod /usr/bin/mongod
Create a data file and log file for four instances
mkdir -p /data/mongodb/mongodb{1,2,3,4}mkdir /data/mongodb/logstouch /data/mongodb/logs/mongodb{1,2,3,4}.logchmod -R 777 /data/mongodb/logs/*.log
The value of Nlimit-u and Ulimit-n is greater than 20000
ulimit -n 25000ulimit -u 25000
Configure the server
cd /usr/local/mongodb/bin/ //在bin目录下vim mongodb1.confport=37017 //添加如下内容dbpath=/data/mongodb/mongodb1logpath=/data/mongodb/logs/mongodb1.loglogappend=truefork=truemaxConns=5000storageEngine=mmapv1configsvr=truemongod -f /usr/local/mongodb/bin/mongodb1.conf //启动服务sysctl -w vm.zone_reclaim_mode=0echo never > /sys/kernel/mm/transparent_hugepage/enabledecho never > /sys/kernel/mm/transparent_hugepage/defrag//某节点内存不足时,从其他节点分配内存
Shard Server
#mongodb2.confcp -p mongodb1.conf mongodb2.confvim mongodb2.conf //对mongodb2.conf进行配置port=47017 //端口号为47017dbpath=/data/mongodb/mongodb2 //数据文件logpath=/data/mongodb/logs/mongodb2.log //日志文件logappend=truefork=truemaxConns=5000storageEngine=mmapv1shardsvr=true#mongodb3.confcp -p mongodb1.conf mongodb3.confvim mongodb3.confport=47018 //端口为47018dbpath=/data/mongodb/mongodb3logpath=/data/mongodb/logs/mongodb3.loglogappend=truefork=truemaxConns=5000storageEngine=mmapv1shardsvr=true#开启两个服务mongod -f mongodb2.confmongod -f mongodb3.conf
Start the routing server
./mongos --port 27017 --fork --logpath=/usr/local/mongodb/bin/route.log --configdb 192.168.235.204:37017 --chunkSize 1 //必须在/usr/local/mongodb/bin下
Enable a shard server
mongomongos> show dbsmongos> sh.status() //shards下为空,没有分片服务器mongos> sh.addShard("192.168.177.133:47017") //添加47017端口的分片服务器mongos> sh.addShard("192.168.177.133:47018") //添加47018端口的分片服务器mongos> sh.status()
Shard function
Add a database to create the data in the collection
mongos> sh.enableSharding("school") #启用数据库分片mongos> db.info.createIndex({"id":1}) #对info表创建索引mongos> sh.shardCollection("school.info",{"id":1}) #表分片mongos> sh.status()
MongoDB Shard Cluster