MongoDB 12节点分片集群安装搭建(3.2版)

2020-07-28 17:42发布

12节点分片集群安装搭建(3.2版)

 
主机 MongoDB01 MongoDB02 MongoDB03 MongoDB04 MongoDB05 MongoDB06
IP 10.10.1.5 10.10.1.6 10.10.1.7 10.10.1.8 10.10.1.9 10.10.1.10
副本集(20081)数据 SH1RS SH1RS SH1RS SH2RS SH2RS SH2RS
主机 MongoDB07 MongoDB08 MongoDB09 MongoDB10 MongoDB11 MongoDB12
IP 10.10.1.11 10.10.1.12 10.10.1.13 10.10.1.14 10.10.1.15 10.10.1.16
副本集(20081)数据 SH3RS SH3RS SH3RS SH4RS SH4RS SH4RS
副本集(20082)配置  CFRS  CFRS CFRS      
28003 路由        Y Y Y
 
 
前提是安装好操作系统(本案例基于rhel6.10),数据库使用的存储可使用(xfs)
一、针对集群的每个节点,都需要创建用户和安装软件的基本操作:
--创建用户和路径
root/root123
 
groupadd -g 1000 mongodb
useradd -u 1000 -g mongodb mongodb
passwd mongodb
密码:mongodb
mkdir /mongodb
chown mongodb:mongodb /mongodb
--修改原文件系统挂载到/mongodb
vi /etc/fstab
umount /data
mount -a
chown mongodb:mongodb /mongodb
 
--解压软件:
su - mongodb
cd /mongodb
mkdir data conf soft log
cd soft
mkdir 3.2.22
cd 3.2.22
--先行上传安装包到/mongodb目录
tar -xzvf /mongodb/mongodb-linux-x86_64-rhel62-3.2.22.tgz
cd mongodb-linux-x86_64-rhel62-3.2.22
mv * ../
cd ../
rm -r mongodb-linux-x86_64-rhel62-3.2.22
 
--添加环境变量
cd
vi .bash_profile
添加
export PATH=$PATH:/mongodb/soft/3.2.22/bin
source .bash_profile
 
--操作系统参数配置--
vi /etc/hosts
 
10.10.1.5 MongoDB01
10.10.1.6 MongoDB02
10.10.1.7 MongoDB03
10.10.1.8 MongoDB04
10.10.1.9 MongoDB05
10.10.1.10 MongoDB06
10.10.1.11 MongoDB07
10.10.1.12 MongoDB08
10.10.1.13 MongoDB09
10.10.1.14 MongoDB10
10.10.1.15 MongoDB11
10.10.1.16 MongoDB12
 
--关闭selinux
setenforce 0
getenforce
vi /etc/selinux/config
SELINUX=disabled
 
--关闭防火墙:
chkconfig iptables off
chkconfig ip6tables off
service iptables stop
service ip6tables stop
 
--修改系统参数:
vi /etc/sysctl.conf
 
fs.file-max = 6815744
kernel.sem = 10000 1280000 512 1024
kernel.shmmni = 4096
kernel.shmall = 1073741824
kernel.shmmax = 4398046511104
net.core.rmem_default = 262144
net.core.wmem_default = 262144
net.core.rmem_max = 4194304
net.core.wmem_max = 1048576
fs.aio-max-nr = 4194304
vm.dirty_ratio=20
vm.dirty_background_ratio=3
vm.dirty_writeback_centisecs=100
vm.dirty_expire_centisecs=500
vm.swappiness=10
vm.min_free_kbytes=524288
 
sysctl -p
 
--关闭透明大页:
vi /etc/rc.local
if test -f /sys/kernel/mm/transparent_hugepage/enabled; then
   echo never > /sys/kernel/mm/transparent_hugepage/enabled
fi
if test -f /sys/kernel/mm/transparent_hugepage/defrag; then
   echo never > /sys/kernel/mm/transparent_hugepage/defrag
fi
 
--设置mongodb用户的limits
vi /etc/security/limits.conf
mongodb soft nproc 4095
mongodb hard nproc 16384
mongodb soft nofile 8192
mongodb hard nofile 65536
mongodb soft stack 10240
mongodb hard stack 32768
 
vi /etc/security/limits.d/90-nproc.conf
添加
mongodb soft nproc unlimited
 
 
二、mongodb副本集配置(数据节点和配置节点):
su - mongodb
mkdir /mongodb/data/28001
mkdir /mongodb/conf/28001
mkdir /mongodb/log/28001
cd /mongodb/conf/28001
 
openssl rand -base64 102 > .keyFile
chmod 400 .keyFile
将文件拷到其它节点相同目录下
 
