成都創(chuàng)新互聯(lián)公司主營墾利網(wǎng)站建設的網(wǎng)絡公司,主營網(wǎng)站建設方案,APP應用開發(fā),墾利h5微信平臺小程序開發(fā)搭建,墾利網(wǎng)站營銷推廣歡迎墾利等地區(qū)企業(yè)咨詢復制集是額外的數(shù)據(jù)副本,是跨多個服務器同步數(shù)據(jù)的過程,復制集提供了冗余并增加了數(shù)據(jù)可用性,通過復制集可以對硬件故障和中斷的服務進行恢復。
復制集主從同步的原理和mysql類似,主節(jié)點記錄在其上的所有操作到oplog中,從節(jié)點定期輪詢主節(jié)點獲取這些操作,然后對自己的數(shù)據(jù)副本執(zhí)行這些操作,從而保證從節(jié)點的數(shù)據(jù)與主節(jié)點一致。
復制集的優(yōu)勢如下:
(1)讓數(shù)據(jù)更安全
(2)高數(shù)據(jù)可用性(24*7)
(3)災難恢復
(4)無停機維護(如備份、索引重建、故障轉(zhuǎn)移)
(5)讀縮放(額外的副本讀?。?/h3>
(6)副本集對應用程序是透明。
1.N 個節(jié)點的集群
2.任何節(jié)點可作為主節(jié)點
3.所有寫入操作都在主節(jié)點上
4.自動故障轉(zhuǎn)移
5.自動恢復
首先配置網(wǎng)絡YUM源,baseurl(下載路徑)指定為mongodb官網(wǎng)提供的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 #監(jiān)聽任意地址#security:
#operationProfiling:
replication: #去掉前面的“#”注釋,開啟該參數(shù)設置
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進程狀態(tài)
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() //查看復制集狀態(tài),提示復制集還未配置
{
"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初始化參數(shù)
{
"_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() //再次查看復制集的狀態(tài)信息
{
"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", //此節(jié)點成為主節(jié)點
"uptime" : 1371,
"optime" : {
"ts" : Timestamp(1537025984, 1),
"t" : NumberLong(1)
},....................
{
"_id" : 1,
"name" : "192.168.195.137:27018",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY", //此節(jié)點為從節(jié)點
"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", //此節(jié)點為從節(jié)點
"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 //關(guān)閉主節(jié)點服務
killing process with pid: 93576
[root@localhost ~]# mongo --port 27018 //進入其中一個從節(jié)點
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", //原來的主節(jié)點健康值為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", //此節(jié)點切換成主節(jié)點
"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 #開啟剛才關(guān)閉的端口為27017的節(jié)點實例
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 #進入主節(jié)點服務器實例
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) //交出主節(jié)點位置,維持從節(jié)點狀態(tài)不少于60秒,等待30秒使主節(jié)點和從節(jié)點日志同步
true:SECONDARY> rs.status() //可以看到本實例已經(jīng)切換成從節(jié)點
................
"members" : [
{
"_id" : 0,
"name" : "192.168.195.137:27017", // 端口27017的節(jié)點變成了主節(jié)點
"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的節(jié)點變成了從節(jié)點"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)
},
另外有需要云服務器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。