第六章 MongoDB副本集搭建

buffeer發表於2020-11-01

第六章 MongoDB 副本集搭建

  • 搭建副本叢集
  • 在主節點新增資料,檢視從節點
  • kill一個mongod例項,模擬故障,檢視選舉

1.搭建副本叢集

  • 環境準備:操作環境:CentOS 7。需要三臺安裝MongoBD的主機。安裝移步至:MongoDB安裝與使用
host1:192.168.195.3
host2: 192.168.195.4
host3: 192.168.195.5
  • 關閉Linux防火牆和SElinux
systemctl stop firewalld.service # 關閉防火牆
systemctl disable firewalld.service # 禁止開機啟動
firewall-cmd --state # 檢視防火牆狀態

# 永久關閉Selinux 設定後需要重啟
vim /etc/selinux/config

SELINUX=disabled
  • 給每個mongod例項建立目錄
mkdir -p /srv/mongodb/rs0
  • 修改mongod.conf配置檔案
vim /opt/local/mongodb/conf/mongod.conf

systemLog:
  destination: file
  logAppend: true
  path: /opt/local/mongodb/log/mongod.log

storage:
  dbPath: /opt/local/mongodb/data
  journal:
    enabled: true
    
processManagement:
  fork: true  
  pidFilePath: /opt/local/mongodb/mongod.pid  
  timeZoneInfo: /usr/share/zoneinfo

net:
  port: 27017
  bindIp: 0.0.0.0 # 這裡所有ip地址都可以訪問

replication:
  replSetName: "rs0"
  • 啟動三臺mongod例項,登入到其中一個mongod例項,執行初始化
# 啟動mongod服務
/opt/local/mongodb/bin/mongod -f /opt/local/mongodb/conf/mongod.conf

# 連線mongod服務
/opt/local/mongodb/bin/mongo -p 27017

# 初始化 _id是副本叢集名字
rs.initiate( {
   _id : "rs0",
   members: [
      { _id: 0, host: "192.168.195.3:27017" },
      { _id: 1, host: "192.168.195.4:27017" },
      { _id: 2, host: "192.168.195.5:27017" }
   ]
})
  • 檢視副本叢集的配置情況
rs.conf()

{
        "_id" : "rs0",
        "version" : 1,
        "protocolVersion" : NumberLong(1),
        "writeConcernMajorityJournalDefault" : true,
        "members" : [
                {
                        "_id" : 0,
                        "host" : "192.168.195.3:27017",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {
                                
                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                },
                {
                        "_id" : 1,
                        "host" : "192.168.195.4:27017",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {
                                
                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                },
                {
                        "_id" : 2,
                        "host" : "192.168.195.5:27017",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {
                                
                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                }
        ],
        "settings" : {
                "chainingAllowed" : true,
                "heartbeatIntervalMillis" : 2000,
                "heartbeatTimeoutSecs" : 10,
                "electionTimeoutMillis" : 10000,
                "catchUpTimeoutMillis" : -1,
                "catchUpTakeoverDelayMillis" : 30000,
                "getLastErrorModes" : {
                        
                },
                "getLastErrorDefaults" : {
                        "w" : 1,
                        "wtimeout" : 0
                },
                "replicaSetId" : ObjectId("5f9eb1a81d83200f367912d7")
        }
}

2.在主節點新增資料,檢視從節點

  • 登入到從節點 (secondary) 檢視 student 集合資料,此時並沒有 student 集合
# secondary 節點預設不允許讀,需要`rs.slaveOk()`設定可讀
rs.slaveOk();

use test;
db.student.find({})
  • 在主節點(primary)新增student集合
db.student.insertOne({
    name: "Tome",
    age: 20,
    address: "nanjing"
})
  • secondary 節點上檢視 student 集合就能看到從 primary 節點同步過來的資料了
db.student.find({})

{ "_id" : ObjectId("5f9eb62d9a01685fb407a5bc"), "name" : "Tome", "age" : 20, "address" : "nanjing" }

3.模擬故障

ps -aux | grep mongod

root      36483  1.4 10.4 1943052 103928 ?      Sl   07:52   0:28 ./bin/mongod -f ./conf/mongodb.conf
root      70708  0.0  0.0 112824   976 pts/0    R+   08:24   0:00 grep --color=auto mongod

kill -9 36483

從這裡可以看到以前的 secondary 節點變成 primary 節點
第六章 MongoDB副本集搭建

相關文章