vi mongo_sh1rs_28001.conf            --不同sh的配置文件全称不同
 
port = 28001
dbpath = /mongodb/data/28001
logpath = /mongodb/log/28001
pidfilepath = /mongodb/data/28001/28001.pid
logappend = true
fork = true
oplogSize = 204800
replSet = SH1RS                             --其它对应为SH2RS/SH3RS/SH4RS
#keyFile = /mongodb/conf/28001/.keyFile
 
--启动mongod
numactl --interleave=all mongod -f /mongodb/conf/28001/mongo_sh1rs_28001.conf
 
--配置副本集
repConfig = {
              _id:'SH1RS', 
     members:[
         {_id:0,host:'MongoDB01:28001'},
      {_id:1,host:'MongoDB02:28001'},
      {_id:2,host:'MongoDB03:28001'}]
   }
 
rs.initiate(repConfig)
rs.status()
<wiz_code_mirror>
 
 
 
 
 
 
 
 
 
 
 
1
SH1RS:SECONDARY> rs.status()
2
{
3
        "set" : "SH1RS",
4
        "date" : ISODate("2019-03-14T14:12:17.210Z"),
5
        "myState" : 2,
6
        "term" : NumberLong(1),
7
        "syncingTo" : "MongoDB01:28001",
8
        "heartbeatIntervalMillis" : NumberLong(2000),
9
        "members" : [
10
                {
11
                        "_id" : 0,
12
                        "name" : "MongoDB01:28001",
13
                        "health" : 1,
14
                        "state" : 1,
15
                        "stateStr" : "PRIMARY",
16
                        "uptime" : 655,
17
                        "optime" : {
18
                                "ts" : Timestamp(1552572310, 2),
19
                                "t" : NumberLong(1)
20
                        },
21
                        "optimeDate" : ISODate("2019-03-14T14:05:10Z"),
22
                        "lastHeartbeat" : ISODate("2019-03-14T14:12:15.436Z"),
23
                        "lastHeartbeatRecv" : ISODate("2019-03-14T14:12:16.769Z"),
24
                        "pingMs" : NumberLong(0),
25
                        "electionTime" : Timestamp(1552572066, 1),
26
                        "electionDate" : ISODate("2019-03-14T14:01:06Z"),
27
                        "configVersion" : 1
28
                },
29
                {
30
                        "_id" : 1,
31
                        "name" : "MongoDB02:28001",
32
                        "health" : 1,
33
                        "state" : 2,
34
                        "stateStr" : "SECONDARY",
35
                        "uptime" : 655,
36
                        "optime" : {
37
                                "ts" : Timestamp(1552572310, 2),
38
                                "t" : NumberLong(1)
39
                        },
40
                        "optimeDate" : ISODate("2019-03-14T14:05:10Z"),
41
                        "lastHeartbeat" : ISODate("2019-03-14T14:12:15.436Z"),
42
                        "lastHeartbeatRecv" : ISODate("2019-03-14T14:12:15.436Z"),
43
                        "pingMs" : NumberLong(0),
44
                        "syncingTo" : "MongoDB01:28001",
45
                        "configVersion" : 1
46
                },
47
                {
48
                        "_id" : 2,
49
                        "name" : "MongoDB03:28001",
50
                        "health" : 1,
51
                        "state" : 2,
52
                        "stateStr" : "SECONDARY",
53
                        "uptime" : 1002,
54
                        "optime" : {
55
                                "ts" : Timestamp(1552572310, 2),
56
                                "t" : NumberLong(1)
57
                        },
58
                        "optimeDate" : ISODate("2019-03-14T14:05:10Z"),
59
                        "syncingTo" : "MongoDB01:28001",
60
                        "configVersion" : 1,
61
                        "self" : true
62
                }
63
        ],
64
        "ok" : 1
65
}
 
 
 
--测试复制:
use test
db.testc.insert({id:1,test:"from Mongodb01,the primary"})
db.testc.find()
 
--连接到备节点:
<wiz_code_mirror>
 
 
 
 
 
 
 
 
 
 
 
1
use test
2
rs.slaveOk()
3
db.testc.find()
4
SH1RS:SECONDARY> db.testc.find()db.testc.find()
5
{ "_id" : ObjectId("5c8a5f960304e7b4a4ac66a4"), "id" : 1, "test" : "from Mongodb01,the primary" }
 
 
 
----------------------------------------------------------------------------------------------------
--Deploy the config servers as a three-member replica set
mkdir /mongodb/data/28002
mkdir /mongodb/conf/28002
mkdir /mongodb/log/28002
cd /mongodb/conf/28002
 
