mongodb生产部署手册

2020-06-15 06:31:41 浏览数 (1)

mongodb生产部署文档,继上一篇mongodb-4.x shard cluster 搭建-复制集节点为单个节点-适合开发环境后。本文主要记录了生产环境mongodb-shard集群部署的步骤与方法,提供快速安全搭建生产集群的配置。本文使用的mongodb版本为4.2,部署环境为centos7。

mongodb集群架构图

配置所有集群的hostname与hosts文件

代码语言:javascript复制
vi /etc/hosts

hostname <hostname>

(可选)在一台机器上配置证书登录其它服务器

代码语言:javascript复制
ssh-keygen -t rsa
ssh-copy-id -i ~/.ssh/id_rsa.pub mongo1
ssh-copy-id -i ~/.ssh/id_rsa.pub mongo2
ssh-copy-id -i ~/.ssh/id_rsa.pub mongo3
ssh-copy-id -i ~/.ssh/id_rsa.pub mongo4
ssh-copy-id -i ~/.ssh/id_rsa.pub mongo5

创建mongodb用户

创建一个不可用于登录的mongo用户

代码语言:javascript复制
[root@mongo1 ~]# groupadd mongo
[root@mongo1 ~]# useradd -g mongo mongo -s /sbin/nologin

开放端口

按照配置文件开启对应端口,不要打开无用的端口

代码语言:javascript复制
firewall-cmd --zone=public --add-port=27100/tcp --permanent
firewall-cmd --zone=public --add-port=27200/tcp --permanent
firewall-cmd --zone=public --add-port=27001/tcp --permanent
firewall-cmd --zone=public --add-port=27002/tcp --permanent
firewall-cmd --zone=public --add-port=27003/tcp --permanent
firewall-cmd --zone=public --add-port=27004/tcp --permanent
firewall-cmd --zone=public --add-port=27005/tcp --permanent
firewall-cmd --reload
firewall-cmd --zone=public --list-ports

创建mongodb集群认证文件并分发

代码语言:javascript复制
openssl rand -base64 756 > /home/mongo/.mongoKeyFile
chmod 400 /home/mongo/.mongoKeyFile
chown mongo:mongo /home/mongo/.mongoKeyFile 

时钟同步服务

  1. 解压软件包

softpackage软件包下载,提取码请关注cosmozhu公众号后发送:347

代码语言:javascript复制
tar xvf softpackage.tar
  1. 安装配置ntpd服务
代码语言:javascript复制
cd softpackage
rpm -ivh ntpdate-4.2.6p5-29.el7.centos.x86_64.rpm
rpm -ivh autogen-libopts-5.18-5.el7.x86_64.rpm
rpm -ivh ntp-4.2.6p5-29.el7.centos.x86_64.rpm
systemctl start ntpd.service
systemctl enable ntpd.service

打开ntp服务的端口

代码语言:javascript复制
firewall-cmd --add-service=ntp --permanent
firewall-cmd --reload
firewall-cmd --zone=public --list-service
  1. 配置时钟服务器

选一台机器作为时钟服务器

代码语言:javascript复制
vi /etc/ntp.conf
restrict <ntpserver_ip> mask 255.255.255.0
server 127.127.1.0 prefer
#server 0.centos.pool.ntp.org iburst
#server 1.centos.pool.ntp.org iburst
#server 2.centos.pool.ntp.org iburst
#server 3.centos.pool.ntp.org iburst
systemctl restart ntpd
  1. 配置时钟客户端
代码语言:javascript复制
vi /etc/ntp.conf
restrict <ntpserver_ip>
server <ntpserver_ip>
#server 0.centos.pool.ntp.org iburst
#server 1.centos.pool.ntp.org iburst
#server 2.centos.pool.ntp.org iburst
#server 3.centos.pool.ntp.org iburst

systemctl restart ntpd
  1. 验证

配置好后并不会实时生效,需要等待几分钟

代码语言:javascript复制
[root@mongo3 ~]# ntpstat
synchronised to NTP server (client) at stratum 7
   time correct to within 15 ms
   polling server every 64 s

mongodb 安装

0. 机器设置

禁用透明页

