您好,登錄后才能下訂單哦!
復制是基于操作日志oplog,相當于MySQL中的二進制日志,只記錄發生改變的記錄。復制是將主節點的oplog日志同步并應用到其他從節點過程
節點類型分為標準(host)節點 、被動(passive)節點和仲裁(arbiter)節點。
(1)只有標準節點可能被選舉為主(primary)節點,有選舉權;被動節點有完整副本,只能作為復制集保存,不可能成為主節點,沒有選舉權;仲裁節點不存放數據,只負責投票選舉,不可能成為主節點,不存放數據,依然沒有選舉權
(2)標準節點與被動節點的區別:priority值高者是標準節點,低者則為被動節點
(3)選舉規則是票數高者獲勝,priority是優先權為0~1000的值,相當于額外增加0~1000的票數。選舉結果:票數高者獲勝;若票數相同,數據新者獲勝
首先配置網絡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> cfg={"_id":"true","members":[{"_id":0,"host":"192.168.195.137:27017","priority":100},???????????????????????
{"_id":1,"host":"192.168.195.137:27018","priority":100},{"_id":2,"host":"192.168.195.137:27019","priority":0},{"_id":3,"host":"192.168.195.137:27020","arbiterOnly":true}]}???????????????????????
{
??? "_id" : "true",
??? "members" : [
??????? {
??????????? "_id" : 0,
??????????? "host" : "192.168.195.137:27017",??????????????? #標準節點1,優先級為100
??????????? "priority" : 100
??????? },
??????? {
??????????? "_id" : 1,
??????????? "host" : "192.168.195.137:27018",?????????????? #標準節點2,優先級為100
??????????? "priority" : 100
??????? },
??????? {
??????????? "_id" : 2,
??????????? "host" : "192.168.195.137:27019",????????????? #被動節點,優先級為0
??????????? "priority" : 0
??????? },
??????? {
??????????? "_id" : 3,
??????????? "host" : "192.168.195.137:27020",??????????????? #仲裁節點
??????????? "arbiterOnly" : true
> rs.initiate(cfg)?????????????????????????????? #初始化配置
{
??? "ok" : 1,
??? "operationTime" : Timestamp(1537077618, 1),
??? "$clusterTime" : {
??????? "clusterTime" : Timestamp(1537077618, 1),
??????? "signature" : {
??????????? "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
??????????? "keyId" : NumberLong(0)
??????? }
??? }
true:PRIMARY> rs.isMaster()
{
??? "hosts" : [
??????? "192.168.195.137:27017",?????????????? #標準節點
??????? "192.168.195.137:27018"
??? ],
??? "passives" : [
??????? "192.168.195.137:27019"????????????? ? #被動節點
??? ],
??? "arbiters" : [
??????? "192.168.195.137:27020"????????????? #仲裁節點
??? ],
??? "setName" : "true",
??? "setVersion" : 1,
??? "ismaster" : true,
??? "secondary" : false,
??? "primary" : "192.168.195.137:27017",
??? "me" : "192.168.195.137:27017",
true:PRIMARY> use kfc
switched to db kfc
true:PRIMARY> db.info.insert({"id":1,"name":"tom"})
WriteResult({ "nInserted" : 1 })
true:PRIMARY> db.info.insert({"id":2,"name":"jack"})
WriteResult({ "nInserted" : 1 })
true:PRIMARY> db.info.find()
{ "_id" : ObjectId("5b9df3ff690f4b20fa330b18"), "id" : 1, "name" : "tom" }
{ "_id" : ObjectId("5b9df40f690f4b20fa330b19"), "id" : 2, "name" : "jacktrue:PRIMARY> db.info.update({"id":2},{$set:{"name":"lucy"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
true:PRIMARY> db.info.remove({"id":1})
WriteResult({ "nRemoved" : 1 })
?
true:PRIMARY> use local
switched to db local
true:PRIMARY> show tables
me
oplog.rs
replset.election
replset.minvalid
startup_log
system.replset
system.rollback.id
true:PRIMARY> db.oplog.rs.find()?????????????????? #查看日志記錄所有操作????
............?????????????????????????????????????????????????????? # 通過日志記錄,可以找到剛才的操作信息
{ "ts" : Timestamp(1537078271, 2), "t" : NumberLong(1), "h" : NumberLong("-5529983416084904509"), "v" : 2, "op" : "c", "ns" : "kfc.$cmd", "ui" : UUID("2de2277f-df99-4fb2-96ef-164b59dfc768"), "wall" : ISODate("2018-09-16T06:11:11.072Z"), "o" : { "create" : "info", "idIndex" : { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "kfc.info" } } }
{ "ts" : Timestamp(1537078271, 3), "t" : NumberLong(1), "h" : NumberLong("-1436300260967761649"), "v" : 2, "op" : "i", "ns" : "kfc.info", "ui" : UUID("2de2277f-df99-4fb2-96ef-164b59dfc768"), "wall" : ISODate("2018-09-16T06:11:11.072Z"), "o" : { "_id" : ObjectId("5b9df3ff690f4b20fa330b18"), "id" : 1, "name" : "tom" } }
{ "ts" : Timestamp(1537078287, 1), "t" : NumberLong(1), "h" : NumberLong("9052955074674132871"), "v" : 2, "op" : "i", "ns" : "kfc.info", "ui" : UUID("2de2277f-df99-4fb2-96ef-164b59dfc768"), "wall" : ISODate("2018-09-16T06:11:27.562Z"), "o" : { "_id" : ObjectId("5b9df40f690f4b20fa330b19"), "id" : 2, "name" : "jack" } }...............
{ "ts" : Timestamp(1537078543, 1), "t" : NumberLong(1), "h" : NumberLong("-5120962218610090442"), "v" : 2, "op" : "u", "ns" : "kfc.info", "ui" : UUID("2de2277f-df99-4fb2-96ef-164b59dfc768"), "o2" : { "_id" : ObjectId("5b9df40f690f4b20fa330b19") }, "wall" : ISODate("2018-09-16T06:15:43.494Z"), "o" : { "$v" : 1, "$set" : { "name" : "lucy" } } }
[root@localhost ~]# mongod -f /etc/mongod.conf --shutdown??????????? #關閉主節點服務
killing process with pid: 52986
[root@localhost ~]# mongo --port 27018?????????????????????????????? #登錄另一個標準節點端口 27018MongoDB 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",
??????????? "health" : 0,????????????????????????????????????????????? #健康值為 0 ,說明端口27017 已經宕機了
??????????? "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",?????????????????????????? #此時另一臺標準節點被選舉為主節點,端口為 27018
??????????? "uptime" : 3192,
??????????? "optime" : {
??????????????? "ts" : Timestamp(1537080552, 1),
??????????????? "t" : NumberLong(2)
??????????? },
[root@localhost ~]# mongod -f /etc/mongod2.conf --shutdown???????? #關閉第二個標準節點服務
killing process with pid: 53018
[root@localhost ~]# mongo --port 27019?????????????????????????? #進入第三個被動節點實例
MongoDB shell version v3.6.7
connecting to: mongodb://127.0.0.1:27019/
MongoDB server version: 3.6.7true:SECONDARY> rs.status()??????????????????????????? #查看復制集狀態信息??????????????????????
..............
"members" : [
??????? {
??????????? "_id" : 0,
??????????? "name" : "192.168.195.137:27017",
??????????? "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" : 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" : 2,
??????????? "name" : "192.168.195.137:27019",
??????????? "health" : 1,
??????????? "state" : 2,
??????????? "stateStr" : "SECONDARY",????????????????????????? #被動節點并沒有被選舉為主節點,說明被動節點不可能成為活躍節點
??????????? "uptime" : 3972,
??????????? "optime" : {
??????????????? "ts" : Timestamp(1537081303, 1),
??????????????? "t" : NumberLong(2)
??????????? },..................
{
??????? "_id" : 3,
??????? "name" : "192.168.195.137:27020",
??????? "health" : 1,
??????? "state" : 7,
??????? "stateStr" : "ARBITER",
??????? "uptime" : 3722,
另外我們可以通過啟動標準節點的先后順序,實現人為指定主節點,默認誰先啟動,誰就是主節點。
[root@localhost ~]# mongod -f /etc/mongod.conf
about to fork child process, waiting until server is ready for connections.
forked process: 54685
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: 54773
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.7true:SECONDARY> rs.slaveOk()?????????????????????????? #允許默認從節點讀取數據
true:SECONDARY> show dbs?????????????#讀取成功
admin?? 0.000GB
config? 0.000GB
kfc???? 0.000GB
local?? 0.000GB
true:SECONDARY> rs.printReplicationInfo()???????? #查看日志文件能夠使用的大小 默認oplog大小會占用64位實例5%的可用磁盤空間
configured oplog size:? 990MB
log length start to end: 5033secs (1.4hrs)
oplog first event time:? Sun Sep 16 2018 14:00:18 GMT+0800 (CST)
oplog last event time:?? Sun Sep 16 2018 15:24:11 GMT+0800 (CST)
now:???????????????????? Sun Sep 16 2018 15:24:13 GMT+0800 (CST)
true:SECONDARY> rs.printSlaveReplicationInfo()??????????????? #查看節點????????
source: 192.168.195.137:27018
??? syncedTo: Sun Sep 16 2018 15:24:21 GMT+0800 (CST)
??? 0 secs (0 hrs) behind the primary
source: 192.168.195.137:27019
??? syncedTo: Sun Sep 16 2018 15:24:21 GMT+0800 (CST)
??? 0 secs (0 hrs) behind the primary
會發現仲裁節點并不具備數據復制
true:PRIMARY> use admin
switched to db admin
true:PRIMARY> db.shutdownServer()
vim /etc/mongod.conf
port: 27027
#replication:
# replSetName: true
mongod -f /etc/mongod.conf
mongodump --port=27028 -d local -c oplog.rs -o /opt/
[root@localhost logs]# mongo --port 27027
> use local
> db.oplog.rs.drop()
> db.runCommand( { create: "oplog.rs", capped: true, size: (2 * 1024 * 1024 * 1024) } )
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。