中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

詳解MongoDB復制集

發布時間:2020-07-21 00:26:47 來源:網絡 閱讀:635 作者:明月幽谷 欄目:MongoDB數據庫

何為復制集?

復制集是額外的數據副本,是跨多個服務器同步數據的過程,復制集提供了冗余并增加了數據可用性,通過復制集可以對硬件故障和中斷的服務進行恢復。

復制集主從同步的原理和mysql類似,主節點記錄在其上的所有操作到oplog中,從節點定期輪詢主節點獲取這些操作,然后對自己的數據副本執行這些操作,從而保證從節點的數據與主節點一致。


復制集的優勢如下:


(1)讓數據更安全


(2)高數據可用性(24*7)


(3)災難恢復


(4)無停機維護(如備份、索引重建、故障轉移)


(5)讀縮放(額外的副本讀取)


(6)副本集對應用程序是透明。

MongoDB復制集結構原理圖分析

  • .mongodb的復制集至少需要兩個節點。其中一個是主節點(Primary),負責處理客戶端請求,其余的都是從節點(Secondary),負責復制主節點上的數據。

  • mongodb各個節點常見的搭配方式為:一主一從或一主多從。

  • 客戶端在主節點寫入數據,在從節點讀取數據,主節點與從節點進行數據交互,保障數據的一致性。如果其中一個節點出現故障,其他節點馬上會將業務接過來而無需停機操作

詳解MongoDB復制集

復制集的特點:

1.N 個節點的集群


2.任何節點可作為主節點


3.所有寫入操作都在主節點上



4.自動故障轉移


5.自動恢復

MongoDB復制集部署

  • 在一臺CentOS7主機上使用yum在線安裝Mongodb,并創建多實例,進行部署MongoDB復制集

首先配置網絡YUM源,baseurl(下載路徑)指定為mongodb官網提供的yum倉庫

vim /etc/yum.repos.d/mongodb.repo

[mongodb-org]

name=MongoDB Repository

baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.6/x86_64/             #指定獲得下載的路徑

gpgcheck=1                     #表示對從這個源下載的rpm包進行校驗

enabled=1                   #表示啟用這個源。

gpgkey=https://www.mongodb.org/static/pgp/server-3.6.asc

重新加載yum源,并使用yum命令下載安裝mongodb

yum list

yum -y install mongodb-org

準備4個實例,暫時開啟3個, 1主2從    再額外添加群集(追加實例)也可以撤銷實例

  • 創建數據文件和日志文件存儲路徑,并賦予權限


    [root@localhost ~]# mkdir -p /data/mongodb{2,3,4}
    [root@localhost ~]# mkdir /data/logs
    [root@localhost ~]# touch /data/logs/mongodb{2,3,4}.log
    [root@localhost ~]# chmod 777 /data/logs/mongodb*
    [root@localhost ~]# ll /data/logs/
    總用量 0
    -rwxrwxrwx. 1 root root 0 9月  15 22:31 mongodb2.log
    -rwxrwxrwx. 1 root root 0 9月  15 22:31 mongodb3.log
    -rwxrwxrwx. 1 root root 0 9月  15 22:31 mongodb4.log

編輯4個MongoDB實例的配置文件

  • 先編輯yum安裝的默認實例的配置文件/etc/mongod.conf,指定監聽IP,端口默認為27017,開啟replication參數配置,replSetName:true(自定義)

[root@localhost ~]# vim /etc/mongod.conf

# mongod.conf

# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/  

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

# Where and how to store data.
storage:
  dbPath: /var/lib/mongo
  journal:
    enabled: true
#  engine:
#  mmapv1:
#  wiredTiger:

# how the process runs
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /var/run/mongodb/mongod.pid  # location of pidfile
  timeZoneInfo: /usr/share/zoneinfo

# network interfaces
net: 

  port: 27017                    #默認端口         
  bindIp: 0.0.0.0             #監聽任意地址

#security:

#operationProfiling:

replication:                   #去掉前面的“#”注釋,開啟該參數設置
replSetName: true          #設置復制集名稱

  • 復制配置文件給其他實例,并將mongodb2.conf 中的port參數配置為27018,mongod3.conf中的port參數配置為27019,mongod4.conf中的port參數配置為27020。 同樣也將dbpath和logpath參數修改為對應的路徑值

cp  /etc/mongod.conf /etc/mongod2.conf

cp /etc/mongod2.conf /etc/mongod3.conf

cp /etc/mongod2.conf /etc/mongod4.conf

  • 實例2的配置文件mongodb2.conf 修改

vim /etc/mongod2.conf

systemLog:

  destination: file

  logAppend: true

  path: /data/logs/mongodb2.log    

storage:

  dbPath: /data/mongodb/mongodb2  

  journal:

enabled: true

port: 27018 

bindIp: 0.0.0.0  # Listen to local interface only, comment to listen on all interfaces.

#security:

#operationProfiling:

replication:
replSetName: true

  • 實例3的配置文件mongodb3.conf 修改

vim /etc/mongod3.conf

systemLog:

  destination: file

  logAppend: true

  path: /data/logs/mongodb3.log    

storage:

  dbPath: /data/mongodb/mongodb3 

  journal:

enabled: true

port: 27019 

bindIp: 0.0.0.0  # Listen to local interface only, comment to listen on all interfaces.

#security:

#operationProfiling:

replication:
replSetName: true

  • 實例4的配置文件mongodb4.conf 修改

vim /etc/mongod4.conf

systemLog:

  destination: file

  logAppend: true

  path: /data/logs/mongodb4.log    

storage:

  dbPath: /data/mongodb/mongodb4  

  journal:

enabled: true

port: 27020 

bindIp: 0.0.0.0  # Listen to local interface only, comment to listen on all interfaces.

#security:

#operationProfiling:

replication:
replSetName: true

啟動mongodb各實例

[root@localhost ~]# mongod -f /etc/mongod.conf
about to fork child process, waiting until server is ready for connections.
forked process: 93576
child process started successfully, parent exiting
[root@localhost ~]# mongod -f /etc/mongod2.conf
about to fork child process, waiting until server is ready for connections.
forked process: 93608
child process started successfully, parent exiting
[root@localhost ~]# mongod -f /etc/mongod3.conf
about to fork child process, waiting until server is ready for connections.
forked process: 93636
child process started successfully, parent exiting
[root@localhost ~]# mongod -f /etc/mongod4.conf
about to fork child process, waiting until server is ready for connections.
forked process: 93664
child process started successfully, parent exiting
[root@localhost ~]# netstat -antp | grep mongod                        //查看mongodb進程狀態
tcp        0      0 0.0.0.0:27019           0.0.0.0:*               LISTEN      93636/mongod       
tcp        0      0 0.0.0.0:27020           0.0.0.0:*               LISTEN      93664/mongod       
tcp        0      0 0.0.0.0:27017           0.0.0.0:*               LISTEN      93576/mongod       
tcp        0      0 0.0.0.0:27018           0.0.0.0:*               LISTEN      93608/mongod

配置三個節點的復制集

[root@localhost ~]# mongo                     //進入其中一個實例
MongoDB shell version v3.6.7
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.6.7

> rs.status()                               //查看復制集狀態,提示復制集還未配置
{
    "info" : "run rs.initiate(...) if not yet done for the set",
    "ok" : 0,
    "errmsg" : "no replset config has been received",
    "code" : 94,
    "codeName" : "NotYetInitialized",
    "operationTime" : Timestamp(0, 0),
    "$clusterTime" : {
        "clusterTime" : Timestamp(0, 0),
        "signature" : {
            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId" : NumberLong(0)
        }
    }

> cfg={"_id":"true","members":[{"_id":0,"host":"192.168.195.137:27017"},{"_id":1,"host":"192.168.195.137:27018"},{"_id":2,"host":"192.168.195.137:27019"}]}                    // 定義cfg初始化參數                    
{
    "_id" : "true",
    "members" : [
        {
            "_id" : 0,
            "host" : "192.168.195.137:27017"
        },
        {
            "_id" : 1,
            "host" : "192.168.195.137:27018"
        },
        {
            "_id" : 2,
            "host" : "192.168.195.137:27019"
        }
    ]
}

> rs.initiate(cfg)                    //初始化并啟動復制集

{"ok" : 1}

true:PRIMARY> rs.status()               //再次查看復制集的狀態信息
{
    "set" : "true",
    "date" : ISODate("2018-09-15T15:39:48.426Z"),
    "myState" : 1,
    "term" : NumberLong(1),
    "syncingTo" : "",
    "syncSourceHost" : "",
    "syncSourceId" : -1,
    "heartbeatIntervalMillis" : NumberLong(2000),
    "optimes" : {
        "lastCommittedOpTime" : {
            "ts" : Timestamp(1537025984, 1),
            "t" : NumberLong(1)
        },
        "readConcernMajorityOpTime" : {
            "ts" : Timestamp(1537025984, 1),
            "t" : NumberLong(1)
        },
        "appliedOpTime" : {
            "ts" : Timestamp(1537025984, 1),
            "t" : NumberLong(1)
        },
        "durableOpTime" : {
            "ts" : Timestamp(1537025984, 1),
            "t" : NumberLong(1)
        }
    },
    "members" : [
        {
            "_id" : 0,
           "name" : "192.168.195.137:27017",
            "health" : 1,
            "state" : 1,
           "stateStr" : "PRIMARY",              //此節點成為主節點
            "uptime" : 1371,
            "optime" : {
                "ts" : Timestamp(1537025984, 1),
                "t" : NumberLong(1)
            },

....................


                  {
            "_id" : 1,
            "name" : "192.168.195.137:27018",
            "health" : 1,
            "state" : 2,
           "stateStr" : "SECONDARY",                //此節點為從節點
            "uptime" : 18,
            "optime" : {
                "ts" : Timestamp(1537025984, 1),
                "t" : NumberLong(1)
            },
            "optimeDurable" : {
                "ts" : Timestamp(1537025984, 1),
                "t" : NumberLong(1)
            },

...................


                 {
            "_id" : 2,
            "name" : "192.168.195.137:27019",
            "health" : 1,
            "state" : 2,
           "stateStr" : "SECONDARY",                   //此節點為從節點
            "uptime" : 18,
            "optime" : {
                "ts" : Timestamp(1537025984, 1),
                "t" : NumberLong(1)
            },
            "optimeDurable" : {
                "ts" : Timestamp(1537025984, 1),
                "t" : NumberLong(1)
            },
         

  • 添加節點

true:PRIMARY> rs.add("192.168.195.137:27020")

true:PRIMARY> rs.status()

..............

{
            "_id" : 3,
            "name" : "192.168.195.137:27020",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 8,
            "optime" : {
                "ts" : Timestamp(1537026818, 1),
                "t" : NumberLong(1)
            },
            "optimeDurable" : {
                "ts" : Timestamp(1537026818, 1),
                "t" : NumberLong(1)
            },

.................

  • 刪除節點

true:PRIMARY> rs.remove("192.168.195.137:27020")

模擬故障自動轉移切換

[root@localhost ~]# mongod -f /etc/mongod.conf --shutdown              //關閉主節點服務
killing process with pid: 93576
[root@localhost ~]# mongo --port 27018                      //進入其中一個從節點
MongoDB shell version v3.6.7
connecting to: mongodb://127.0.0.1:27018/
MongoDB server version: 3.6.7

true:PRIMARY> rs.status()                  //查看復制集信息

..............


    },
    "members" : [
        {
            "_id" : 0,
           "name" : "192.168.195.137:27017",                           //原來的主節點健康值為0
            "health" : 0,
            "state" : 8,
            "stateStr" : "(not reachable/healthy)",
            "uptime" : 0,
            "optime" : {
                "ts" : Timestamp(0, 0),
                "t" : NumberLong(-1)
            },
            "optimeDurable" : {
                "ts" : Timestamp(0, 0),
                "t" : NumberLong(-1)
            },

.................


                  {
            "_id" : 1,
           "name" : "192.168.195.137:27018",
            "health" : 1,
            "state" : 1,
           "stateStr" : "PRIMARY",                         //此節點切換成主節點
            "uptime" : 2657,
            "optime" : {
                "ts" : Timestamp(1537027275, 1),
                "t" : NumberLong(2)
            },

................


                    {
            "_id" : 2,
            "name" : "192.168.195.137:27019",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 1312,
            "optime" : {
                "ts" : Timestamp(1537027275, 1),
                "t" : NumberLong(2)
            },
            "optimeDurable" : {
                "ts" : Timestamp(1537027275, 1),
                "t" : NumberLong(2)
            },
          

手動進行主從切換

[root@localhost ~]# mongod -f /etc/mongod.conf           #開啟剛才關閉的端口為27017的節點實例

about to fork child process, waiting until server is ready for connections.
forked process: 94723
child process started successfully, parent exiting
[root@localhost ~]# mongo --port 27018             #進入主節點服務器實例
MongoDB shell version v3.6.7
connecting to: mongodb://127.0.0.1:27018/
MongoDB server version: 3.6.7

true:PRIMARY> rs.freeze(30)                     //暫停30s不參與選舉

true:PRIMARY> rs.stepDown(60.30)              //交出主節點位置,維持從節點狀態不少于60秒,等待30秒使主節點和從節點日志同步  

true:SECONDARY> rs.status()             //可以看到本實例已經切換成從節點

................


    "members" : [
        {
            "_id" : 0,
            "name" : "192.168.195.137:27017",                // 端口27017的節點變成了主節點
            "health" : 1,
            "state" : 1,
            "stateStr" : "PRIMARY",                        
            "uptime" : 167,
            "optime" : {
                "ts" : Timestamp(1537027620, 1),
                "t" : NumberLong(3)
            },
            "optimeDurable" : {
                "ts" : Timestamp(1537027620, 1),
                "t" : NumberLong(3)
            },

..................
            
        {
            "_id" : 1,
            "name" : "192.168.195.137:27018",                    // 端口27018的節點變成了從節點         

            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 2997,
            "optime" : {
                "ts" : Timestamp(1537027620, 1),
                "t" : NumberLong(3)
            },

...................
          
        {
            "_id" : 2,
            "name" : "192.168.195.137:27019",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 1651,
            "optime" : {
                "ts" : Timestamp(1537027620, 1),
                "t" : NumberLong(3)
            },
            "optimeDurable" : {
                "ts" : Timestamp(1537027620, 1),
                "t" : NumberLong(3)
            },

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

临猗县| 黄龙县| 军事| 涞水县| 托克托县| 高邑县| 泾源县| 金昌市| 藁城市| 永定县| 应用必备| 钟祥市| 屏东县| 普安县| 甘孜| 开化县| 怀集县| 庆阳市| 图们市| 贵港市| 会理县| 楚雄市| 夏河县| 城步| 苗栗市| 台东县| 拜城县| 札达县| 永登县| 克拉玛依市| 宁安市| 呼伦贝尔市| 化德县| 旬阳县| 德清县| 司法| 大悟县| 曲沃县| 蓬溪县| 乐清市| 萨迦县|