zoukankan      html  css  js  c++  java
  • mongodb分片集群 搭建 + keyFile认证

     创建mongodb分片集群 + keyFile认证

    一、环境准备

       1. 服务器节点信息【三台服务器】
        x.x.x.159、 x.x.x..160、 x.x.x..161
       2. 服务器系统
        linux x86_64 GNU/Linux 或者 centos7 或者 redhat
      3. mongodb版本
        mongodb v4.2.5
        下载地址:https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.2.5.tgz

     二、 集群环境设计

      1. 三个 分片 复制集合
        shard1(x.x.x.159:27017、 x.x.x.160:27017、 x.x.x.161:27017)
        shard2(x.x.x.159:27018、 x.x.x.160:27018、 x.x.x.161:27018)
        shard3(x.x.x.159:27019、 x.x.x.160:27019、 x.x.x.161:27019)
      2. 一个 config 复制集合
        (x.x.x.159:28018、 x.x.x.160:28018、 x.x.x.161:28018)
      3. 一个 mongos 节点
        (x.x.x.159:27000)

     三、mongodb 安装、

    `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分片复制集

      1. 分别在三台服务器(x.x.x.159、x.x.x.160、 x.x.x.161)节点 执行如下命令

      2. mongodb分片集群 配置文件 目录

          mkdir -p /opt/mongodb4.2/conf

      3. 分片日志目录

        mkdir -p /opt/mongodb4.2/logs

      4. 创建集群安全认证机制KeyFile

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

      Notice: keyfile 必须满足以下条件:

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

     

      5. 创建分片 数据存储目录

        mkdir -p /opt/mongodb4.2/data/db1 【分片1】
        mkdir -p /opt/mongodb4.2/data/db2 【分片2】
        mkdir -p /opt/mongodb4.2/data/db3 【分片3】

      6. 创建分片配置文件

      vim /opt/mongodb4.2/conf/mongodb1.conf 【分片1 配置文件】
            storage:
                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: 0.0.0.0
            processManagement:
                fork: true
            replication:
                oplogSizeMB: 4000
                replSetName: jxk1
            sharding:
                clusterRole: shardsvr
            security:
                authorization: enabled
                keyFile: /opt/mongodb4.2/conf/mongodb.keyfile
                clusterAuthMode: keyFile
    
    
        vim /opt/mongodb4.2/conf/mongodb2.conf 【分片2 配置文件】
            storage:
                journal:
                    enabled: true
                wiredTiger:
                    engineConfig:
                        cacaeSizeGB: 2
                        directoryForIndexes: true
            systemLog:
                destination: file
                logAppend: true
                path: /opt/mongodb4.2/logs/mongodb2.log
            net:
                port: 27018
                bindIp: 0.0.0.0
            processManagement:
                fork: true
            replication:
                oplogSizeMB: 4000
                replSetName: jxk2
            sharding:
                clusterRole: shardsvr
            security:
                authorization: enabled
                keyFile: /opt/mongodb4.2/conf/mongodb.keyfile
                clusterAuthMode: keyFile
    
    
        vim /opt/mongodb4.2/conf/mongodb3.conf 【分片3 配置文件】
            storage:
                journal:
                    enabled: true
                wiredTiger:
                    engineConfig:
                        cacaeSizeGB: 2
                        directoryForIndexes: true
            systemLog:
                destination: file
                logAppend: true
                path: /opt/mongodb4.2/logs/mongodb3.log
            net:
                port: 27018
                bindIp: 0.0.0.0
            processManagement:
                fork: true
            replication:
                oplogSizeMB: 4000
                replSetName: jxk3
            sharding:
                clusterRole: shardsvr
            security:
                authorization: enabled
                keyFile: /opt/mongodb4.2/conf/mongodb.keyfile
                clusterAuthMode: keyFile
    
        注意: 配置文件 yuml语法格式 直接拷贝下来 语法格式估计不能用
    

       

     六、启动 分片复制集

      1. 分别在三台服务器(x.x.x.159、x.x.x.160、 x.x.x.161)节点 执行如下命令
      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

    七、 初始化分片复制集 并 设置 分片复制集账号

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

        var conf = {
            _id:'jxk',
            members:[
                {_id:1, host:'x.x.x.159:27017'},
                {_id:2, host:'x.x.x.160:27017'},
                {_id:3, host:'x.x.x.161:27017'},
            ],
        }
        rs.initiate(conf)
        rs.status()
    

      

      4. 找到mongodb自己选举出来的 主库 输入命令:

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

    八、搭建config节点复制集

      1. 分别在三台服务器(x.x.x.159、x.x.x.160、 x.x.x.161)节点 执行如下命令

      2. 创建 config 数据存储 目录

        mkdir -p /opt/mongodb4.2/mongo-cfg/data

      3. 创建 config 日志 目录

        mkdir -p /opt/mongodb4.2/mongo-cfg/logs

      4. 创建 config复制集 配置文件
     

        vim /opt/mongodb4.2/conf/mongodb-cfg.conf
            systemLog:
                destination: file
                logAppend: true
                path: /opt/mongodb4.2/logs/mongodb3.log
            storage:
                journal:
                    enabled: true
                dbPath: /opt/mongodb4.2/mongo-cfg/data
                directoryPerDB: true
                wiredTiger:
                    engineConfig:
                        cacaeSizeGB: 2
                        directoryForIndexes: true
                    collectionConfig:
                        blockCompressor: zstd
                    indexConfig:
                        prefixCompression: true
            net:
                port: 28018
                bindIp: x.x.x.159
            replication:
                oplogSizeMB: 4000
                replSetName: configReplSet
            sharding:
                clusterRole: configsvr
            processManagement:
                fork: true 

      5. 启动config节点复制集

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

      6. 登录config节点

        mongo -host x.x.x.(159/160/161) -port 28018

      7.初始化 config节点

            var conf = {
                _id:'configReplSet',
                members:[
                    {_id:0, host:"x.x.x.159:28018"},
                    {_id:1, host:"x.x.x.160:28018"},
                    {_id:2, host:"x.x.x.161:28018"},
                ]
            }
            rs.initiate(conf)
            rs.status()
        

      8. 创建认证用户

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

      

      9. 开启config复制集 认证
        关闭 config复制集
        在配置文件追加
        vim /opt/mongodb4.2/conf/mongodb-cfg.conf

         security:
              authorization: enabled
              keyFile: /opt/mongodb4.2/conf/mongodb.keyfile
              clusterAuthMode: keyFile
    

        启动 config复制集

    九、搭建mongos节点(x.x.18.159)

      1. 在 x.x.x.159 服务器上搭建客户端
      2. 创建mongos 日志目录

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

    3. 创建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: 0.0.0.0
            sharding:
                configDB: configReplSet/x.x.x.159:28018,x.x.x.160:28018,x.x.x.160:28018,
            processManagement:
                fork: true  
            security:
                keyFile: /opt/mongodb4.2/conf/mongodb.keyfile
                clusterAuthMode: keyFile

      4. 启动mongos

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

      5. 登录并初始化 mongos

        mongo -port 27000
        use admin
        db.auth("username", "password")  #这里使用的账号是 config复制集的账号
        db.runCommand({
            addshard:"jxk1/x.x.x.159:27017,x.x.x.160:27017,x.x.x.161:27017,",
            name: "shard1"
        });
        db.runCommand({
            addshard:"jxk2/x.x.x.159:27018,x.x.x.160:27018,x.x.x.161:27018,",
            name: "shard1"
        });
        db.runCommand({
            addshard:"jxk3/x.x.x.159:27019,x.x.x.160:27019,x.x.x.161:27019,",
            name: "shard1"
        })
    

      

      6. 测试是否搭建成功

        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()
    

      

    十、常用命令汇总

      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()
    

      

    参考资料

      https://my.oschina.net/u/563789/blog/3050068
      https://blog.csdn.net/qq_24434491/article/details/102907486
      https://blog.csdn.net/lezeqe/article/details/90518179
      https://blog.csdn.net/tototuzuoquan/article/details/78295040
      https://www.freesion.com/article/6886370039/#8mongo_247
      https://www.cnblogs.com/shaosks/p/5775757.html
      https://www.cnblogs.com/wxmdevelop/p/7341292.html

  • 相关阅读:
    Android SharedPreferences 简单使用
    Android Broadcast广播
    Android Service 的生命周期
    Android RecyclerView的使用
    Android ListView addHeaderView()、addFooterView()和变更列表
    Android ListView的缓存机制
    Android ViewPager + Fragment的实现与Fragment生命周期管理
    Android 触摸事件的传递过程
    Java 泛型及相关使用
    android 常用View的一些注意点(CheckedTextView)
  • 原文地址:https://www.cnblogs.com/jxkshu/p/mongodb.html
Copyright © 2011-2022 走看看