MongoDB 搭建複製集

森林森發表於2020-09-27

下載

https://blog.csdn.net/ko0491/article/details/108764916

建立3個配置檔案

  • mongo_27017.conf
#資料目錄
dbpath=/data/mongo/data/server1
#埠號
port=27017
bind_ip=0.0.0.0
#以後臺執行
fork=true
# 日誌路徑
logpath = /data/mongo/logs/server1.log
# 追加
logappend = true
# 授權
auth=false
#複製集
replSet=myCluster
  • mongo_27018.conf
#資料目錄
dbpath=/data/mongo/data/server2
#埠號
port=27018
bind_ip=0.0.0.0
#以後臺執行
fork=true
# 日誌路徑
logpath = /data/mongo/logs/server2.log
# 追加
logappend = true
# 授權
auth=false
#複製集
replSet=myCluster
  • mongo_27019.conf
#資料目錄
dbpath=/data/mongo/data/server3
#埠號
port=27019
bind_ip=0.0.0.0
#以後臺執行
fork=true
# 日誌路徑
logpath = /data/mongo/logs/server3.log
# 追加
logappend = true
# 授權
auth=false
#複製集
replSet=myCluster

在data目錄下建立相就的目錄

分別啟動


./mongod -f ../mongo_27017.conf
./mongod -f ../mongo_27018.conf
./mongod -f ../mongo_27019.conf

分別連線

./mongo --host=192.168.181.135 --port=27017
./mongo --host=192.168.181.135 --port=27018
./mongo --host=192.168.181.135 --port=27019

在任意一個節點

rs.initiate({
    _id: "myCluster",
    members: [{
        _id: 0,
        host: "192.168.181.135:27017",
        "priority":10
        
    },{
        _id: 1,
        host: "192.168.181.135:27018"
    },{
        _id: 2,
        host: "192.168.181.135:27019"
    }]
})


  • 檢視狀態
    每個節點
    rs.status()
rs.status()

