MongoDB 高可用集群副本集+分片搭建

Posted davie_2020

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MongoDB 高可用集群副本集+分片搭建相关的知识,希望对你有一定的参考价值。

MongoDB 高可用集群搭建

一、架构概况


192.168.150.129
192.168.150.130
192.168.150.131

参考文档:https://www.cnblogs.com/vadim/p/7100683.html

 

mongos           mongos             mongos
Config   server          Config server         Config server
Shared1 server 1      Shared1 server 1 副本    Shared1 server 1 仲裁/隐藏
Shared2 server 2      Shared2 server 2 副本    Shared2 server 2 仲裁/隐藏
Shared3 server 3      Shared3 server 3 副本    Shared3 server 3 仲裁/隐藏

  规划5个组件对应的端口号,由于每台机器均需要同时部署 mongos、config server 、shard1、shard2、shard3,所以需要用端口进行区分。端口可以自由定义,本架构中 

mongos : 20000
config server : 21000
shard11 : 22001
shard2 : 22002
shard3 : 22003


二、 系统参数配置

echo "never" > /sys/kernel/mm/transparent_hugepage/enabled
echo "never" > /sys/kernel/mm/transparent_hugepage/defrag

cat >> /etc/rc.local <<"EOF"
echo "never" > /sys/kernel/mm/transparent_hugepage/enabled
echo "never" > /sys/kernel/mm/transparent_hugepage/defrag
EOF

 

三、 下载解压mongodb

cd /opt/
wget http://downloads.mongodb.org/linux/mongodb-linux-x86_64-rhel62-v3.4-latest.tgz
# 注意系统时间同步
/usr/sbin/ntpdate ntp1.aliyun.com
tar -zxf mongodb-linux-x86_64-rhel62-v3.4-latest.tgz
ln -s /opt/mongodb-linux-x86_64-rhel62-3.4.16-9-g2b19fb5e85/ mongodb
chown -R mongodb.mongodb /opt/mongodb

四、防火墙设置

iptables -I INPUT -s 192.168.150.129/32 -i eth0 -p tcp -m tcp --dport 20000 -j ACCEPT
iptables -I INPUT -s 192.168.150.130/32 -i eth0 -p tcp -m tcp --dport 20000 -j ACCEPT
iptables -I INPUT -s 192.168.150.131/32 -i eth0 -p tcp -m tcp --dport 20000 -j ACCEPT

iptables -I INPUT -s 192.168.150.129/32 -i eth0 -p tcp -m tcp --dport 21000 -j ACCEPT
iptables -I INPUT -s 192.168.150.130/32 -i eth0 -p tcp -m tcp --dport 21000 -j ACCEPT
iptables -I INPUT -s 192.168.150.131/32 -i eth0 -p tcp -m tcp --dport 21000 -j ACCEPT

iptables -I INPUT -s 192.168.150.129/32 -i eth0 -p tcp -m tcp --dport 22001 -j ACCEPT
iptables -I INPUT -s 192.168.150.130/32 -i eth0 -p tcp -m tcp --dport 22001 -j ACCEPT
iptables -I INPUT -s 192.168.150.131/32 -i eth0 -p tcp -m tcp --dport 22001 -j ACCEPT

iptables -I INPUT -s 192.168.150.129/32 -i eth0 -p tcp -m tcp --dport 22002 -j ACCEPT
iptables -I INPUT -s 192.168.150.130/32 -i eth0 -p tcp -m tcp --dport 22002 -j ACCEPT
iptables -I INPUT -s 192.168.150.131/32 -i eth0 -p tcp -m tcp --dport 22002 -j ACCEPT

iptables -I INPUT -s 192.168.150.129/32 -i eth0 -p tcp -m tcp --dport 22003 -j ACCEPT
iptables -I INPUT -s 192.168.150.130/32 -i eth0 -p tcp -m tcp --dport 22003 -j ACCEPT
iptables -I INPUT -s 192.168.150.131/32 -i eth0 -p tcp -m tcp --dport 22003 -j ACCEPT

service iptables save
service iptables reload

五、创建相应的目录

#建立mongos及日志目录
mkdir -p /data/mongodb/mongos/log

#建立config server 数据文件存放目录
mkdir -p /data/mongodb/config/data
#建立config server 日志文件存放目录
mkdir -p /data/mongodb/config/log

#建立shard1 数据文件存放目录
mkdir -p /data/mongodb/shard1/data
#建立shard1 日志文件存放目录
mkdir -p /data/mongodb/shard1/log

#建立shard2 数据文件存放目录
mkdir -p /data/mongodb/shard2/data
#建立shard2 日志文件存放目录
mkdir -p /data/mongodb/shard2/log

#建立shard3 数据文件存放目录
mkdir -p /data/mongodb/shard3/data
#建立shard3 日志文件存放目录
mkdir -p /data/mongodb/shard3/log

# 创建用户
useradd mongodb
echo "mongodb" | passwd --stdin mongodb

# 授权
chown -R mongodb.mongodb /data/mongodb

六、Config server配置

su - mongodb
cat >/data/mongodb/config/mongo.conf <<"EOF"
dbpath=/data/mongodb/config/data/
logpath=/data/mongodb/config/log/config.log
logappend=true
port=21000
fork=true
rest=false
httpinterface=true
configsvr=true
replSet=cfgReplSet
EOF


# 启动方式
numactl --interleave=all /opt/mongodb/bin/mongod -f /data/mongodb/config/mongo.conf

# 查看日志
cat /data/mongodb/config/log/config.log

# 连接到任意一台配置服务器上
numactl --interleave=all /opt/mongodb/bin/mongo --host 192.168.150.129 --port 21000
创建配置服务器副本集
rs.initiate({_id:"cfgReplSet",configsvr:true,members:[{_id:0,host:"192.168.150.129:21000"},{_id:1,host:"192.168.150.130:21000"},{_id:2,host:"192.168.150.131:21000"}]})

七、Shard server配置

# 需要在 su - mongodb
# shard1
cat > /data/mongodb/shard1/mongo.conf <<"EOF"
dbpath=/data/mongodb/shard1/data
logpath=/data/mongodb/shard1/log/shard1.log
logappend=true
port=22001
fork=true
rest=false
httpinterface=true
replSet=shard1ReplSet
shardsvr=true
journal=false
EOF

# 启动方式
numactl --interleave=all /opt/mongodb/bin/mongod -f /data/mongodb/shard1/mongo.conf

# 查看日志
cat /data/mongodb/shard1/log/shard1.log

# 连接任意一台分片服务器
/opt/mongodb/bin/mongo --host 192.168.150.129 --port 22001
# 创建副本集并初始化
rs.initiate({_id:"shard1ReplSet",members:[{_id:0,host:"192.168.150.129:22001"},{_id:1,host:"192.168.150.130:22001"},{_id:2,host:"192.168.150.131:22001"}]})


# shard2
# 需要在 su - mongodb
cat > /data/mongodb/shard2/mongo.conf <<"EOF"
dbpath=/data/mongodb/shard2/data
logpath=/data/mongodb/shard2/log/shard2.log
logappend=true
port=22002
fork=true
rest=false
httpinterface=true
replSet=shard2ReplSet
shardsvr=true
journal=false
EOF

# 启动方式
numactl --interleave=all /opt/mongodb/bin/mongod -f /data/mongodb/shard2/mongo.conf

# 查看日志
cat /data/mongodb/shard2/log/shard2.log

# 连接任意一台分片服务器
/opt/mongodb/bin/mongo --host 192.168.150.129 --port 22002
# 创建副本集并初始化
rs.initiate({_id:"shard2ReplSet",members:[{_id:0,host:"192.168.150.129:22002"},{_id:1,host:"192.168.150.130:22002"},{_id:2,host:"192.168.150.131:22002"}]})


# shard3
# 需要在 su - mongodb
cat > /data/mongodb/shard3/mongo.conf <<"EOF"
dbpath=/data/mongodb/shard3/data
logpath=/data/mongodb/shard3/log/shard3.log
logappend=true
port=22003
fork=true
rest=false
httpinterface=true
replSet=shard3ReplSet
shardsvr=true
journal=false
EOF

# 启动方式
numactl --interleave=all /opt/mongodb/bin/mongod -f /data/mongodb/shard3/mongo.conf

# 查看日志
cat /data/mongodb/shard3/log/shard3.log

# 连接任意一台分片服务器
/opt/mongodb/bin/mongo --host 192.168.150.129 --port 22003
# 创建副本集并初始化
rs.initiate({_id:"shard3ReplSet",members:[{_id:0,host:"192.168.150.129:22003"},{_id:1,host:"192.168.150.130:22003"},{_id:2,host:"192.168.150.131:22003"}]})

八、Mongos配置

