分片副本集1 (3实例) 主1 从1 裁1
分片副本集2 (3实例) 主1 从1 裁1
配置副本集(3实例) 主1 从2
路由(2配置)
用Windows平台搭建
配置目录设置:
├─config │ CFG-ARBITER.cfg │ CFG-PRIMARY.cfg │ CFG-REPLICA.cfg │ ROUTER1.cfg │ ROUTER2.cfg │ RS1-ARBITER.cfg │ RS1-PRIMARY.cfg │ RS1-REPLICA.cfg │ RS2-ARBITER.cfg │ RS2-PRIMARY.cfg │ RS2-REPLICA.cfg
数据存储设置:
├─data │ ├─CFG-RS │ │ ├─ARBITER │ │ ├─PRIMARY │ │ └─REPLICA │ ├─RS-01 │ │ ├─ARBITER │ │ ├─PRIMARY │ │ └─REPLICA │ └─RS-02 │ ├─ARBITER │ ├─PRIMARY │ └─REPLICA
日志目录设置:
├─log │ │ cfg-arbiter.log │ │ cfg-primary.log │ │ cfg-replica.log │ │ router1.log │ │ rs-01-arbiter.log │ │ rs-01-primary.log │ │ rs-01-replica.log │ │ rs-02-arbiter.log │ │ rs-02-primary.log │ │ rs-02-replica.log
pid目录
├─pid │ cfg-arbiter.pid │ cfg-primary.pid │ cfg-replica.pid │ router1.pid │ rs-01-arbiter.pid │ rs-01-primary.pid │ rs-01-replica.pid │ rs-02-arbiter.pid │ rs-02-primary.pid │ rs-02-replica.pid
MongoDB源
└─source │ LICENSE-Community.txt │ mongo.cfg │ MPL-2 │ README │ THIRD-PARTY-NOTICES │ ├─bin │ Install-Compass.ps1 │ mongo.exe │ mongo.pdb │ mongod.exe │ mongod.pdb │ mongos.2022-02-03T02-19-37.mdmp │ mongos.2022-02-03T02-20-29.mdmp │ mongos.exe │ mongos.pdb │ vcredist_x64.exe
配置样本:
# mongod.conf # for documentation of all options, see: # http://docs.mongodb.org/manual/reference/configuration-options/ # where to write logging data. systemLog: destination: file logAppend: true path: "D:\\MongoDB ShardsCluster\\log\\rs-01-primary.log" # 指定好日志输出位置(每个实例都区分) # Where and how to store data. storage: dbPath: "D:\\MongoDB ShardsCluster\\data\\RS-01\\PRIMARY" # 指定好数据存放位置(每个实例都区分) journal: enabled: true # engine: # wiredTiger: # how the process runs processManagement: # fork: true # fork and run in background pidFilePath: "D:\\MongoDB ShardsCluster\\pid\\rs-01-primary.pid" # 进程号存放文件(每个实例都区分) # location of pidfile # timeZoneInfo: /usr/share/zoneinfo # network interfaces 27017 net: port: 27101 # 这里副本集1的 主:270101 从:27102 裁:27103,副本集2的 主:27201 从:27202 裁:27203,配置集 主:27301 从 27302 + 270303 bindIp: 0.0.0.0 # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting. #security: #operationProfiling: #replication: replication: #副本集的名称 副本集1 shards_rs_1 副本集2 shards_rs_2 配置集就写配置集 shards_config (名称自定义,建议是见名知义) replSetName: "shards_rs_1" #sharding: sharding: #分片角色 副本集1和2都是这个角色,不要改成别的东西, 配置集则改成【configsvr】 clusterRole: "shardsvr" ## Enterprise-Only Options #auditLog: #snmp:
路由配置则不一样:
# mongod.conf # for documentation of all options, see: # http://docs.mongodb.org/manual/reference/configuration-options/ # where to write logging data. systemLog: destination: file logAppend: true path: "D:\\MongoDB ShardsCluster\\log\\router1.log" # (路由1,2 区分) # Where and how to store data. # storage: # dbPath: "D:\\MongoDB ShardsCluster\\Router\\Node01\\data" # journal: #enabled: true # engine: # wiredTiger: # how the process runs processManagement: # fork: true # fork and run in background pidFilePath: "D:\\MongoDB ShardsCluster\\pid\\router1.pid" #(路由1,2 区分) # location of pidfile # timeZoneInfo: /usr/share/zoneinfo # network interfaces 27017 net: port: 27401 # 路由1 27401 路由2 27402 bindIp: 0.0.0.0 # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting. #security: #operationProfiling: #replication: #sharding: sharding: #指定配置节点副本集 路由1和路由2都写一样的 configDB: shards_config/192.168.124.9:27301,192.168.124.9:27302,192.168.124.9:27303 ## Enterprise-Only Options #auditLog: #snmp:
配置和目录都建好之后,安装服务到Windows服务列表中
先把副本集处理好
# 分片副本集和配置副本集的服务安装 "D:\MongoDB ShardsCluster\source\bin\mongod.exe" --config "D:\MongoDB ShardsCluster\config\RS1-PRIMARY.cfg" --serviceName "MongoDB-RS1-PRIMARY" --serviceDisplayName "MongoDB-RS1-PRIMARY" --install "D:\MongoDB ShardsCluster\source\bin\mongod.exe" --config "D:\MongoDB ShardsCluster\config\RS1-REPLICA.cfg" --serviceName "MongoDB-RS1-REPLICA" --serviceDisplayName "MongoDB-RS1-REPLICA" --install "D:\MongoDB ShardsCluster\source\bin\mongod.exe" --config "D:\MongoDB ShardsCluster\config\RS1-ARBITER.cfg" --serviceName "MongoDB-RS1-ARBITER" --serviceDisplayName "MongoDB-RS1-ARBITER" --install "D:\MongoDB ShardsCluster\source\bin\mongod.exe" --config "D:\MongoDB ShardsCluster\config\RS2-PRIMARY.cfg" --serviceName "MongoDB-RS2-PRIMARY" --serviceDisplayName "MongoDB-RS2-PRIMARY" --install "D:\MongoDB ShardsCluster\source\bin\mongod.exe" --config "D:\MongoDB ShardsCluster\config\RS2-REPLICA.cfg" --serviceName "MongoDB-RS2-REPLICA" --serviceDisplayName "MongoDB-RS2-REPLICA" --install "D:\MongoDB ShardsCluster\source\bin\mongod.exe" --config "D:\MongoDB ShardsCluster\config\RS2-ARBITER.cfg" --serviceName "MongoDB-RS2-ARBITER" --serviceDisplayName "MongoDB-RS2-ARBITER" --install "D:\MongoDB ShardsCluster\source\bin\mongod.exe" --config "D:\MongoDB ShardsCluster\config\CFG-PRIMARY.cfg" --serviceName "MongoDB-CFG-PRIMARY" --serviceDisplayName "MongoDB-CFG-PRIMARY" --install "D:\MongoDB ShardsCluster\source\bin\mongod.exe" --config "D:\MongoDB ShardsCluster\config\CFG-REPLICA.cfg" --serviceName "MongoDB-CFG-REPLICA" --serviceDisplayName "MongoDB-CFG-REPLICA" --install "D:\MongoDB ShardsCluster\source\bin\mongod.exe" --config "D:\MongoDB ShardsCluster\config\CFG-ARBITER.cfg" --serviceName "MongoDB-CFG-ARBITER" --serviceDisplayName "MongoDB-CFG-ARBITER" --install # 启动配置 NET START "MongoDB-RS1-PRIMARY" NET START "MongoDB-RS1-REPLICA" NET START "MongoDB-RS1-ARBITER" NET START "MongoDB-RS2-PRIMARY" NET START "MongoDB-RS2-REPLICA" NET START "MongoDB-RS2-ARBITER" NET START "MongoDB-CFG-PRIMARY" NET START "MongoDB-CFG-REPLICA" NET START "MongoDB-CFG-ARBITER"
如果出现错误了,可以及时停止服务并卸载
# 停止服务 NET STOP "MongoDB-RS1-PRIMARY" NET STOP "MongoDB-RS1-REPLICA" NET STOP "MongoDB-RS1-ARBITER" NET STOP "MongoDB-RS2-PRIMARY" NET STOP "MongoDB-RS2-REPLICA" NET STOP "MongoDB-RS2-ARBITER" NET STOP "MongoDB-CFG-PRIMARY" NET STOP "MongoDB-CFG-REPLICA" NET STOP "MongoDB-CFG-ARBITER" # 删除服务 SC DELETE "MongoDB-RS1-PRIMARY" SC DELETE "MongoDB-RS1-REPLICA" SC DELETE "MongoDB-RS1-ARBITER" SC DELETE "MongoDB-RS2-PRIMARY" SC DELETE "MongoDB-RS2-REPLICA" SC DELETE "MongoDB-RS2-ARBITER" SC DELETE "MongoDB-CFG-PRIMARY" SC DELETE "MongoDB-CFG-REPLICA" SC DELETE "MongoDB-CFG-ARBITER"
创建客户端的快捷方式
# 副本集1 主节点登陆 "D:\MongoDB ShardsCluster\source\bin\mongo.exe" --port=27101 # 副本集2 主节点登陆 "D:\MongoDB ShardsCluster\source\bin\mongo.exe" --port=27201 # 配置集 主节点登陆 "D:\MongoDB ShardsCluster\source\bin\mongo.exe" --port=27301 # 路由1和路由2 "D:\MongoDB ShardsCluster\source\bin\mongo.exe" --port=27401 "D:\MongoDB ShardsCluster\source\bin\mongo.exe" --port=27402
登陆主节点进行副本初始化配置
# RS1-PRIMARY 添加副本节点 和仲裁节点 rs.initiate() rs.conf() rs.add("192.168.124.9:27102") rs.addArb("192.168.124.9:27103") # RS2-PRIMARY 添加副本节点 和仲裁节点 rs.initiate() rs.add("192.168.124.9:27202") rs.addArb("192.168.124.9:27203") # CFG-PRIMARY 配置副本集主节点追加从节点 rs.initiate() rs.add("192.168.124.9:27302") rs.add("192.168.124.9:27303")
确认副本集都创建无误后再开始启动路由:
路由配置,注意路由是用的mongos.exe,副本集是用mongod.exe
"D:\MongoDB ShardsCluster\source\bin\mongos.exe" --config "D:\MongoDB ShardsCluster\config\ROUTER1.cfg" --serviceName "MongoDB-ROUTER1" --serviceDisplayName "MongoDB-ROUTER1" --install "D:\MongoDB ShardsCluster\source\bin\mongos.exe" --config "D:\MongoDB ShardsCluster\config\ROUTER2.cfg" --serviceName "MongoDB-ROUTER2" --serviceDisplayName "MongoDB-ROUTER2" --install NET START "MongoDB-ROUTER1" NET START "MongoDB-ROUTER2" NET STOP "MongoDB-ROUTER1" NET STOP "MongoDB-ROUTER2" SC DELETE "MongoDB-ROUTER1" SC DELETE "MongoDB-ROUTER2"
进入任意路由进行分片添加
(发现直接写IP地址添加会报错说节点不属于副本集,所以改成主机名的了)
sh.addShard("shards_rs_1/DESKTOP-P46A09B:27101,DESKTOP-P46A09B:27102,DESKTOP-P46A09B:27103") sh.addShard("shards_rs_2/DESKTOP-P46A09B:27201,DESKTOP-P46A09B:27202,DESKTOP-P46A09B:27203")