myCluster:PRIMARY> rs.status()
{
	"set" : "myCluster",
	"date" : ISODate("2020-09-27T10:19:14.279Z"),
	"myState" : 1,
	"term" : NumberLong(1),
	"syncingTo" : "",
	"syncSourceHost" : "",
	"syncSourceId" : -1,
	"heartbeatIntervalMillis" : NumberLong(2000),
	"optimes" : {
		"lastCommittedOpTime" : {
			"ts" : Timestamp(1601201950, 1),
			"t" : NumberLong(1)
		},
		"readConcernMajorityOpTime" : {
			"ts" : Timestamp(1601201950, 1),
			"t" : NumberLong(1)
		},
		"appliedOpTime" : {
			"ts" : Timestamp(1601201950, 1),
			"t" : NumberLong(1)
		},
		"durableOpTime" : {
			"ts" : Timestamp(1601201950, 1),
			"t" : NumberLong(1)
		}
	},
	"lastStableRecoveryTimestamp" : Timestamp(1601201950, 1),
	"lastStableCheckpointTimestamp" : Timestamp(1601201950, 1),
	"members" : [
		{
			"_id" : 0,
			"name" : "192.168.181.135:27017",
			"health" : 1,
			"state" : 1,
			"stateStr" : "PRIMARY",
			"uptime" : 988,
			"optime" : {
				"ts" : Timestamp(1601201950, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2020-09-27T10:19:10Z"),
			"syncingTo" : "",
			"syncSourceHost" : "",
			"syncSourceId" : -1,
			"infoMessage" : "",
			"electionTime" : Timestamp(1601201409, 1),
			"electionDate" : ISODate("2020-09-27T10:10:09Z"),
			"configVersion" : 1,
			"self" : true,
			"lastHeartbeatMessage" : ""
		},
		{
			"_id" : 1,
			"name" : "192.168.181.135:27018",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 555,
			"optime" : {
				"ts" : Timestamp(1601201950, 1),
				"t" : NumberLong(1)
			},
			"optimeDurable" : {
				"ts" : Timestamp(1601201950, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2020-09-27T10:19:10Z"),
			"optimeDurableDate" : ISODate("2020-09-27T10:19:10Z"),
			"lastHeartbeat" : ISODate("2020-09-27T10:19:13.397Z"),
			"lastHeartbeatRecv" : ISODate("2020-09-27T10:19:13.653Z"),
			"pingMs" : NumberLong(0),
			"lastHeartbeatMessage" : "",
			"syncingTo" : "192.168.181.135:27017",
			"syncSourceHost" : "192.168.181.135:27017",
			"syncSourceId" : 0,
			"infoMessage" : "",
			"configVersion" : 1
		},
		{
			"_id" : 2,
			"name" : "192.168.181.135:27019",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 555,
			"optime" : {
				"ts" : Timestamp(1601201950, 1),
				"t" : NumberLong(1)
			},
			"optimeDurable" : {
				"ts" : Timestamp(1601201950, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2020-09-27T10:19:10Z"),
			"optimeDurableDate" : ISODate("2020-09-27T10:19:10Z"),
			"lastHeartbeat" : ISODate("2020-09-27T10:19:13.397Z"),
			"lastHeartbeatRecv" : ISODate("2020-09-27T10:19:13.653Z"),
			"pingMs" : NumberLong(0),
			"lastHeartbeatMessage" : "",
			"syncingTo" : "192.168.181.135:27017",
			"syncSourceHost" : "192.168.181.135:27017",
			"syncSourceId" : 0,
			"infoMessage" : "",
			"configVersion" : 1
		}
	],
	"ok" : 1,
	"$clusterTime" : {
		"clusterTime" : Timestamp(1601201950, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	},
	"operationTime" : Timestamp(1601201950, 1)
}
myCluster:PRIMARY> 

27017
在這裡插入圖片描述
27018

在這裡插入圖片描述
27019
在這裡插入圖片描述

在這裡插入圖片描述

節點的動態增刪

增加節點
rs.add("192.168.181.135:37019")
刪除slave 節點
rs.remove("192.168.181.135:37019")

調整複製集配置

var conf = rs.conf()
// 將0號節點的優先順序調整為10
conf.members[0].priority = 10;
// 將1號節點調整為hidden節點
conf.members[1].hidden = true;
// hidden節點必須配置{priority: 0}
conf.members[1].priority = 0;
// 應用以上調整
rs.reconfig(conf);

在這裡插入圖片描述

其它配置

# /usr/local/mongodb/mongodb.conf
systemLog:
  destination: file
  path: /usr/local/mongodb/log/mongodb.log # log path
  logAppend: true
storage:
  dbPath: /usr/local/mongodb/data/ # data directory
net:
  bindIp: 0.0.0.0
  port: 27017 # port
  maxIncomingConnections: 500
replication:
  enableMajorityReadConcern: true
  replSetName: rs0
processManagement:
  fork: true
  pidFilePath: /usr/local/mongodb/run/mongodb.pid

測試

myCluster:PRIMARY> use local
switched to db local
myCluster:PRIMARY> show tables
oplog.rs
replset.election
replset.minvalid
replset.oplogTruncateAfterPoint
startup_log

在這裡插入圖片描述

  • 插入資料
myCluster:PRIMARY> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
myCluster:PRIMARY> use myresume
switched to db myresume
myCluster:PRIMARY> db.my_test.insert({name:'zhangsan',age:18})
WriteResult({ "nInserted" : 1 })
myCluster:PRIMARY> db.my_test.find()
{ "_id" : ObjectId("5f706974ada3cc994b4d8e41"), "name" : "zhangsan", "age" : 18 }
myCluster:PRIMARY> show tables;
my_test

從庫檢視

rs.slaveOk()

不然不能查

檢視從節點,都有

  • 從節點插入失敗
myCluster:SECONDARY> db.my_test.insert({name:"lisi",age:33})
WriteCommandError({
	"operationTime" : Timestamp(1601203170, 1),
	"ok" : 0,
	"errmsg" : "not master",
	"code" : 10107,
	"codeName" : "NotMaster",
	"$clusterTime" : {
		"clusterTime" : Timestamp(1601203170, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
})
myCluster:SECONDARY> 


有仲裁節點複製集搭建

_id 整數 _id:0 複製集中的標示
host 字串 host:“主機:埠” 節點主機名
arbiterOnly 布林值 arbiterOnly:true 是否為仲裁(裁判)節點
priority(權重)
整數 priority=0|1 預設1,是否有資格變成主節點,取值範圍0-1000,0永遠不
會變成主節點
hidden 布林值 hidden=true|false,0|1 隱藏,權重必須為0,才可以設定
votes 整數 votes= 0|1 投票,是否為投票節點,0 不投票,1投票
slaveDelay 整數 slaveDelay=3600 從庫的延遲多少秒
buildIndexes 布林值 buildIndexes=true|false,0|1 主庫的索引,從庫也建立,_id索引無效

和上面的配置步驟相同 只是增加了 一個特殊的仲裁節點

注入節點 執行 rs.addArb(“IP:埠”);
rs.addArb(“192.168.181.135:27020”)

或者重新配置

只能在主節點下進行

var cfg ={
 _id: "myCluster",
 "protocolVersion" : 1,
    members: [{
        _id: 0,
        host: "192.168.181.135:27017",
		priority:10
    },{
        _id: 1,
        host: "192.168.181.135:27018",
		priority:1
    },{
        _id: 2,
        host: "192.168.181.135:27019",
		priority:5
    },{
        _id: 3,
        host: "192.168.181.135:27020",
		arbiterOnly:true
	}]
}
rs.reconfig(cfg)

  • 檢視
myCluster:PRIMARY> rs.status()
{
	"set" : "myCluster",
	"date" : ISODate("2020-09-27T11:12:20.987Z"),
	"myState" : 1,
	"term" : NumberLong(1),
	"syncingTo" : "",
	"syncSourceHost" : "",
	"syncSourceId" : -1,
	"heartbeatIntervalMillis" : NumberLong(2000),
	"optimes" : {
		"lastCommittedOpTime" : {
			"ts" : Timestamp(1601205137, 1),
			"t" : NumberLong(1)
		},
		"readConcernMajorityOpTime" : {
			"ts" : Timestamp(1601205137, 1),
			"t" : NumberLong(1)
		},
		"appliedOpTime" : {
			"ts" : Timestamp(1601205137, 1),
			"t" : NumberLong(1)
		},
		"durableOpTime" : {
			"ts" : Timestamp(1601205137, 1),
			"t" : NumberLong(1)
		}
	},
	"lastStableRecoveryTimestamp" : Timestamp(1601205131, 1),
	"lastStableCheckpointTimestamp" : Timestamp(1601205131, 1),
	"members" : [
		{
			"_id" : 0,
			"name" : "192.168.181.135:27017",
			"health" : 1,
			"state" : 1,
			"stateStr" : "PRIMARY",
			"uptime" : 4174,
			"optime" : {
				"ts" : Timestamp(1601205137, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2020-09-27T11:12:17Z"),
			"syncingTo" : "",
			"syncSourceHost" : "",
			"syncSourceId" : -1,
			"infoMessage" : "",
			"electionTime" : Timestamp(1601201409, 1),
			"electionDate" : ISODate("2020-09-27T10:10:09Z"),
			"configVersion" : 4,
			"self" : true,
			"lastHeartbeatMessage" : ""
		},
		{
			"_id" : 1,
			"name" : "192.168.181.135:27018",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 3742,
			"optime" : {
				"ts" : Timestamp(1601205137, 1),
				"t" : NumberLong(1)
			},
			"optimeDurable" : {
				"ts" : Timestamp(1601205137, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2020-09-27T11:12:17Z"),
			"optimeDurableDate" : ISODate("2020-09-27T11:12:17Z"),
			"lastHeartbeat" : ISODate("2020-09-27T11:12:19.912Z"),
			"lastHeartbeatRecv" : ISODate("2020-09-27T11:12:19.918Z"),
			"pingMs" : NumberLong(0),
			"lastHeartbeatMessage" : "",
			"syncingTo" : "192.168.181.135:27017",
			"syncSourceHost" : "192.168.181.135:27017",
			"syncSourceId" : 0,
			"infoMessage" : "",
			"configVersion" : 4
		},
		{
			"_id" : 2,
			"name" : "192.168.181.135:27019",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 3742,
			"optime" : {
				"ts" : Timestamp(1601205137, 1),
				"t" : NumberLong(1)
			},
			"optimeDurable" : {
				"ts" : Timestamp(1601205137, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2020-09-27T11:12:17Z"),
			"optimeDurableDate" : ISODate("2020-09-27T11:12:17Z"),
			"lastHeartbeat" : ISODate("2020-09-27T11:12:19.912Z"),
			"lastHeartbeatRecv" : ISODate("2020-09-27T11:12:19.922Z"),
			"pingMs" : NumberLong(0),
			"lastHeartbeatMessage" : "",
			"syncingTo" : "192.168.181.135:27017",
			"syncSourceHost" : "192.168.181.135:27017",
			"syncSourceId" : 0,
			"infoMessage" : "",
			"configVersion" : 4
		},
		{
			"_id" : 3,
			"name" : "192.168.181.135:27020",
			"health" : 1,
			"state" : 7,
			"stateStr" : "ARBITER",
			"uptime" : 3,
			"lastHeartbeat" : ISODate("2020-09-27T11:12:19.914Z"),
			"lastHeartbeatRecv" : ISODate("2020-09-27T11:12:19.938Z"),
			"pingMs" : NumberLong(1),
			"lastHeartbeatMessage" : "",
			"syncingTo" : "",
			"syncSourceHost" : "",
			"syncSourceId" : -1,
			"infoMessage" : "",
			"configVersion" : 4
		}
	],
	"ok" : 1,
	"$clusterTime" : {
		"clusterTime" : Timestamp(1601205137, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	},
	"operationTime" : Timestamp(1601205137, 1)
}

相關文章