mongodb3.6.13集群_三分片副本集安装过程记录【使用非root用户启停】

环境准备以及配置文件
准备三台服务器,mongos、config server、副本集如下
10.242.210.175        10.242.210.176        10.242.210.177
(mongos)27017        (mongos)27017        (mongos)27017
config server(1004)        config server(1004)        config server(1004)
shard1(10001)主        shard1(10001)副        shard1(10001)仲裁
shard1(10002)仲裁        shard1(10002)主        shard1(10002)副
shard1(10003)副        shard1(10003)仲裁        shard1(10003)主
1,系统配置修改
Linux操作系统参数
系统全局允许分配的最大文件句柄数:
sysctl -w fs.file-max=2097152
sysctl -w fs.nr_open=2097152
echo 2097152 > /proc/sys/fs/nr_open
允许当前会话/进程打开文件句柄数:
ulimit -n 1048576
修改 ‘fs.file-max’ 设置到 /etc/sysctl.conf 文件:
fs.file-max = 1048576
修改/etc/security/limits.conf 持久化设置允许用户/进程打开文件句柄数
* soft nofile 1048576
* hard nofile 1048576
* soft nproc 524288
* hard nproc 524288
TCP 协议栈网络参数
并发连接 backlog 设置:
sysctl -w net.core.somaxconn=32768
sysctl -w net.ipv4.tcp_max_syn_backlog=16384
sysctl -w net.core.netdev_max_backlog=16384
可用知名端口范围:
sysctl -w net.ipv4.ip_local_port_range=80 65535'
TCP Socket 读写 Buffer 设置:
sysctl -w net.core.rmem_default=262144
sysctl -w net.core.wmem_default=262144
sysctl -w net.core.rmem_max=16777216
sysctl -w net.core.wmem_max=16777216
sysctl -w net.core.optmem_max=16777216
sysctl -w net.ipv4.tcp_rmem='1024 4096 16777216'
sysctl -w net.ipv4.tcp_wmem='1024 4096 16777216'
TCP 连接追踪设置(Centos7以下才有,以上版本则不用):
sysctl -w net.nf_conntrack_max=1000000
sysctl -w net.netfilter.nf_conntrack_max=1000000
sysctl -w net.netfilter.nf_conntrack_tcp_timeout_time_wait=30

2,下载配置文件以及目录
三台服务器分别,下载mongodb-linux-x86_64-rhel62-3三台服务器分别,下载mongodb-linux-x86_64-rhel62-3.6.13 版本安装文件,
然后解压到/opt/mongodb/mongodb-linux-x86_64-rhel62-3目录
#解压
tar -xzvf mongodb-linux-x86_64-3.4.6.tgz
创建配置文件目录
/opt/mongodb/mongodb-linux-x86_64-rhel62-3.6.13/etc
配置文件
2.1 config.conf
pidfilepath = /opt/mongodb/data/config/logs/configsrv.pid
dbpath = /opt/mongodb/data/config/db/
logpath = /opt/mongodb/data/config/logs/congigsrv.log
logappend = true
bind_ip = 0.0.0.0
port = 10004
fork = true
#declare this is a config db of a cluster;
configsvr = true
#replSetname
replSet = configs
maxConns = 2000
#auth=true

2.2 mongos.conf
pidfilepath = /opt/mongodb/data/mongos/logs/mongos.pid
logpath = /opt/mongodb/data/mongos/logs/mongos.log
logappend = true
bind_ip = 0.0.0.0
port = 27017
fork = true
#config servers
configdb = configs/10.242.210.175:10004,10.242.210.176:10004,10.242.210.177:10004
maxConns = 2000
#auth=true
#keyFile=/usr/local/mongodb3.6/keyfile

2.3 shard1.conf
pidfilepath = /opt/mongodb/data/shard1/logs/shard1.pid
dbpath = /opt/mongodb/data/shard1/db
logpath=/opt/mongodb/data/shard1/logs/shard1.log
logappend = true
bind_ip = 0.0.0.0
port = 10001
fork = true
#replSet name
replSet = shard1
#declare this is a shard db of a cluster
shardsvr = true
maxConns = 2000
#cache size
wiredTigerCacheSizeGB = 4
#auth=true

