好记性不如烂笔头,作个记录以便日后参考
- 简介
- Replica Set(副本集): 有自动故障恢复功能主动集群,它相对于主从集群最主要的区别就是没有固定主节点,当主节点故障时集群会自动选举出主节点。Rplica Set总有一个主节点(Primary)和一个或者多个从节点(Secondary)组成。Replica Set 通过集群中数据异步复制达到故障转移实现数据冗余,多台服务器同一时时刻只有Primary提供写操作;并把读操作分发给slave,从而保障数据的一致性。
- Sharding(分片):分片即对数据进行拆分,将其分散存储放在不同的服务器上。MongoDB支持自动分片。
- 目标:
- 通过副本集达到数据的高可用
- 通过Sharding提高数据写入性能
- 通过keepalived 实现VIP达到服务的高可用
服务器环境:
- CentOS 7.4
- mongodb01 172.30.0.81
- mongodb02 172.30.0.82
- mongodb03 172.30.0.83
VIP: 172.30.0.80
软件:
- MongoDB 3.6.4
- keepalived
软件安装:
- 增加MongoDB yum库安装,MongoDB和keepalived
1
2
3
4
5
6
7
8
9
10vi /etc/yum.repos.d/mongodb-3.6.repo
[mongodb-org-3.6]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.6/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.6.asc
yum clean expire-cache
yum install -y mongodb-org-3.6.4 mongodb-org-server-3.6.4 mongodb-org-shell-3.6.4 mongodb-org-mongos-3.6.4 mongodb-org-tools-3.6.4
yum install epel-release -y && yum install keepalived -y
配置Replica Set(副本集)
- mongodb81
- mkdir -p /data/replica1_1
- mkdir -p /data/replica2_1
- 创建keyfile
- openssl rand -base64 > /data/keyfile
- 复制keyfile到82、83的/data下
- mongodb82
- mkdir -p /data/replica1_2
- mkdir -p /data/replica2_2
- mongodb83
- mkdir -p /data/replica1_3
mkdir -p /data/replica2_3
修改配置文件
mongod.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
# Where and how to store data.
storage:
dbPath: /data/replica1_1
journal:
enabled: true
directoryPerDB: true
# how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /var/run/mongodb/mongod.pid # location of pidfile
timeZoneInfo: /usr/share/zoneinfo
# network interfaces
net:
port: 20000
bindIp: 0.0.0.0 # Listen to local interface only, comment to listen on all interfaces.
security:
authorization: enabled
keyFile: /data/keyfile
#operationProfiling:
replication:
replSetName: shard1
sharding:
clusterRole: shardsvr82、83更改存储路径即可
- 3台都执行 systemctl enable mongod && systemctl start mongod
- mongod2.conf
- 修改81、82、83端口为20001和路径
- 3台都执行 systemctl enable mongod2 && systemctl start mongod2
注意在增加用户密码前先不开启验证 authorization: disabled
- 初始化副本集,默认priority为1,设置81 priority为5,优先为primary
1 |
|
config server配置服务器
- 在81、82、83上增加以下配置
1 | mkdir /data/config && chown -R mongod.mongod /data/config |
mongos server配置服务器
- 在81、82、83上增加以下配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39vi /etc/mongos.conf
# mongos.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: /var/log/mongodb/mongos.log
# how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /var/run/mongodb/mongos.pid # location of pidfile
# network interfaces
net:
port: 40000
bindIp: 0.0.0.0 # Listen to local interface only, comment to listen on all interfaces.
security:
keyFile: /data/keyfile
clusterAuthMode: keyFile
#configure for mongos server
sharding:
configDB: configd/172.30.0.81:30000,172.30.0.82:30000,172.30.0.83:30000
systemctl enable mongos && systemctl start mongos
# 登陆mongos增加分片
mongo 172.30.0.80:40000/admin
sh.addShard("shard1/172.30.0.81:20000"),172.30.0.82:20000,172.30.0.83:20000")
sh.addShard("shard2/172.30.0.81:20001"),172.30.0.82:20001,172.30.0.83:20001")
# 查看状态
sh.status()
配置keepalived
1 | 脚本检测mongos如果进程停止则切换VIP |
相关配置
- WARNING: /sys/kernel/mm/transparent_hugepage/enabled is ‘always’.We suggest setting it to ‘never’
WARNING: /sys/kernel/mm/transparent_hugepage/defrag is ‘always’.We suggest setting it to ‘never’
增加以下内容 /etc/rc.local
- echo “never” > /sys/kernel/mm/transparent_hugepage/enabled
- echo “never” > /sys/kernel/mm/transparent_hugepage/defrag
修改文件描述符和进程配置
1
2
3
4
5
6vim /etc/security/limits.conf
mongod soft nofile 64000
mongod hard nofile 64000
mongod soft nproc 32000
mongod hard nproc 32000
systemctl restart mongod
最后更新: 2023年08月27日 03:06
原始链接: https://blog.icanwen.com/2018/05/23/MongoDB-replicaset-sharding/