代码语言:javascript复制
ssh mongo1 "echo 'never' > /sys/kernel/mm/transparent_hugepage/defrag"
ssh mongo2 "echo 'never' > /sys/kernel/mm/transparent_hugepage/defrag"
ssh mongo3 "echo 'never' > /sys/kernel/mm/transparent_hugepage/defrag"
ssh mongo4 "echo 'never' > /sys/kernel/mm/transparent_hugepage/defrag"
ssh mongo5 "echo 'never' > /sys/kernel/mm/transparent_hugepage/defrag"
ssh mongo1 "echo 'never' > /sys/kernel/mm/transparent_hugepage/enabled "
ssh mongo2 "echo 'never' > /sys/kernel/mm/transparent_hugepage/enabled "
ssh mongo3 "echo 'never' > /sys/kernel/mm/transparent_hugepage/enabled "
ssh mongo4 "echo 'never' > /sys/kernel/mm/transparent_hugepage/enabled "
ssh mongo5 "echo 'never' > /sys/kernel/mm/transparent_hugepage/enabled "

添加 /etc/security/limits.conf

代码语言:javascript复制
*                soft   fsize            unlimited
*                hard   fsize            unlimited
*                soft   cpu              unlimited
*                hard   cpu              unlimited
*                soft   as               unlimited
*                hard   as               unlimited
*                soft   memlock          unlimited
*                hard   memlock          unlimited
*                soft   nofile           64000
*                hard   nofile           64000
*                soft   nproc            64000
*                hard   nproc            64000

设置tcp_keepalive_time为300

代码语言:javascript复制
ssh mongo1 "sysctl -w net.ipv4.tcp_keepalive_time=300"
ssh mongo2 "sysctl -w net.ipv4.tcp_keepalive_time=300"
ssh mongo3 "sysctl -w net.ipv4.tcp_keepalive_time=300"
ssh mongo4 "sysctl -w net.ipv4.tcp_keepalive_time=300"
ssh mongo5 "sysctl -w net.ipv4.tcp_keepalive_time=300"

ssh mongo1 "echo "net.ipv4.tcp_keepalive_time = 300" >> /etc/sysctl.conf"
ssh mongo2 "echo "net.ipv4.tcp_keepalive_time = 300" >> /etc/sysctl.conf"
ssh mongo3 "echo "net.ipv4.tcp_keepalive_time = 300" >> /etc/sysctl.conf"
ssh mongo4 "echo "net.ipv4.tcp_keepalive_time = 300" >> /etc/sysctl.conf"
ssh mongo5 "echo "net.ipv4.tcp_keepalive_time = 300" >> /etc/sysctl.conf"
1. 解压mongodb
代码语言:javascript复制
tar xvzf mongodb-linux-x86_64-rhel70-4.2.7.tgz
2. 创建目录
代码语言:javascript复制
mkdir -p /home/mongo/cluster/data/conf
mkdir -p /home/mongo/cluster/data/shard1
mkdir -p /home/mongo/cluster/data/shard2
mkdir -p /home/mongo/cluster/data/shard3
mkdir -p /home/mongo/cluster/data/shard4
mkdir -p /home/mongo/cluster/data/shard5
mkdir -p /home/mongo/cluster/conf
mkdir -p /home/mongo/cluster/log
touch /home/mongo/cluster/log/config.log
touch /home/mongo/cluster/log/shard1.log
touch /home/mongo/cluster/log/shard2.log
touch /home/mongo/cluster/log/shard3.log
touch /home/mongo/cluster/log/shard4.log
touch /home/mongo/cluster/log/shard5.log
touch /home/mongo/cluster/log/mongos.log
chown -R mongo:mongo /home/mongo/*
3. 编写配置文件
  1. 配置节点配置文件 configure.conf
代码语言:javascript复制
systemLog:
   destination: file
   path: "/home/mongo/cluster/log/config.log"
   logAppend: true
storage:
   dbPath: "/home/mongo/cluster/data/conf"
processManagement:
   fork: true
net:
   bindIp: mongo1,mongo2,mongo3,mongo4.mongo5
   port: 27100
sharding:
   clusterRole: configsvr
replication:
   replSetName: configs

security:
   authorization: enabled
   keyFile: /home/mongo/.mongoKeyFile
  1. 数据节点配置文件 <shardx>.conf

按实际情况修改

代码语言:javascript复制
systemLog:
   destination: file
   path: "/home/mongo/cluster/log/<shardx>.log"
   logAppend: true
storage:
   dbPath: "/home/mongo/cluster/data/<shardx>"
   journal:
      enabled: true
   wiredTiger:
      engineConfig:
         cacheSizeGB: 3
processManagement:
   fork: true
net:
   bindIp: 0.0.0.0
   port: <port>
sharding:
   clusterRole: shardsvr
replication:
   replSetName: <shardx>-rs
security:
   authorization: enabled
   keyFile: /home/mongo/.mongoKeyFile
  1. mongos配置文件 mongos.conf
代码语言:javascript复制
systemLog:
  destination: file
  logAppend: true
  path: /home/mongo/cluster/log/mongos.log
processManagement:
  fork: true
#  pidFilePath: /var/log/nginx/mongodbmongos.pid

# network interfaces
net:
  port: 27200
  bindIp: 0.0.0.0
#监听的配置服务器,只能有1个或者3个 configs为配置服务器的副本集名字
sharding:
   configDB: configs/mongo1:27100,mongo3:27100,mongo5:27100
security:
   keyFile: /home/mongo/.mongoKeyFile
4. 启动配置服务
代码语言:javascript复制
ssh mongo1 "sudo -E -u mongo /opt/softpackage/mongodb-linux-x86_64-rhel70-4.2.7/bin/mongod -f /home/mongo/cluster/conf/configure.conf"
ssh mongo3 "sudo -E -u mongo /opt/softpackage/mongodb-linux-x86_64-rhel70-4.2.7/bin/mongod -f /home/mongo/cluster/conf/configure.conf"
ssh mongo5 "sudo -E -u mongo /opt/softpackage/mongodb-linux-x86_64-rhel70-4.2.7/bin/mongod -f /home/mongo/cluster/conf/configure.conf"

链接任意一各配置服务,制作配置集群

代码语言:javascript复制
config = {
    _id : "configs",
     members : [
         {_id : 0, host : "mongo1:27100" },
         {_id : 1, host : "mongo3:27100" },
         {_id : 2, host : "mongo5:27100" }
     ]
};
rs.initiate(config);
5. 启动数据节点与仲裁节点
代码语言:javascript复制
ssh mongo1 "sudo -E -u mongo /opt/softpackage/mongodb-linux-x86_64-rhel70-4.2.7/bin/mongod -f /home/mongo/cluster/conf/shard1.conf --wiredTigerCacheSizeGB 13"
ssh mongo1 "sudo -E -u mongo /opt/softpackage/mongodb-linux-x86_64-rhel70-4.2.7/bin/mongod -f /home/mongo/cluster/conf/shard2.conf --wiredTigerCacheSizeGB 13"
ssh mongo1 "sudo -E -u mongo /opt/softpackage/mongodb-linux-x86_64-rhel70-4.2.7/bin/mongod -f /home/mongo/cluster/conf/shard3.conf --wiredTigerCacheSizeGB 1"

ssh mongo2 "sudo -E -u mongo /opt/softpackage/mongodb-linux-x86_64-rhel70-4.2.7/bin/mongod -f /home/mongo/cluster/conf/shard2.conf --wiredTigerCacheSizeGB 1"
ssh mongo2 "sudo -E -u mongo /opt/softpackage/mongodb-linux-x86_64-rhel70-4.2.7/bin/mongod -f /home/mongo/cluster/conf/shard3.conf --wiredTigerCacheSizeGB 14"
ssh mongo2 "sudo -E -u mongo /opt/softpackage/mongodb-linux-x86_64-rhel70-4.2.7/bin/mongod -f /home/mongo/cluster/conf/shard4.conf --wiredTigerCacheSizeGB 14"

ssh mongo3 "sudo -E -u mongo /opt/softpackage/mongodb-linux-x86_64-rhel70-4.2.7/bin/mongod -f /home/mongo/cluster/conf/shard3.conf --wiredTigerCacheSizeGB 13"
ssh mongo3 "sudo -E -u mongo /opt/softpackage/mongodb-linux-x86_64-rhel70-4.2.7/bin/mongod -f /home/mongo/cluster/conf/shard4.conf --wiredTigerCacheSizeGB 13"
ssh mongo3 "sudo -E -u mongo /opt/softpackage/mongodb-linux-x86_64-rhel70-4.2.7/bin/mongod -f /home/mongo/cluster/conf/shard5.conf --wiredTigerCacheSizeGB 1"

ssh mongo4 "sudo -E -u mongo /opt/softpackage/mongodb-linux-x86_64-rhel70-4.2.7/bin/mongod -f /home/mongo/cluster/conf/shard1.conf --wiredTigerCacheSizeGB 14"
ssh mongo4 "sudo -E -u mongo /opt/softpackage/mongodb-linux-x86_64-rhel70-4.2.7/bin/mongod -f /home/mongo/cluster/conf/shard4.conf --wiredTigerCacheSizeGB 1"
ssh mongo4 "sudo -E -u mongo /opt/softpackage/mongodb-linux-x86_64-rhel70-4.2.7/bin/mongod -f /home/mongo/cluster/conf/shard5.conf --wiredTigerCacheSizeGB 14"

ssh mongo5 "sudo -E -u mongo /opt/softpackage/mongodb-linux-x86_64-rhel70-4.2.7/bin/mongod -f /home/mongo/cluster/conf/shard1.conf --wiredTigerCacheSizeGB 1"
ssh mongo5 "sudo -E -u mongo /opt/softpackage/mongodb-linux-x86_64-rhel70-4.2.7/bin/mongod -f /home/mongo/cluster/conf/shard2.conf --wiredTigerCacheSizeGB 13"
ssh mongo5 "sudo -E -u mongo /opt/softpackage/mongodb-linux-x86_64-rhel70-4.2.7/bin/mongod -f /home/mongo/cluster/conf/shard5.conf --wiredTigerCacheSizeGB 13"

链接任意一个shard1分片节点

代码语言:javascript复制
config = {
    _id : "shard1-rs",
     members : [
         {_id : 0, host : "mongo1:27001"},
         {_id : 1, host : "mongo4:27001",priority:10 },
         {_id : 2, host : "mongo5:27001",arbiterOnly:true }   
     ]
 }
rs.initiate(config);

链接任意一个shard2分片节点

代码语言:javascript复制
config = {
    _id : "shard2-rs",
     members : [
         {_id : 0, host : "mongo1:27002",priority:10},
         {_id : 1, host : "mongo2:27002",arbiterOnly:true },
         {_id : 2, host : "mongo5:27002"}
     ]
 }
rs.initiate(config);

链接任意一个shard3分片节点

代码语言:javascript复制
config = {
    _id : "shard3-rs",
     members : [
         {_id : 0, host : "mongo1:27003",arbiterOnly:true },
         {_id : 1, host : "mongo2:27003",priority:10},
         {_id : 2, host : "mongo3:27003"}
     ]
 }
rs.initiate(config);

链接任意一个shard4分片节点

代码语言:javascript复制
config = {
    _id : "shard4-rs",
     members : [
         {_id : 0, host : "mongo2:27004"},
         {_id : 1, host : "mongo3:27004",priority:10},
         {_id : 2, host : "mongo4:27004",arbiterOnly:true }
     ]
 }
rs.initiate(config);

链接任意一个shard5分片节点

代码语言:javascript复制
config = {
    _id : "shard5-rs",
     members : [
         {_id : 0, host : "mongo3:27005",arbiterOnly:true },
         {_id : 1, host : "mongo4:27005"},
         {_id : 2, host : "mongo5:27005",priority:10}
     ]
 }
rs.initiate(config);
6. 启动mongos
代码语言:javascript复制
ssh mongo1 "sudo -E -u mongo /opt/softpackage/mongodb-linux-x86_64-rhel70-4.2.7/bin/mongos -f /home/mongo/cluster/conf/mongos.conf"
ssh mongo2 "sudo -E -u mongo /opt/softpackage/mongodb-linux-x86_64-rhel70-4.2.7/bin/mongos -f /home/mongo/cluster/conf/mongos.conf"
ssh mongo3 "sudo -E -u mongo /opt/softpackage/mongodb-linux-x86_64-rhel70-4.2.7/bin/mongos -f /home/mongo/cluster/conf/mongos.conf"
ssh mongo4 "sudo -E -u mongo /opt/softpackage/mongodb-linux-x86_64-rhel70-4.2.7/bin/mongos -f /home/mongo/cluster/conf/mongos.conf"
ssh mongo5 "sudo -E -u mongo /opt/softpackage/mongodb-linux-x86_64-rhel70-4.2.7/bin/mongos -f /home/mongo/cluster/conf/mongos.conf"
7. 添加分片
代码语言:javascript复制
sh.addShard("shard1-rs/mongo1:27001,mongo4:27001,mongo5:27001")
sh.addShard("shard2-rs/mongo1:27002,mongo2:27002,mongo5:27002")
sh.addShard("shard3-rs/mongo1:27003,mongo2:27003,mongo3:27003")
sh.addShard("shard4-rs/mongo2:27004,mongo3:27004,mongo4:27004")
sh.addShard("shard5-rs/mongo3:27005,mongo4:27005,mongo5:27005")

作者:cosmozhu --90后的老父亲,专注于保护地球的程序员

个人网站:https://www.cosmozhu.fun

欢迎转载,转载时请注明出处。

相关文章

  • mongodb:实时数据同步(一)
  • MongoDB-4.0 使用注意事项
  • MongoDB-4.0 生产部署建议
  • mongodb-4.x复制集数据同步(replica-set-sync)
  • MongoDB-4.x 关于shard在启动时报错 Got signal: 6 (Aborted).

0 人点赞