|
Mongodb安装搭建Replica Set+Sharding集群
一.简介
Replica Set:复制在为数据提供了冗余同时,也提高了数据的可用性。由于在不同的数据库服务器上拥有多个数据镜像,复制可以有效的防止由于单台服务器故障而导致的数据丢失。复制还能够帮助我们从硬件故障或是服务中断中恢复数据。复制集是由一组拥有相同数据集的 mongod 实例组成的。其中的一个节点为主节点(Primary),所有的写请求都是在它上面完成的。而其他的节点都是从节点(secondary),从节点接收从主节点上传来的操作并应用,并以此来保证其与主节点的数据集一致。
Sharding:分片是使用多个机器存储数据的方法,MongoDB使用分片以支持巨大的数据存储量与对数据操作.。高数据量和吞吐量的数据库应用会对单机的性能造成较大压力,大的查询量会将单机的CPU耗尽,大的数据量对单机的存储压力较大,最终会耗尽系统的内存而将压力转移到磁盘IO上。
Shard server:存储实际数据分片。每个shard可以是一个mongod实例,也可以是一组mongod实例构成的replicasets为了提供高可用性和数据的一致性,shard内部的故障自动切换,建议每个shard为一组replica sets。
Config server:所有shard节点的配置信息,每个分片的shard key范围,分片在各个shard的分布情况,集群中所有db和collection的sharding的配置信息。配置一个数据集存储于那些分片中。
Router server:客户端接入,先询问配置服务器需要到那个shard上查询或者保存记录然后连接shard执行,最后返回结果给客户端。分片群集可以包含多个查询路由器来划分客户端请求负载。客户端发送请求给一个查询的路由器。大多数分片集群有很多查询的路由器。
二.服务器以及服务信息
服务器
Ip
服务及端口
CentosA
10.255.4.200
mongod shard1_1:27017
mongod shard2_1:27018
mongod config:20000
Mongs:30000
CentosB
10.255.4.201
mongod shard1_2:7017
mongod shard2_2:7018
mongod config:20000
Mongs:30000
centosC
10.255.4.202
mongod shard1_3:27017
mongod shard2_3:27018
mongod config:20000
Mongs:30000
三.配置步骤和命令
1. 连接服务器
ssh root@10.255.4.200
ssh root@10.255.4.201
ssh root@10.255.4.202
2. 建立数据和日志的目录
centosA:
mkdir -p /data/shard1_1
mkdir -p /data/shard2_1
mkdir -p /data/config
centosB:
mkdir -p /data/shard1_2
mkdir -p /data/shard2_2
mkdir -p /data/config
centosC:
mkdir -p /data/shard1_3
mkdir -p /data/shard2_3
mkdir -p /data/config
3. 安装mongodb
CentosA:yum –y install mongodb*
centosB:yum –y install mongodb*
centosC:yum –y install mongodb*
4. 启动分片mongod服务
centosA:
service mongod stop 停止已经启动的服务,可能会占用端口,导致后面报错
mongod --shardsvr --replSet shard1 --port 27017 --dbpath /data/shard1_1 --logpath /data/shard1_1/shard1_1.log --logappend --fork
mongod --shardsvr --replSet shard2 --port 27018 --dbpath /data/shard2_1 --logpath /data/shard2_1/shard2_1.log --logappend --fork
centosB:
service mongod stop
mongod --shardsvr --replSet shard1 --port 27017 --dbpath /data/shard1_2 --logpath /data/shard1_2/shard1_2.log --logappend --fork
mongod --shardsvr --replSet shard2 --port 27018 --dbpath /data/shard2_2 --logpath /data/shard2_2/shard2_2.log --logappend –fork
centosC:
service mongod stop
mongod --shardsvr --replSet shard1 --port 27017 --dbpath /data/shard1_3 --logpath /data/shard1_3/shard1_3.log --logappend --fork
mongod --shardsvr --replSet shard2 --port 27018 --dbpath /data/shard2_3 --logpath /data/shard2_3/shard2_3.log --logappend –fork
5. 任意连接一台centos配置分片和数据集
mongo --port 27017
config= = {_id:'shard1',members:[{_id:0,host:'10.255.4.200:27017'},{_id:1,host:'10.255.4.201:27017'},{_id:2,host:'10.255.4.202:27017'}]}
rs.initiate(config)
mongo --port 27018
config= = {_id:'shard2',members:[{_id:0,host:'10.255.4.200:27018'},{_id:1,host:'10.255.4.201:27018'},{_id:2,host:'10.255.4.202:27018'}]}
rs.initiate(config)
6. 启动config mongod服务
CentosA:
mongod --configsvr --dbpath /data/config --port 20000 --logpath /data/config/config.log --logappend –fork
CentosB:
mongod --configsvr --dbpath /data/config --port 20000 --logpath /data/config/config.log --logappend --fork
CentosC:
mongod --configsvr --dbpath /data/config --port 20000 --logpath /data/config/config.log --logappend --fork
7. 启动路由服务
centosA:
mongos --configdb 10.255.4.200:20000,10.255.4.201:20000,10.255.4.202:20000 --port 30000 --chunkSize 1 --logpath /data/mongos.log --logappend --fork
centosB:
mongos --configdb 10.255.4.200:20000,10.255.4.201:20000,10.255.4.202:20000 --port 30000 --chunkSize 1 --logpath /data/mongos.log --logappend --fork
centosC:
mongos --configdb 10.255.4.200:20000,10.255.4.201:20000,10.255.4.202:20000 --port 30000 --chunkSize 1 --logpath /data/mongos.log --logappend --fork
8.添加分片复制集,激活数据库和表分片
use admin
db.runCommand({addshard:"shard1/10.255.4.200:27017,10.255.4.201:27017,10.255.4.202:27017"});
db.runCommand({addshard:"shard2/10.255.4.200:27018,10.255.4.201:27018,10.255.4.202:27018"});
db.runCommand({enablesharding:"test"})
db.runCommand({shardcollection:"test.users",key:{_id:1}})
9. 测试集群分片结果
use test
for(var i=1;i |
|