# 需要在 su - mongodb
cat >/data/mongodb/mongos/mongo.conf <<"EOF"
logpath=/data/mongodb/mongos/log/mongos.log
logappend=true
port=20000
fork=true
configdb=cfgReplSet/192.168.150.129:21000,192.168.150.130:21000,192.168.150.131:21000
EOF

# 启动服务
numactl --interleave=all /opt/mongodb/bin/mongos -f /data/mongodb/mongos/mongo.conf

# 查看日志
cat /data/mongodb/mongos/log/mongos.log

九、添加分片到集群

# 登录路由服务客户端
/opt/mongodb/bin/mongo --host 192.168.150.129 --port 20000
#添加分片到集群
mongos> sh.addShard("shard1ReplSet/192.168.150.129:22001,192.168.150.130:22001,192.168.150.131:22001")
# 依次添加shard2 shard3
mongos> sh.addShard("shard2ReplSet/192.168.150.129:22002,192.168.150.130:22002,192.168.150.131:22002")
mongos> sh.addShard("shard3ReplSet/192.168.150.129:22003,192.168.150.130:22003,192.168.150.131:22003")

# Enable Sharding for a Database
sh.enableSharding("test")

# Shard a Collection
sh.shardCollection("test.testdoc", { id: 1})

十、测试

/opt/mongodb/bin/mongo --host 192.168.150.129 --port 20000
# 插入测试数据
mongos> use test
for(var i = 1; i <= 100000; i++){
db.testdoc.save({id:i,"message":"message"+i});
}
mongos> db.testdoc.stats()

十一、快捷使用方式

# 快捷启动方式
cat >>/home/mongodb/.bashrc <<"EOF"
alias mongodb_shard1.start=\'numactl --interleave=all /opt/mongodb/bin/mongod -f /data/mongodb/shard1/mongo.conf\'
alias mongodb_shard2.start=\'numactl --interleave=all /opt/mongodb/bin/mongod -f /data/mongodb/shard2/mongo.conf\'
alias mongodb_shard3.start=\'numactl --interleave=all /opt/mongodb/bin/mongod -f /data/mongodb/shard3/mongo.conf\'
alias mongodb_config.start=\'numactl --interleave=all /opt/mongodb/bin/mongod -f /data/mongodb/config/mongo.conf\'
alias mongodb_mongos.start=\'numactl --interleave=all /opt/mongodb/bin/mongos -f /data/mongodb/mongos/mongo.conf\'
EOF
source /home/mongodb/.bash_profile

# 快捷登陆方式
# /opt/mongodb/bin/mongo --host 127.0.0.1 --port 22001 # shard1
# /opt/mongodb/bin/mongo --host 127.0.0.1 --port 22002 # shard1
# /opt/mongodb/bin/mongo --host 127.0.0.1 --port 22003 # shard1
# /opt/mongodb/bin/mongo --host 127.0.0.1 --port 21000 # config server
# /opt/mongodb/bin/mongo --host 127.0.0.1 --port 20000 # mongos

cat >>/home/mongodb/.bashrc <<"EOF"
alias mongodb_shard1.login=\'/opt/mongodb/bin/mongo --host 127.0.0.1 --port 22001\'
alias mongodb_shard2.login=\'/opt/mongodb/bin/mongo --host 127.0.0.1 --port 22002\'
alias mongodb_shard3.login=\'/opt/mongodb/bin/mongo --host 127.0.0.1 --port 22003\'
alias mongodb_config.login=\'/opt/mongodb/bin/mongo --host 127.0.0.1 --port 21000\'
alias mongodb_mongos.login=\'/opt/mongodb/bin/mongo --host 127.0.0.1 --port 20000\'
EOF
source /home/mongodb/.bash_profile

十二、副本集手工主从切换

# 切换主从方式一
config=rs.conf() //查看当前配置,存入config变量中。
config.members[2].priority = 3 //修改config变量,第三组成员的优先级为3.
rs.reconfig(config) //配置生效 
rs.conf() //查看当前配置
rs.status()

# 切换主从方式二
rs.stepDown()  

  # 服务异常重启,需要删除lock文件
  find /data/mongodb/*/ -name *.lock | xargs rm -f "{}" \\;

 

以上是关于MongoDB 高可用集群副本集+分片搭建的主要内容,如果未能解决你的问题,请参考以下文章

mongodb 3.4 集群搭建:分片+副本集

mongodb高可用集群部署

MongoDB分片集群搭建

mongdob-高可用集群

mongdob-高可用集群

MongoDB高可用复制集分片集群搭建