1. 程式人生 > 資料庫 >詳解mongodb搭建Replica Set的方法

詳解mongodb搭建Replica Set的方法

1.建立資料資料夾:

mkdir -p /data/master  
mkdir -p /data/slaver  
mkdir -p /data/arbiter

效果:

data 資料夾包含 arbiter master slaver 三個資料夾

2.建立日誌存放檔案

vi /log/master.log
vi /log/slaver.log
vi /log/arbiter.log

效果:

log資料夾包含 master.log slaver.log arbiter.log 三個檔案(注意,data資料夾和lon資料夾均無上級資料夾,可自行建立不同名稱不同位置的資料夾,注意路徑與下文中的配置檔案一致即可)

3.建立配置檔案

在第一步建立的三個檔案中建立 資料夾同名.conf 字尾檔案,即:master資料夾中應有 master.conf 檔案,slaver資料夾中應有 slaver.conf檔案,arbiter資料夾中應有 arbiter.conf檔案。

各配置檔案內容如下:

master.conf

dbpath =/data/master
logpath = /log/master.log
pidfilepath =/data/ master.pid
directoryperdb = true
logappend = true
replSet = away
bind_ip = localhost
port = 27018
#fork = true

slaver.conf

dbpath =/data/slaver
logpath =/log/slaver.log
pidfilepath = /data/slaver.pid
directoryperdb = true
logappend = true
replSet = away
bind_ip = localhost
port = 27019
#fork = true

arbiter.conf

dbpath = /data/arbiter
logpath = /log/arbiter.log
pidfilepath = arbiter.pid
directoryperdb = true
logappend = true
replSet = away
bind_ip = localhost
port = 27020
#fork = true

replSet、bind_ip、port三個屬性可根據自己情況進行更改。

屬性大致解釋如下:

dbpath:資料存放目錄

logpath:日誌存放路徑

pidfilepath:程序檔案,方便停止mongodb

directoryperdb:為每一個數據庫按照資料庫名建立資料夾存放

logappend:以追加的方式記錄日誌

replSet:replica set的名字

bind_ip:mongodb所繫結的ip地址

port:mongodb程序所使用的埠號,預設為27017

oplogSize:mongodb操作日誌檔案的最大大小。單位為Mb,預設為硬碟剩餘空間的5%

fork:以後臺方式執行程序

noprealloc:不預先分配儲存

4.啟動mongod程式

mongod --config <配置路徑>

例如:

lhd@lhd:~$ sudo mongod --config /data/master/master.conf
[sudo] lhd 的密碼:

輸入密碼即可,此出應注意啟動許可權。

5.主從配置

1).啟動mongo客戶端:

mongo localhost:27018

執行結果如下:

mongo localhost:27018
MongoDB shell version v4.4.2
connecting to: mongodb://localhost:27018/test?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("0078e025-5485-4967-85c8-160755ac3d58") }
MongoDB server version: 4.4.2
---
The server generated these startup warnings when booting:
2020-12-22T09:39:40.347+08:00: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem
2020-12-22T09:39:41.093+08:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
2020-12-22T09:39:41.094+08:00: You are running this process as the root user,which is not recommended
2020-12-22T09:39:41.095+08:00: Soft rlimits too low
2020-12-22T09:39:41.095+08:00: currentValue: 1024
2020-12-22T09:39:41.095+08:00: recommendedMinimum: 64000
---
---
Enable MongoDB's free cloud-based monitoring service,which will then receive and display
metrics about your deployment (disk utilization,CPU,operation statistics,etc).

The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.

To enable free monitoring,run the following command: db.enableFreeMonitoring()
To permanently disable this reminder,run the following command: db.disableFreeMonitoring()
---

2).設定主,從,仲裁點

use admin
switched to db admin
zjd={_id:"one",members:[{_id:0,host:"localhost:27018",priority:2},{_id:1,host:"localhost:27019",priority:1},{_id:2,host:"localhost:27020",arbiterOnly:true}]};

zjd是可以任意的名字,不要用mongodb的關鍵字,conf,config都可以。

第一個_id表示replica set的名字,這個資料必須和第三步配置檔案中的replica set一致,不然會報錯。

members裡包含的是所有節點的地址以及優先順序,優先順序最高的即成為主節點,值為0則不會參加選舉成為主節點,對於仲裁節點,需要有個特別的配置——arbiterOnly:true。這個千萬不能少了,不然主備模式就不能生效。

配置的生效時間根據不同的機器配置會有長有短,配置不錯的話基本上十幾秒內就能生效,有的配置需要一兩分鐘。

3).使配置生效

rs.initiate(zjd)

顯示:

{
	"operationTime" : Timestamp(0,0),"ok" : 0,"errmsg" : "Rejecting initiate with a set name that differs from command line set name,initiate set name: one,command line set name: away","code" : 93,"codeName" : "InvalidReplicaSetConfig","$clusterTime" : {
		"clusterTime" : Timestamp(0,"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),"keyId" : NumberLong(0)
		}
	}
}

4).檢視狀態

> rs.status()

顯示:

{
	"set" : "away","date" : ISODate("2020-12-22T02:07:27.058Z"),"myState" : 2,"term" : NumberLong(0),"syncSourceHost" : "","syncSourceId" : -1,"heartbeatIntervalMillis" : NumberLong(2000),"majorityVoteCount" : 2,"writeMajorityCount" : 2,"votingMembersCount" : 3,"writableVotingMembersCount" : 2,"optimes" : {
		"lastCommittedOpTime" : {
			"ts" : Timestamp(0,"t" : NumberLong(-1)
		},"lastCommittedWallTime" : ISODate("1970-01-01T00:00:00Z"),"appliedOpTime" : {
			"ts" : Timestamp(1608602837,1),"durableOpTime" : {
			"ts" : Timestamp(1608602837,"lastAppliedWallTime" : ISODate("2020-12-22T02:07:17.467Z"),"lastDurableWallTime" : ISODate("2020-12-22T02:07:17.467Z")
	},"lastStableRecoveryTimestamp" : Timestamp(0,"members" : [
		{
			"_id" : 0,"name" : "localhost:27018","health" : 1,"state" : 2,"stateStr" : "SECONDARY","uptime" : 1667,"optime" : {
				"ts" : Timestamp(1608602837,"t" : NumberLong(-1)
			},"optimeDate" : ISODate("2020-12-22T02:07:17Z"),"infoMessage" : "Could not find member to sync from","configVersion" : 1,"configTerm" : 0,"self" : true,"lastHeartbeatMessage" : ""
		},{
			"_id" : 1,"name" : "localhost:27019","uptime" : 9,"optimeDurable" : {
				"ts" : Timestamp(1608602837,"optimeDurableDate" : ISODate("2020-12-22T02:07:17Z"),"lastHeartbeat" : ISODate("2020-12-22T02:07:26.714Z"),"lastHeartbeatRecv" : ISODate("2020-12-22T02:07:26.768Z"),"pingMs" : NumberLong(0),"lastHeartbeatMessage" : "","infoMessage" : "","configTerm" : 0
		},{
			"_id" : 2,"name" : "localhost:27020","state" : 7,"stateStr" : "ARBITER","lastHeartbeat" : ISODate("2020-12-22T02:07:26.713Z"),"lastHeartbeatRecv" : ISODate("2020-12-22T02:07:25.991Z"),"configTerm" : 0
		}
	],"ok" : 1,"$clusterTime" : {
		"clusterTime" : Timestamp(1608602837,"keyId" : NumberLong(0)
		}
	},"operationTime" : Timestamp(1608602837,1)
}

配置完成!

到此這篇關於mongodb搭建Replica Set的方法的文章就介紹到這了,更多相關mongodb搭建Replica Set內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!