KeyFile从MongoDB01点拷过来,统一放在28001目录下
vi mongo_cfgrs_28002.conf
 
port = 28002
dbpath = /mongodb/data/28002
logpath = /mongodb/log/28002
pidfilepath = /mongodb/data/28002/28002.pid
logappend = true
fork = true
oplogSize = 204800
replSet = CFGRS
#keyFile = /mongodb/conf/28001/.keyFile
 
--启动mongod
numactl --interleave=all mongod --configsvr -f /mongodb/conf/28001/mongo_sh1rs_28001.conf           -- 注意,配置服务库的启动一定要加上--configsvr参数
 
--配置副本集
repConfig = {
              _id:'CFGRS'
configsvr: true,
     members:[
         {_id:0,host:'MongoDB07:28002'},
      {_id:1,host:'MongoDB08:28002'},
      {_id:2,host:'MongoDB09:28002'}]
   }
 
rs.initiate(repConfig)
rs.status()
 
--Config and Start mongos instance
mkdir /mongodb/conf/28003
mkdir /mongodb/log/28003
cd /mongodb/conf/28003
 
KeyFile从其它节点拷过来
vi mongos_28003.conf
 
port = 28003
logpath = /mongodb/log/28002
pidfilepath = /mongodb/data/28002/28002.pid
logappend = true
fork = true
configdb = CFGRS/MongoDB07:28002,MongoDB08:28002,MongoDB09:28002
#keyFile = /mongodb/conf/28001/.keyFile
 
--启动mongos
numactl --interleave=all mongos -f /mongodb/conf/28003/mongos_28003.conf
 
--Add Replica Sets as Shards
连接到mongos
mongo MongoDB10:28003/admin
sh.addShard( "SH1RS/MongoDB01:28001,MongoDB02:28001,MongoDB03:28001" );
sh.addShard( "SH2RS/MongoDB04:28001,MongoDB05:28001,MongoDB06:28001" );
sh.addShard( "SH3RS/MongoDB07:28001,MongoDB08:28001,MongoDB09:28001" );
sh.addShard( "SH4RS/MongoDB10:28001,MongoDB11:28001,MongoDB12:28001" );
 
sh.status()
 
 

--Enable Keyfile Access Control

在所有角色的配置文件中添加:
keyFile = /mongodb/conf/28001/.keyFile
 

Create the shard-local user administrator (optional)

在四个分片的主节点都执行:
admin = db.getSiblingDB("admin")
admin.createUser(
  {
    user: "mongoadmin",
    pwd: "Super**********",
    roles: [ { role: "root", db: "admin" } ]
  }
)
-------------------------
<wiz_code_mirror>
 
 
 
 
 
 
 
 
 
 
 
1
SH1RS:PRIMARY> admin = db.getSiblingDB("admin")admin = db.getSiblingDB("admin")
2
admin
3
SH1RS:PRIMARY> admin.createUser(admin.createUser(
4
... { {
5
... user: "mongoadmin", user: "mongoadmin",
6
... pwd: "Super**********", pwd: "Super**********",
7
... roles: [ { role: "root", db: "admin" } ] roles: [ { role: "root", db: "admin" } ]
8
... } }
9
... ))
10
Successfully added user: {
11
        "user" : "mongoadmin",
12
        "roles" : [
13
                {
14
                        "role" : "root",
15
                        "db" : "admin"
16
                }
17
        ]
18
}
 
 
 
mongo -u "mongoadmin" -p "Super**********" --authenticationDatabase "admin" --port 28001
-----------------------------------------

Create the user administrator

admin = db.getSiblingDB("admin")
admin.createUser(
  {
    user: "shadmin",
    pwd: "shSuper**********",
    roles: [ { role: "root", db: "admin" } ]
  }
)
-----------------------
<wiz_code_mirror>
 
 
 
 
 
 
 
 
 
 
 
1
mongos> admin = db.getSiblingDB("admin")admin = db.getSiblingDB("admin")
2
admin
3
mongos> admin.createUser(admin.createUser(
4
... { {
5
... user: "shadmin", user: "shadmin",
6
... pwd: "shSuper**********", pwd: "shSuper**********",
7
... roles: [ { role: "root", db: "admin" } ] roles: [ { role: "root", db: "admin" } ]
8
... } }
9
... ))
10
Successfully added user: {
11
        "user" : "shadmin",
12
        "roles" : [
13
                {
14
                        "role" : "root",
15
                        "db" : "admin"
16
                }
17
        ]
18
}
 
 
注意:创建用户之后,本地操作系统登录就失效了,后续需要使用账号密码登录
-------------------------------
查看分片状态:
<wiz_code_mirror>
标签: