mongo分片集群 + KeyFile 认证


原文地址

一、环境准备

  1. 服务器节点信息【三台服务器】
      x.x.x.33、 x.x.x..37、 x.x.x..41
  2. 服务器系统
      linux x86_64 GNU/Linux 或者 centos7 或者 redhat
  3. mongodb版本
      mongodb v4.2.5 下载地址

二、集群环境设计

  1. 三个 分片 复制集合
        shard1(x.x.x.33:28017、 x.x.x.37:28017、 x.x.x.41:28017)
        shard2(x.x.x.33:28018、 x.x.x.37:28018、 x.x.x.41:28018)
        shard3(x.x.x.33:28019、 x.x.x.37:28019、 x.x.x.41:28019)
  2. 一个 config 复制集合
        (x.x.x.33:27017、 x.x.x.37:27017、 x.x.x.41:27017)
  3. 一个 mongos 节点
        (x.x.x.37:27000)

三、mongo 安装

$ tar -zxvf mongodb-linux-x86_64-rhel70-4.2.5.tgz
$ mv mongodb-linux-x86_64-rhel70-4.2.5 /opt/mongodb4.2

四、添加环境变量

1.vim /etc/profile
2. 文件末尾追加: export PATH=$PATH:/opt/mongodb/bin
3. 加载文件立刻生效: source /etc/profile

五、搭建mongodb分片复制集

分别在三台服务器(x.x.x.33、x.x.x.37、 x.x.x.41)节点 执行如下命令

5.1 创建目录

创建分片数据存储目录 dbPath 存储目录
 mkdir -p /opt/mongodb4.2/data/db1  【分片1】
 mkdir -p /opt/mongodb4.2/data/db2  【分片2】
 mkdir -p /opt/mongodb4.2/data/db3  【分片3】

分片集群配置文件 目录
 mkdir -p /opt/mongodb4.2/conf

分片日志目录
 mkdir -p /opt/mongodb4.2/logs

5.2 创建集群安全认证机制KeyFile

openssl rand -base64 736 > /opt/mongodb4.2/conf/mongodb.keyfile

注意: keyfile 必须满足以下条件:

  1. 至少6个字符,小于1024字节。
  2. 认证的时候不考虑文件中的空白字符。
  3. 连接副本集成员的KeyFile和启动mongos进程的 KeyFile文件内容必须一致。
  4. 三台服务器的 keyFile 必须一样
  5. 必须是base64编码,但不能有等号。
  6. 文件权限必须是 600(chmod 600),不能分配任何权限给group成员和其他成员。

5.3 编写分片复制集配置文件

x.x.x.37 为例,【分片1】 配置文件:
vim /opt/mongodb4.2/conf/mongodb1.conf
mongodb1.conf db1 28017 jxk1
mongodb2.conf db2 28018 jxk2
mongodb2.conf db3 28019 jxk3

storage:
  dbPath: /opt/mongodb4.2/data/db1 # 不同副本不同
  journal:
      enabled: true
  wiredTiger:
      engineConfig:
          cacaeSizeGB: 2
          directoryForIndexes: true
systemLog:
  destination: file
  logAppend: true
  path: /opt/mongodb4.2/logs/mongodb1.log # 日志
net:
  port: 27017 # 端口
  bindIp: 127.0.0.1,192.x.x.37 # 远程 0.0.0.0
processManagement:
  fork: true
replication:
  oplogSizeMB: 4000
  replSetName: jxk1  # 不同副本名称不同
sharding:
  clusterRole: shardsvr
# 注意,如果需要建立用户,这部分先注释则不进行 auth 校验,等创建完用户再开启
#security:
#  authorization: enabled
#  keyFile: /opt/mongodb4.2/conf/mongodb.keyfile
#  clusterAuthMode: keyFile

5.4 启动 分片复制集

1.分别在三台服务器(x.x.x.33、x.x.x.37、 x.x.x.41)节点 执行如下命令
2. mongod -f /opt/mongodb4.2/conf/mongodb1.conf
3. mongod -f /opt/mongodb4.2/conf/mongodb2.conf
4. mongod -f /opt/mongodb4.2/conf/mongodb3.conf

5.5 初始化分片复制集

  1. 在其中任意一台节点服务器登录 本人使用 x.x.x.159节点
  2. mongo -port 27017 【登录】
  3. 进入mongodb命令输入终端 输入命令如下

var conf = {
    _id:'shard',
    members:[
        {_id:1, host:'x.x.x.33:28017'},
        {_id:2, host:'x.x.x.37:28017'},
        {_id:3, host:'x.x.x.41:28017'},
    ],
}
rs.initiate(conf)

5.6 设置分片复制集账号

1.查看集群服务器信息
rs.status()

2.找到mongodb自己选举出来的 主库(Master),其他库不行,输入命令

use admin
db.createUser({
    user: "username",
    pwd : "password",
    roles:[
        {role:"root", db:"admin"}
    ]
})
db.auth("username", "password")

5.7 验证

到从库验证主库建立的用户是否可以通过授权
db.auth("username", "password")
注意:
防火墙状态,防火墙端口是否开启

六、搭建config节点复制集

分别在三台服务器(x.x.x.33、x.x.x.37、x.x.x.41)节点 执行如下命令

6.1 创建目录

创建 config 数据存储 目录
mkdir -p /opt/mongodb4.2/cfg/data

创建 config 日志 目录
mkdir -p /opt/mongodb4.2/cfg/logs

6.2 创建复制集配置文件

vim /opt/mongodb4.2/conf/mongodb-cfg.conf

systemLog:
    destination: file
    logAppend: true
    path: /opt/mongodb4.2/cfg/logs/mongodb.log
storage:
    journal:
        enabled: true
    dbPath: /opt/mongodb4.2/cfg/data
    directoryPerDB: true
    wiredTiger:
        engineConfig:
            cacaeSizeGB: 2
            directoryForIndexes: true
        collectionConfig:
            blockCompressor: zstd
        indexConfig:
            prefixCompression: true
net:
    port: 27017
    bindIp: x.x.x.37
replication:
    oplogSizeMB: 4000
    replSetName: configReplSet
sharding:
    clusterRole: configsvr
processManagement:
    fork: true 
# 注意,如果需要建立用户,这部分先注释则不进行 auth 校验,等创建完用户再开启
#security:
#  authorization: enabled
#  keyFile: /opt/mongodb4.2/conf/mongodb.keyfile
#  clusterAuthMode: keyFile        

6.3 启动config节点复制集

mongod -f /opt/mongodb4.2/conf/mongodb-cfg.conf

6.4 登录config节点

mongo -host x.x.x.(33/37/41) -port 27017

6.5 初始化 config 节点

var conf = {
    _id:'configReplSet',
    members:[
        {_id:0, host:"x.x.x.37:27017"},
        {_id:1, host:"x.x.x.33:27017"},
        {_id:2, host:"x.x.x.41:27017"},
    ]
}
rs.initiate(conf)

6.6 创建认证用户

选择主库,并进入

use admin
db.createUser({
    user: "username",
    pwd : "password",
    roles:[
        {role:"root", db:"admin"}
    ]
})
db.auth("username", "password")

七 搭建 mongos 节点

在 x.x.x.37 服务器上搭建客户端

7.1 创建mongos 日志目录

mkdir -p /opt/mongodb4.2/mongos/logs

7.2 创建mongos 配置文件

vim /opt/mongodb4.2/conf/mongos.conf

systemLog:
    destination: file
    logAppend: true
    path: /opt/mongodb4.2/mongos/logs/mongodb.log
net:
    port: 27000
    bindIp: 192.x.x.37
sharding:
    configDB: configReplSet/x.x.x.33:27017,x.x.x.37:27017,x.x.x.41:27017
processManagement:
    fork: true 
security:
    keyFile: /opt/mongodb4.2/conf/mongodb.keyfile
    clusterAuthMode: keyFile

7.3 启动mongos

mongos -f /opt/mongodb4.2/conf/mongos.conf

7.4 登录并初始化 mongos

mongo -port 27000
use admin
db.auth("username", "password")  #这里使用的账号是 config复制集的账号
db.runCommand({
    addshard:"jxk1/x.x.x.33:28017,x.x.x.37:28017,x.x.x.41:28017,",
    name: "shard1"
});
db.runCommand({
    addshard:"jxk2/x.x.x.33:28018,x.x.x.37:28018,x.x.x.41:28018,",
    name: "shard2"
});
db.runCommand({
    addshard:"jxk3/x.x.x.33:28019,x.x.x.37:28019,x.x.x.41:28019,",
    name: "shard3"
})

7.5 测试是否搭建成功

db.runCommand( {enablesharding:"dbtest"})
db.runCommand( {shardcollection:"dbtest.one", key:{id:1} })
use dbtest
var tmp = [];
for(var i =0; i<10000000; i++){
    tmp.push({ 'id':i, "name":"lunck dog " + i});
}
db.one.insertMany(tmp);
sh.status()


###进入数据库
db.one.count();

八 常用命令汇总

1.分片命令

mongod -f /opt/mongodb4.2/conf/mongodb1.conf 启动
mongod -f /opt/mongodb4.2/conf/mongodb1.conf –shutdown 关闭
OR
mongo -port 27017
use admin
db.shutdownServers()

2.config 复制集命令

mongod -f /opt/mongodb4.2/conf/mongodb-cfg.conf 启动
mongod -f /opt/mongodb4.2/conf/mongodb-cfg.conf –shutdown 关闭

3.mongos命令

mongos -f /opt/mongodb4.2/conf/mongos.conf 启动
mongo -port 27017 关闭
use admin
db.shutdownServers()

4.增加副本和复制点
use admin
db.auth(‘username’,’password’)
rs.add(‘x.x.x.33:28017’);
rs.add(‘x.x.x.33:27017’)


文章作者: Jack
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Jack !
评论
  目录