2.4 shard2.conf
pidfilepath = /opt/mongodb/data/shard2/logs/shard2.pid
dbpath = /opt/mongodb/data/shard2/db
logpath=/opt/mongodb/data/shard2/logs/shard2.log
logappend = true
bind_ip = 0.0.0.0
port = 10002
fork = true
#replSet name
replSet = shard2
#declare this is a shard db of a cluster;
shardsvr = true
maxConns = 2000
#cache size
wiredTigerCacheSizeGB = 4
#auth=true

2.5 shard3.conf
pidfilepath = /opt/mongodb/data/shard3/logs/shard3.pid
dbpath = /opt/mongodb/data/shard3/db
logpath=/opt/mongodb/data/shard3/logs/shard3.log
logappend = true
bind_ip = 0.0.0.0
port = 10003
fork = true
#replSet name
replSet = shard3
#declare this is a shard db of a cluster;
shardsvr = true
maxConns = 2000
#cache size
wiredTigerCacheSizeGB = 4
#auth=true

注意wiredTigerCacheSizeGB = 4 的大小一般建议设置机器内存的一半

继续创建目录
/opt/mongodb/data
/opt/mongodb/data/config/db
/opt/mongodb/data/config/logs
/opt/mongodb/data/mongos/db
/opt/mongodb/data/mongos/logs
/opt/mongodb/data/shard1/db
/opt/mongodb/data/shard1/logs
/opt/mongodb/data/shard2/db
/opt/mongodb/data/shard2/logs
/opt/mongodb/data/shard3/db
/opt/mongodb/data/shard3/logs

启动mongodb集群
先启动配置服务器和分片服务器,后启动路由实例(三台服务器)
一、启动配置服务器
启动三台服务器的config server
cd /opt/mongodb/mongodb-linux-x86_64-rhel62-3.6.13/bin/
./mongod -f /opt/mongodb/mongodb-linux-x86_64-rhel62-3.6.13/etc/config.conf

登录任意一台服务器,初始化配置副本集
登录连接命令:
./mongo 10.242.210.175:10004/admin
配置如下内容:
config = {
...    _id : "configs",
...     members : [
...         {_id : 0, host : "10.242.210.175:10004" },
...         {_id : 1, host : "10.242.210.176:10004" },
...         {_id : 2, host : "10.242.210.177:10004" }
...     ]
... }


初始化副本集
> rs.initiate(config)
其中,“_id” : “configs” 应与配置文件中配置的replSet一致,“members”中的“host”为三个节点的ip和port


二、启动分片服务器
l  启动三台服务器的shard1 server
cd /opt/mongodb/mongodb-linux-x86_64-rhel62-3.6.13/bin/
./mongod -f /opt/mongodb/mongodb-linux-x86_64-rhel62-3.6.13/etc/shard1.conf

登录10.242.210.175一台服务器(10.242.210.177设置为仲裁节点,不能使用该节点登录),初始化分片副本集

登录连接命令:./mongo 10.242.210.175:10001/admin

config = {
...      _id : "shard1",
...      members : [
...         {_id : 0, host : "10.242.210.175:10001",priority:2},
...         {_id : 1, host : "10.242.210.176:10001",priority:1},
...         {_id : 2, host : "10.242.210.177:10001", arbiterOnly : true}
...      ]
... }

> rs.initiate(config)
第三个节点的“arbiterOnly”:true代表其为仲裁节点。
使用exit命令退出mongo的shell操作界面

2   启动三台服务器的shard2 server
cd /opt/mongodb/mongodb-linux-x86_64-rhel62-3.6.13/bin/
./mongod -f /opt/mongodb/mongodb-linux-x86_64-rhel62-3.6.13/etc/shard2.conf

登录连接命令:./mongo 10.242.210.176:10002/admin  (因为10.242.210.175设置为仲裁节点,不能使用该节点登录)

config = {
...      _id : "shard2",
...      members : [
...         {_id : 0, host : "10.242.210.175:10002", arbiterOnly : true },
...         {_id : 1, host : "10.242.210.176:10002",priority:2},
...         {_id : 2, host : "10.242.210.177:10002",priority:1}
...      ]
... }
初始化副本集
> rs.initiate(config)
第三个节点的“arbiterOnly”:true代表其为仲裁节点。
使用exit命令退出mongo的shell操作界面

3   启动三台服务器的shard3 server
cd /opt/mongodb/mongodb-linux-x86_64-rhel62-3.6.13/bin/
./mongod -f /opt/mongodb/mongodb-linux-x86_64-rhel62-3.6.13/etc/shard3.conf

登录连接命令:./mongo 10.242.210.175:10003/admin (因为10.242.210.176设置为仲裁节点,不能使用该节点登录)

config = {
...      _id : "shard3",
...      members : [
...         {_id : 0, host : "10.242.210.175:10003",priority:1},
...         {_id : 1, host : "10.242.210.176:10003", arbiterOnly : true },
...         {_id : 2, host : "10.242.210.177:10003",priority:2}
...      ]
... }
初始化副本集
> rs.initiate(config)
第三个节点的“arbiterOnly”:true代表其为仲裁节点。
使用exit命令退出mongo的shell操作界面

4   启动路由实例
启动三台服务器的mongos server,使用如下命令
cd /opt/mongodb/mongodb-linux-x86_64-rhel62-3.6.13/bin/
./mongos -f /opt/mongodb/mongodb-linux-x86_64-rhel62-3.6.13/etc/mongos.conf

5   启用分片
目前搭建了mongodb配置服务器、路由服务器、各个分片服务器,不过应用程序连接到mongos路由服务器并不能使用分片机制,还需要在程序里设置分片配置,让分片生效。
登录任意一台mongos,这里以10.242.210.175:27017
cd /opt/mongodb/mongodb-linux-x86_64-rhel62-3.6.13/bin/
登录连接命令:./mongo 10.242.210.175:27017/admin

配置如下内容,串联路由服务器与切片副本集:
sh.addShard("shard1/10.242.210.175:10001,10.242.210.176:10001,10.242.210.177:10001")
sh.addShard("shard2/10.242.210.175:10002,10.242.210.176:10002,10.242.210.177:10002")
sh.addShard("shard3/10.242.210.175:10003,10.242.210.176:10003,10.242.210.177:10003")
查看集群状态: 
sh.status()

6    指定数据库与集合分片生效
目前配置服务、路由服务、分片服务、副本集服务都已经串联起来了,但是我们的目的是希望插入数据、数据能够自动分片。
连接在mongos上,准备让指定的数据库、指定的集合分片生效。
接着上面5的步骤,不用退出mongos的操作界面
指定数据库分片生效:
db.runCommand({enablesharding : "testdb"})

指定数据库里需要分片的集合collection和片键,一般是_id:
db.runCommand({shardcollection : "testdb.table1", key : {id : "hashed"}})


7     测试分片配置结果
登录任意一台mongos,这里以10.242.210.175:27017为例:
登录连接命令:./mongo 10.242.210.175:27017/admin
切换数据库:
use testdb
输入如下命令:
for (var i = 1; i <= 1000; i++){ db.table1.insert({id:i,text:"hello world"}) }
查看分配状态:
db.table1.stats()

8. 后期运维
mongodbd的启动顺序是,先启动配置服务器,再启动分片,最后启动mongos。
cd /opt/mongodb/mongodb-linux-x86_64-rhel62-3.6.13/bin/
./mongod -f /opt/mongodb/mongodb-linux-x86_64-rhel62-3.6.13/etc/config.conf

./mongod -f /opt/mongodb/mongodb-linux-x86_64-rhel62-3.6.13/etc/shard1.conf

./mongod -f  /opt/mongodb/mongodb-linux-x86_64-rhel62-3.6.13/etc/shard2.conf

./mongod -f /opt/mongodb/mongodb-linux-x86_64-rhel62-3.6.13/etc/shard3.conf

./mongos -f /opt/mongodb/mongodb-linux-x86_64-rhel62-3.6.13/etc/mongos.conf

关闭时,直接killall杀掉所有进程

killall mongod
killall mongos


9.使用非root用户启停mongodb
创建mongo用户:
  useradd mongo
  passwd mongo
  
  一.现在root用户下执行以上mongodb的安装流程,然后给mongodb的安装目录和数据库目录指定mongo用户,再切换到mongo用户,执行数据库连接即可。
  chown -R mongo:mongo /opt/mongodb/data/
    chown -R mongo:mongo /opt/mongodb/mongodb-linux-x86_64-rhel62-3.6.13
    chown -R mongo:mongo /tmp
    二.从root切换到mongo身份,执行:su - mongo,然后执行以上mongodb的安装流程即可。

  如果mongodb服务启动失败,则我们可以查看/data/mongodb/log/mongodb.log日志信息,以找到错误原因来解决问题。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值