MongoDB副本群集
副本群集就是用多台机器进行数据的同步异步,然后让多台机器有一个数据的多个副本,在当主库挂掉的时候在不需要用户手动配置的情况下自动
切换其他备份节点做主库。而且还可以利用副本服务器做只读服务,实现读写分离,提高负载均衡
MongoDB群集节点类型
1、主节点:简称primary节点响应客户端或者应用程序请求,数据操作主要连接点,可读写
2、备份节点:简称secondary节点,监听主节点状态,复制主节点数据到本地 可读和选举
3、仲裁节点:Arbiter,不保留任何数据的备份,只具有投票选举的作用 也能将仲裁节点维护为副本群集的一部分,是从节点同时也可以是仲裁节点
模型结构
192.168.100.10:27017 | 主节点 |
192.168.100.20:27107 | 从节点1 |
192.168.100.30:27017 | 从节点2 |
192.168.100.40:27017 | 仲裁节点 |
主节点配置文件
[root@centos01 ~]# vim /usr/local/mongodb/conf/mongodb.conf
systemLog:
destination: file
path: /usr/local/mongodb/log/mongodb.log 设置日志文件的存放目录及其日志文件名
logAppend: true 使用追加的方式写日志
storage:
journal:
enabled: true
dbPath: /usr/local/mongodb/data
processManagement:
fork: true
net:
bindIp: 192.168.100.10,127.0.0.1 服务器IP地址
port: 27017 端口号
replication:
oplogSizeMB: 2048 日志大小2GB
replSetName: my_repl 设置副本群集名称
使用 rs.initiate() 方法对副本集进行初始化
> use admin;
switched to db admin
> config=
... {_id:'my_repl',members:[ 配置群集环境变量名字config
... {_id:0,host:'192.168.100.10:27017'}, 复制群集名字my_repl和主配置文件一致
... {_id:1,host:'192.168.100.20:27017'},
... {_id:2,host:'192.168.100.30:27017'}]
... }
my_repl:PRIMARY> rs.addArb("192.168.100.40:27017") 添加192.168.100.40为仲裁节点
my_repl:PRIMARY> rs.remove("192.168.100.40:27017") 移除节点
my_repl:PRIMARY> rs.status() 查看节点状态
"members" : [
{
"_id" : 0,
"name" : "192.168.100.10:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY", stateStr 为节点类型:PRIMARY 主节点,SECONDARY从节点,ARBITER 仲裁节点
"uptime" : 176781,
"optime" : {
"ts" : Timestamp(1688886258, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2023-07-09T07:04:18Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"electionTime" : Timestamp(1688884010, 1),
"electionDate" : ISODate("2023-07-09T06:26:50Z"),
"configVersion" : 6,
"self" : true,
"lastHeartbeatMessage" : ""
在主节点插入数据
my_repl:PRIMARY> use aaa
switched to db aaa
my_repl:PRIMARY> db.aaa.insert({id:1})
WriteResult({ "nInserted" : 1 })
从节点查询数据报错
my_repl:SECONDARY> use aaa
switched to db aaa
my_repl:SECONDARY> db.aaa.find()
Error: error: {
"operationTime" : Timestamp(1688886488, 1),
"ok" : 0,
"errmsg" : "not master and slaveOk=false",
"code" : 13435,
"codeName" : "NotPrimaryNoSecondaryOk",
"$clusterTime" : {
"clusterTime" : Timestamp(1688886488, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
因为默认情况下,从节点是没有读写权限的,可以增加读的权限
my_repl:SECONDARY> rs.slaveOk()
WARNING: slaveOk() is deprecated and may be removed in the next major release. Please use secondaryOk() instead.
MongoDB副本群集选举条件
MongoDB在副本群集中,会自动进行主节点的选举,主节点选举需要的条件
1. 主节点故障
2. 主节点网络不可达(默认心跳信息为10秒)
3. 人工干预(rs.stepDown(600))
修改节点优先级让
my_repl:PRIMARY> sjr=rs.config() 定义变量名字
my_repl:PRIMARY> sjr.members[2].priority=0 修改优先级(ID号默认从0开始)
my_repl:PRIMARY> sjr.members[2].votes=1 投票
my_repl:PRIMARY> sjr.members[2].slaveDelay=60 延迟复制60秒
my_repl:PRIMARY> sjr.members[2].hidden=true 投票
my_repl:PRIMARY> rs.reconfig(sjr) 更新配置
my_repl:PRIMARY> rs.stepDown()
MongoDB分片群集
Mongos:路由节点,访问MongoDB群集的入口,将用户读取请求转发到指定的分片复制群集,合并多个MongoDB复制群集节点的数据
Config Server:配置节点,提供群集数据元存储,帮助客户端定位分片群集所在的MongoDB节点
shards数据节点:存储分片后的数据,shards节点需要配置复制群集避免单节点故障数据丢失,分片不能超过1024个,一个分片数据不超过2TB
模型结构
修改配置节点的配置文件
[root@centos01 ~]# vim /usr/local/mongodb/conf/mongodb.conf
systemLog:
destination: file
path: /usr/local/mongodb/log/mongodb.log
logAppend: true
storage:
journal:
enabled: true
dbPath: /usr/local/mongodb/data
directoryPerDB: true
#engine: wiredTiger
wiredTiger:
engineConfig:
cacheSizeGB: 1
directoryForIndexes: true
collectionConfig:
blockCompressor: zlib
indexConfig:
prefixCompression: true
net:
bindIp: 192.168.100.10,127.0.0.1
port: 27017
replication:
oplogSizeMB: 2048
replSetName: configReplset
sharding:
clusterRole: configsvr
processManagement:
fork: true
修改shard1分片配置文件
[root@centos05 ~]# vim /usr//local/mongodb/conf/mongo.conf
systemLog:
destination: file
path: /usr/local/mongodb/log/mongodb.log
logAppend: true
storage:
journal:
enabled: true
dbPath: /usr/local/mongodb/data
directoryPerDB: true
wiredTiger:
engineConfig:
cacheSizeGB: 1
directoryForIndexes: true
collectionConfig:
blockCompressor: zlib
indexConfig:
prefixCompression: true
net:
bindIp: 192.168.100.50,127.0.0.1
port: 27017
replication:
oplogSizeMB: 2048
replSetName: sh1
sharding:
clusterRole: shardsvr
processManagement:
fork: true
修改shard2分片配置文件
[root@centos08 ~]# vim /usr/local/mongodb/conf/mongo.conf
systemLog:
destination: file
path: /usr/local/mongodb/log/mongodb.log
logAppend: true
storage:
journal:
enabled: true
dbPath: /usr/local/mongodb/data
directoryPerDB: true
#engine: wiredTiger
wiredTiger:
engineConfig:
cacheSizeGB: 1
directoryForIndexes: true
collectionConfig:
blockCompressor: zlib
indexConfig:
prefixCompression: true
net:
bindIp: 192.168.100.80,127.0.0.1
port: 27017
replication:
oplogSizeMB: 2048
replSetName: sh2
sharding:
clusterRole: shardsvr
processManagement:
fork: true
修改mongos配置文件
[root@centos04 ~]# vim /usr/local/mongodb/conf/mongos.conf
systemLog:
destination: file
path: /usr/local/mongodb/log/mongos.log
logAppend: true
net:
bindIp: 192.168.100.40,127.0.0.1
port: 27017
sharding:
configDB: configReplset/192.168.100.10:27017,192.168.100.20:27017,192.168.100.30:27017
processManagement:
fork: true
启动初始化config节点
> use admin
> config={_id: 'configReplset',members:[
... {_id:0,host:'192.168.100.10:27017'},
... {_id:1,host:'192.168.100.20:27017'},
... {_id:2,host:'192.168.100.30:27017'}]
... }
>rs.initiate()
启动并初始化shard1分片
> use admin
> config={_id: 'sh1',members:[
... {_id:0,host:'192.168.100.50:27017'},
... {_id:1,host:'192.168.100.60:27017'},
... {_id:2,host:'192.168.100.70:27017'}]
... }
>rs.initiate()
启动并初始化shard2分片
> use admin
> config={_id: 'sh2',members:[
... {_id:0,host:'192.168.100.80:27017'},
... {_id:1,host:'192.168.100.90:27017'},
... {_id:2,host:'192.168.100.100:27017'}]
... }
>rs.initiate()
启动并初始化mongos
>mongos db.runCommand({addshard:"sh1/192.168.100.50:27017,192.168.100.60:27017,192.168.100.70:27017",name:"shard1"}) 将shard1加入mongos
>mongos db.runCommand({addshard:"sh2/192.168.100.80:27017,192.168.100.90:27017,192.168.100.100:27017",name:"shard2"})将shard2加入mongos
为数据库启用分片功能
[root@centos04 ~]# mongo 192.168.100.40:27017/admin 登录mongos
mongos> use test 创建数据库
mongos> db.st.ensureIndex({id:1}) 对test库的st集合的id列开启分片
mongos> use admin 切换到admin库开启分片
mongos> db.runCommand({enablesharding:"test"})
mongos> db.runCommand({shardcollection:"test.st",key:{id:1}})
插入测试数据并查看
mongos> use test 进入test库插入数据
mongos> for(i=1;i<50000;i++){db.st.insert({"id":i,"name":"sjr","age":70,"date":new Date()});}
sh1:PRIMARY> use test
switched to db test
sh1:PRIMARY> db.st.count()
49999
sh1:SECONDARY> use test
switched to db test
sh1:SECONDARY> db.st.count()
49999