mongoDB複本集的搭建

來源:互聯網
上載者:User

標籤:key   primary   mongodb   複本集   wiredtiger   

環境簡介

    192.168.1.151      FedoraServer   用作複本集的主庫,已經有資料

    192.168.1.152      FedoraServer   從庫,空執行個體

    192.168.1.153      FedoraServer   從庫,空執行個體

MongoDB的複本集和MySQL基於GTID的多源三執行個體迴圈主從即

  650) this.width=650;" src="https://s3.51cto.com/wyfs02/M01/A7/76/wKioL1nm_E7CCZ3PAACKsl1qQYw193.png-wh_500x0-wm_3-wmp_4-s_912918195.png" title="無標題.png" alt="wKioL1nm_E7CCZ3PAACKsl1qQYw193.png-wh_50" />

但:相對與MySQL的基於Keepalive或者MHA等第三方外掛程式來實現切換或選主,mongodb內建容錯移轉與切換選主功能,這個產品很有進階感


搭建步驟

  1. mongodb安裝:


     Percona-Server-MongoDB-34-server

 2. mongodb配置:

    vim /etc/mongod.conf    # mongod.conf, Percona Server for MongoDB    # for documentation of all options, see:    #          #mongodb的配置有個特別之處:冒號“:”後面要加空格    # Where and how to store data.    storage:      dbPath: /data/mongo/27153/data/              #指定mongodb檔案儲存體位置    journal:      enabled: true    #  engine: mmapv1    #  engine: PerconaFT    #  engine: rocksdb    engine: wiredTiger         #指定儲存引擎,線虎引擎和Innodb有些相似,支援文檔鎖(行鎖)    # Storage engine various options    #  mmapv1:    #  wiredTiger:    # where to write logging data.    systemLog:      quiet: true      destination: file      logAppend: true          #日誌採用追加的方式      path: /data/mongo/27153/log/mongod.log       #記錄檔位置    processManagement:      fork: true               #啟動線程拉起主線程,與mysqld_safe進程有些相似      pidFilePath: /data/mongo/27153/mongod.pid     #PID檔案位置    # network interfaces    net:      port: 27153              #連接埠,預設27017      bindIp: 0.0.0.0          #開放訪問的IP範圍    security:      authorization: enabled   #開啟驗證,      keyFile: /data/mongo/mongodb.key             #使用key檔案進行複本集之間的認證    #operationProfiling:    #replication:    replication:      replSetName: zrz         #同步集的名稱    #sharding:    ## Enterprise-Only Options:    #auditLog:#snmp:

 3.搭建過程:

  1.三台都關閉驗證模式

 

 vim /etc/mongod.conf 注釋掉驗證相關的配置語句 #security: #   authorization: enabled   #開啟驗證, #     keyFile: /data/mongo/mongodb.key             #使用key檔案進行複本集之間的認證

  2.啟動服務:

 

mongod -f /etc/mongod.conf

  3.在有資料的執行個體上登陸:

 

mongo --port 27151mongo>config={"_id":"zrz","members":[{"_id":1,host:"192.168.1.151:27151",priority:100},                                     {"_id":2,host:"192.168.1.152:27152",priority:0}]}       //定義配置到變數config中       //zrz為群組名,priority為選主權重,越高越容易成為主(非故障情況下)       mongo>rs.initiate(config)       //使用rs.initiate命令初始化複本集配置mongo>rs.status       //查看複本集配置mongo>rs.add({"_id":3,host:"192.168.1.153:27153",priority:0})       //使用另外一種方式增加複本集

  4.開啟驗證:

    為了安全起見,需要對MongoDB的訪問增加驗證:

    步驟1:對現在primary層級的執行個體進行增加使用者  db.createUser()

    步驟2:關閉secondary層級的執行個體              mongod --shutdown -f /etc/mongod.conf

    步驟3:關閉primary層級的執行個體                

    步驟4:修改每個執行個體的設定檔,開啟驗證。配置賬戶或者分發密鑰,

    步驟5:先開啟最後關閉的primary執行個體,後開啟各個secondary執行個體  

   跨執行個體之間的認證有兩種方式:賬戶與口令 或者 使用SSL密鑰

     其中:賬戶和口令只能在mongos分區的設定管理員上使用,執行個體之間通過config_server驗證

           密鑰檔案,是通過通過openssl程式產生的base64字串,具有相同字串的可以同步

   產生方式:

       openssl rand -base64 756 > /data/mongo/mongodb.key

   變更key檔案的屬性,防修改:

       chmod 400 /data/mongo/mongodb.key

   分發給自己的小夥伴們:

       scp /data/mongo/mongodb.key 192.168.1.152:/data/mongo/       scp /data/mongo/mongodb.key 192.168.1.153:/data/mongo/

   修改設定檔,指定密鑰位置

       vim /etc/mongod.conf             security:        authorization: enabled   #開啟驗證,        keyFile: /data/mongo/mongodb.key             #使用key檔案進行複本集之間的認證

   按順序開啟服務就可以了

       mongod -f /etc/mongo

常見錯誤及其解決方式:

 1.跨版本搭建複本集(3.0-3.4),MongoDB現在不支援

 2.從執行個體在加入複本集失敗後,意外的產生了一些資料,導致無法被primary加入複本集

   處理方式:注釋掉複製相關的設定檔,重啟服務,即進入單機模式,使用db.dropDatabase()

            刪除所有資料庫,然後重新進入複本集啟動模式既可

 3.提示已經有配置了,無法初始化,使用rs.reconfig(config{force:true})

4.使用rs.help()可以查看相關複本集的命令



密鑰檔案:https://docs.mongodb.com/manual/reference/configuration-options/#security.keyFile

重新設定:https://docs.mongodb.com/manual/reference/method/rs.reconfig/#rs.reconfig

複製相關命令:https://docs.mongodb.com/manual/reference/method/js-replication/

本文出自 “漫漫SQL路......” 部落格,請務必保留此出處http://l0vesql.blog.51cto.com/4159433/1973850

mongoDB複本集的搭建

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.