您好,登錄后才能下訂單哦!
復制集是額外的數據副本,是跨多個服務器同步數據的過程,復制集提供了冗余并增加了數據可用性,通過復制集可以對硬件故障和中斷的服務進行恢復。
復制集主從同步的原理和mysql類似,主節點記錄在其上的所有操作到oplog中,從節點定期輪詢主節點獲取這些操作,然后對自己的數據副本執行這些操作,從而保證從節點的數據與主節點一致。
復制集的優勢如下:
(1)讓數據更安全
(2)高數據可用性(24*7)
(3)災難恢復
(4)無停機維護(如備份、索引重建、故障轉移)
(5)讀縮放(額外的副本讀取)
(6)副本集對應用程序是透明。
1.N 個節點的集群
2.任何節點可作為主節點
3.所有寫入操作都在主節點上
4.自動故障轉移
5.自動恢復
首先配置網絡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
[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
[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 #設置復制集名稱
cp /etc/mongod.conf /etc/mongod2.conf
cp /etc/mongod2.conf /etc/mongod3.conf
cp /etc/mongod2.conf /etc/mongod4.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
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
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
[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.7true: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)
},
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。