用AI赚第一桶💰低成本搭建一套AI赚钱工具,源码可二开。 广告
[TOC] > [参考 runboo](https://www.runoob.com/mongodb/mongodb-sharding.html) ## 概述 在mongodb里面存在另一种集群,就是分片技术,可以满足mongodb数据量大量增长的需求 以下情况使用分片 * 复制所有的写入操作到主节点 * 延迟的敏感数据会在主节点查询 * 单个副本集限制在12个节点 * 当请求量巨大时会出现内存不足。 * 本地磁盘不足 * 垂直扩展价格昂贵 ## 教程 ### 端口分布 ``` Shard Server 1:27020 Shard Server 2:27021 Shard Server 3:27022 Shard Server 4:27023 Config Server :27100 Route Process:40000 ``` ## tar 安装mongo ### tar ``` wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.0.6.tgz tar -zxvf mongodb-linux-x86_64-3.0.6.tgz mv mongodb-linux-x86_64-3.0.6/ /usr/local/mongodb export PATH=/usr/local/mongodb/bin:$PATH ``` ### 启动Shard Server ``` [root@100 /]# mkdir -p /www/mongodb/shard/s0 [root@100 /]# mkdir -p /www/mongodb/shard/s1 [root@100 /]# mkdir -p /www/mongodb/shard/s2 [root@100 /]# mkdir -p /www/mongodb/shard/s3 [root@100 /]# mkdir -p /www/mongodb/shard/log [root@100 /]# /usr/local/mongodb/bin/mongod --port 27020 dbpath=/www/mongodb/shard/s0 --logpath=/www/mongodb/shard/log/s0.log --logappend --fork [root@100 /]# /usr/local/mongodb/bin/mongod --port 27021 dbpath=/www/mongodb/shard/s1 --logpath=/www/mongodb/shard/log/s1.log --logappend --fork [root@100 /]# /usr/local/mongodb/bin/mongod --port 27022 dbpath=/www/mongodb/shard/s2 --logpath=/www/mongodb/shard/log/s2.log --logappend --fork [root@100 /]# /usr/local/mongodb/bin/mongod --port 27023 dbpath=/www/mongodb/shard/s3 --logpath=/www/mongodb/shard/log/s3.log --logappend --fork ``` ### 启动Config Server ``` [root@100 /]# mkdir -p /www/mongodb/shard/config [root@100 /]# /usr/local/mongodb/bin/mongod --port 27100 --dbpath=/www/mongodb/shard/config --logpath=/www/mongodb/shard/log/config.log --logappend --fork ``` ### 启动Route Process ``` /usr/local/mongodb/bin/mongos --port 40000 --configdb localhost:27100 --fork --logpath=/www/mongodb/shard/log/route.log --chunkSize 500 ``` > mongos启动参数中,chunkSize这一项是用来指定chunk的大小的,单位是MB,默认大小为200MB. ### 配置Sharding ``` [root@100 shard]# /usr/local/mongodb/bin/mongo admin --port 40000 mongodb shell version: 2.0.7 connecting to: 127.0.0.1:40000/admin mongos> db.runCommand({ addshard:"localhost:27020" }) mongos> db.runCommand({ addshard:"localhost:27021" }) mongos> db.runCommand({ addshard:"localhost:27022" }) mongos> db.runCommand({ addshard:"localhost:27023" }) mongos> db.runCommand({ enablesharding:"test" }) #设置分片存储的数据库 mongos> db.runCommand({ shardcollection: "test.log", key: { id:1,time:1}}) ``` ### 修改连接端口为4000