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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM