mongodb副本集介紹、mongodb副本集搭建、 mongodb副本集測試

mongodb副本集介紹

  • MongoDB早期版本使用master-slave,一主一從和MySQL主從基本是一致的,但salve在此架構中爲只讀,當主庫宕機後,從庫不能自動切換爲主.
  • 目前已經淘汰master-salve模式,改成副本集,副本集架構也是爲了實現MongoDB的高可用,這種模式下有一個主(primary),和多個從(secondary)只讀,支持他們設置權重,當主宕機後,權重最高的從切換爲主.
  • 在此架構中還能夠創建一個仲裁(arbiter)的角色,它只負責裁決,而不存儲數據.
  • 在此架構中讀寫數據都是在主上,要想實現負載均衡的目的須要手動指定讀庫的目標server.

MongoDB副本集架構圖

原理很簡單一個primary,secondary至少是一個,也能夠是多個secondary,除了多個secondary以外,還能夠加一個Arbiter,Arbiter叫作仲裁,當Primary宕機後,Arbiter能夠很準確的告知Primary宕掉了,但可能Primary認爲本身沒有宕掉,這樣的話就會出現腦裂,爲了防止腦裂就增長了Arbiter這個角色,尤爲是數據庫堅定不能出現腦裂的狀態,腦裂會致使數據會紊亂,數據一旦紊亂恢復就很是麻煩.linux

變遷圖

說明:Primary宕機後,其中secondary就成爲一個新的Primary,另一個secondary依然是secondary的角色. 對於MySQL主歷來講,即便作一主多從,萬一master宕機後,可讓從成爲新的主,但這過程是須要手動的更改的. 可是在MongoDB副本集架構當中呢,它徹底都是自動的,rimary宕機後,其中secondary就成爲一個新的Primary,另一個secondary能夠自動識別新的primary.mongodb

mongodb副本集搭建

準備三臺機器: 192.168.193.130 (primary)
                      192.168.193.131 (secondary)                    
                      192.168.193.132 (secondary)
三臺機器都須要安裝MongoDB,primary已安裝過,兩臺secondary須要安裝,因步驟同樣,在此不作演示.shell

編輯三臺機器的配置文件

[root@aminglinux-130 ~]# vim /etc/mongod.conf數據庫

# network interfaces
net:
  port: 27017
  bindIp: 127.0.0.1,192.168.193.130  # Listen to local interface only, comment to listen on all interfaces.
"/etc/mongod.conf" 44L, 784C

說明:作副本集bindIp 要監聽本機IP和內網IPvim

#replication:                 //把#去掉,並增兩行
replication:
  oplogSizeMB: 20              //前面兩個空格
  replSetName;aminglinux       //定義副本集的名字 前面兩個空格

重啓MongoDB服務:bash

[root@aminglinux-130 ~]# systemctl restart mongod架構

啓動從主機app

[root@aminglinux-131 ~]# vim /etc/mongod.conf
 負載均衡

net:
  port: 27017
  bindIp: 127.0.0.1,192.168.193.131  # Listen to local interface only, comment to listen on all interfaces.

說明:作副本集bindIp 要監聽本機IP和內網IPspa

#replication:    //把#去掉,並增兩行

replication:
  oplogSizeMB: 20            
  replSetName: aminglinux      //定義副本集的名字

重啓MongoDB服務:

[root@aminglinux-131 ~]# systemctl restart mongod
 

[root@aminglinux-131 ~]# ps aux |grep mongod
mongod     3111  2.2  3.9 1018232 39316 ?       Sl   16:23   0:21 /usr/bin/mongod -f /etc/mongod.conf
root       3282  0.0  0.0 112704   660 pts/0    R+   16:39   0:00 grep --color=auto mongod

啓動從主機

[root@aminglinux-132 ~]# vim /etc/mongod.conf

#replication:    //把#去掉,並增兩行

replication:
  oplogSizeMB: 20            
  replSetName: aminglinux      //定義副本集的名字

[root@aminglinux-132 ~]# systemctl restart mongod
 

[root@aminglinux-132 ~]# ps aux |grep mongod
mongod     3246 60.0  3.7 1018232 37532 ?       Sl   16:44   0:42 /usr/bin/mongod -f /etc/mongod.conf
root       3292  0.0  0.0 112720   980 pts/0    R+   16:45   0:00 grep --color=auto mongod

鏈接MongoDB --Primary機器

[root@aming-130 ~]# mongo
MongoDB shell version v3.4.9
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.9
Server has startup warnings: 
2017-10-20T17:18:53.740+0800 I CONTROL  [initandlisten] 
2017-10-20T17:18:53.740+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2017-10-20T17:18:53.740+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2017-10-20T17:18:53.740+0800 I CONTROL  [initandlisten] 
2017-10-20T17:18:53.740+0800 I CONTROL  [initandlisten] 
2017-10-20T17:18:53.740+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2017-10-20T17:18:53.740+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2017-10-20T17:18:53.740+0800 I CONTROL  [initandlisten] 
2017-10-20T17:18:53.740+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2017-10-20T17:18:53.740+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2017-10-20T17:18:53.740+0800 I CONTROL  [initandlisten]

置副本集

說明:在哪臺機器上執行這一步,那麼哪臺機器就會成爲primary

>  config={_id:"aminglinux",members:[{_id:0,host:"192.168.193.130:27017"},{_id:1,host:"192.168.193.131:27017"},{_id:2,host:"192.168.193.132:27017"}]}
{
	"_id" : "aminglinux",
	"members" : [
		{
			"_id" : 0,
			"host" : "192.168.193.130:27017"
		},
		{
			"_id" : 1,
			"host" : "192.168.193.131:27017"
		},
		{
			"_id" : 2,
			"host" : "192.168.193.132:27017"
		}
	]
}
> 
config={_id:"aminglinux" --> aminglinux(副本集的名字)
members --> 指定成員

rs.initiate(config) -->初始化 

> rs.initiate()?
{ "ok" : 1 }

rs.status() -->查看副本集狀態

說明:能夠看到192.168.193.130 顯示:"stateStr" : "PRIMARY"
          192.168.193.131和192.168.193.132 分別顯示: "stateStr" : "SECONDARY"

aminglinux:OTHER> rs.status()
{
	"set" : "aminglinux",
	"date" : ISODate("2019-05-14T10:54:40.893Z"),
	"myState" : 1,
	"term" : NumberLong(1),
	"syncingTo" : "",
	"syncSourceHost" : "",
	"syncSourceId" : -1,
	"heartbeatIntervalMillis" : NumberLong(2000),
	"optimes" : {
		"lastCommittedOpTime" : {
			"ts" : Timestamp(1557831277, 1),
			"t" : NumberLong(1)
		},
		"appliedOpTime" : {
			"ts" : Timestamp(1557831277, 1),
			"t" : NumberLong(1)
		},
		"durableOpTime" : {
			"ts" : Timestamp(1557831277, 1),
			"t" : NumberLong(1)
		}
	},
	"members" : [
		{
			"_id" : 0,
			"name" : "192.168.193.130:27017",
			"health" : 1,
			"state" : 1,
			"stateStr" : "PRIMARY",
			"uptime" : 319,
			"optime" : {
				"ts" : Timestamp(1557831277, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2019-05-14T10:54:37Z"),
			"syncingTo" : "",
			"syncSourceHost" : "",
			"syncSourceId" : -1,
			"infoMessage" : "could not find member to sync from",
			"electionTime" : Timestamp(1557831175, 2),
			"electionDate" : ISODate("2019-05-14T10:52:55Z"),
			"configVersion" : 1,
			"self" : true,
			"lastHeartbeatMessage" : ""
		}
	],
	"ok" : 1
}
annalinux:PRIMARY> rs.status()
{
	"set" : "annalinux",
	"date" : ISODate("2019-05-14T11:05:12.553Z"),
	"myState" : 1,
	"term" : NumberLong(1),
	"syncingTo" : "",
	"syncSourceHost" : "",
	"syncSourceId" : -1,
	"heartbeatIntervalMillis" : NumberLong(2000),
	"optimes" : {
		"lastCommittedOpTime" : {
			"ts" : Timestamp(1557831909, 1),
			"t" : NumberLong(1)
		},
		"appliedOpTime" : {
			"ts" : Timestamp(1557831909, 1),
			"t" : NumberLong(1)
		},
		"durableOpTime" : {
			"ts" : Timestamp(1557831909, 1),
			"t" : NumberLong(1)
		}
	},
	"members" : [
		{
			"_id" : 0,
			"name" : "192.168.193.131:27017",
			"health" : 1,
			"state" : 1,
			"stateStr" : "PRIMARY",
			"uptime" : 953,
			"optime" : {
				"ts" : Timestamp(1557831909, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2019-05-14T11:05:09Z"),
			"syncingTo" : "",
			"syncSourceHost" : "",
			"syncSourceId" : -1,
			"infoMessage" : "",
			"electionTime" : Timestamp(1557831527, 2),
			"electionDate" : ISODate("2019-05-14T10:58:47Z"),
			"configVersion" : 1,
			"self" : true,
			"lastHeartbeatMessage" : ""
		}
	],
	"ok" : 1
}
annalinux:OTHER> rs.status()
{
	"set" : "annalinux",
	"date" : ISODate("2019-05-14T11:06:48.154Z"),
	"myState" : 1,
	"term" : NumberLong(1),
	"syncingTo" : "",
	"syncSourceHost" : "",
	"syncSourceId" : -1,
	"heartbeatIntervalMillis" : NumberLong(2000),
	"optimes" : {
		"lastCommittedOpTime" : {
			"ts" : Timestamp(1557832006, 1),
			"t" : NumberLong(1)
		},
		"appliedOpTime" : {
			"ts" : Timestamp(1557832006, 1),
			"t" : NumberLong(1)
		},
		"durableOpTime" : {
			"ts" : Timestamp(1557832006, 1),
			"t" : NumberLong(1)
		}
	},
	"members" : [
		{
			"_id" : 0,
			"name" : "192.168.193.132:27017",
			"health" : 1,
			"state" : 1,
			"stateStr" : "PRIMARY",
			"uptime" : 654,
			"optime" : {
				"ts" : Timestamp(1557832006, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2019-05-14T11:06:46Z"),
			"syncingTo" : "",
			"syncSourceHost" : "",
			"syncSourceId" : -1,
			"infoMessage" : "could not find member to sync from",
			"electionTime" : Timestamp(1557832004, 2),
			"electionDate" : ISODate("2019-05-14T11:06:44Z"),
			"configVersion" : 1,
			"self" : true,
			"lastHeartbeatMessage" : ""
		}
	],
	"ok" : 1
}
相關文章
相關標籤/搜索