詳解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 <配置路徑>

例如:

[email protected]:~$ 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, 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, 0),
			"t" : NumberLong(-1)
		},
		"lastCommittedWallTime" : ISODate("1970-01-01T00:00:00Z"),
		"appliedOpTime" : {
			"ts" : Timestamp(1608602837, 1),
			"t" : NumberLong(-1)
		},
		"durableOpTime" : {
			"ts" : Timestamp(1608602837, 1),
			"t" : NumberLong(-1)
		},
		"lastAppliedWallTime" : ISODate("2020-12-22T02:07:17.467Z"),
		"lastDurableWallTime" : ISODate("2020-12-22T02:07:17.467Z")
	},
	"lastStableRecoveryTimestamp" : Timestamp(0, 0),
	"members" : [
		{
			"_id" : 0,
			"name" : "localhost:27018",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 1667,
			"optime" : {
				"ts" : Timestamp(1608602837, 1),
				"t" : NumberLong(-1)
			},
			"optimeDate" : ISODate("2020-12-22T02:07:17Z"),
			"syncSourceHost" : "",
			"syncSourceId" : -1,
			"infoMessage" : "Could not find member to sync from",
			"configVersion" : 1,
			"configTerm" : 0,
			"self" : true,
			"lastHeartbeatMessage" : ""
		},
		{
			"_id" : 1,
			"name" : "localhost:27019",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 9,
			"optime" : {
				"ts" : Timestamp(1608602837, 1),
				"t" : NumberLong(-1)
			},
			"optimeDurable" : {
				"ts" : Timestamp(1608602837, 1),
				"t" : NumberLong(-1)
			},
			"optimeDate" : ISODate("2020-12-22T02:07:17Z"),
			"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" : "",
			"syncSourceHost" : "",
			"syncSourceId" : -1,
			"infoMessage" : "",
			"configVersion" : 1,
			"configTerm" : 0
		},
		{
			"_id" : 2,
			"name" : "localhost:27020",
			"health" : 1,
			"state" : 7,
			"stateStr" : "ARBITER",
			"uptime" : 9,
			"lastHeartbeat" : ISODate("2020-12-22T02:07:26.713Z"),
			"lastHeartbeatRecv" : ISODate("2020-12-22T02:07:25.991Z"),
			"pingMs" : NumberLong(0),
			"lastHeartbeatMessage" : "",
			"syncSourceHost" : "",
			"syncSourceId" : -1,
			"infoMessage" : "",
			"configVersion" : 1,
			"configTerm" : 0
		}
	],
	"ok" : 1,
	"$clusterTime" : {
		"clusterTime" : Timestamp(1608602837, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	},
	"operationTime" : Timestamp(1608602837, 1)
}

配置完成!

到此這篇關於mongodb搭建Replica Set的方法的文章就介紹到這瞭,更多相關mongodb搭建Replica Set內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: