MongoDB 搭建複製集
下載
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)
}
相關文章
- 【Mongodb】 可複製集搭建MongoDB
- MongoDB搭建Replica Set複製集MongoDB
- MongoDB學習4:MongoDB複製集機制和原理,搭建複製集MongoDB
- mongodb6.0.13 搭建複製集PSAMongoDB
- mongodb複製集(replica set)搭建及管理MongoDB
- mongodb複製集MongoDB
- mongodb複製集(replica sets)+分片(sharding)環境搭建MongoDB
- 【Mongodb】往分片複製集新增複製成員MongoDB
- MongoDB學習之複製集MongoDB
- MongoDB複製集資料同步流程MongoDB
- 02 . MongoDB複製集,分片集,備份與恢復MongoDB
- MongoDB原理:複製集狀態同步機制MongoDB
- MongoDB日常運維-03主從複製搭建MongoDB運維
- 【Mongodb】分片複製集環境新增新的分片MongoDB
- 和麵試官這樣吹MongoDB 複製集!MongoDB
- Mongodb 配置複製集,並啟用安全校驗MongoDB
- Redis搭建主從複製、哨兵叢集Redis
- MongoDB 複製機制MongoDB
- Docker Compose搭建MySQL主從複製叢集DockerMySql
- 搭建MongoDB分片叢集MongoDB
- MongoDB 分片叢集搭建MongoDB
- Docker 搭建叢集 MongoDBDockerMongoDB
- 輕鬆掌握元件啟動之MongoDB(番外篇):高可用複製集架構環境搭建-mtools元件MongoDB架構
- MongoDB系列-解決面試中可能遇到的MongoDB複製集(replica set)問題MongoDB面試
- MongoDB高可用叢集搭建MongoDB
- MongoDB 重新同步複製整合員MongoDB
- MongoDB Sharding(二) -- 搭建分片叢集MongoDB
- Mongodb叢集搭建一篇就夠了-複製集、分片、帶認證、不帶認證等(帶詳細步驟說明)MongoDB
- 【Mongodb】mongo複製集只剩一個secondery節點的解決辦法MongoDB
- linux搭建kafka叢集,詳細到複製命令就能成功LinuxKafka
- MySQL5.7主從複製-半同步複製搭建MySql
- MongoDB mongoshake 遷移分片到複製集合MongoDB
- mysql主從複製搭建MySql
- 在滴滴雲上搭建 MongoDB 叢集 (一):MongoDB
- linux下Mongodb叢集搭建:分片+副本集LinuxMongoDB
- mongodb叢集shard_replica的搭建方法MongoDB
- GreatSQL 非同步複製及搭建SQL非同步
- 【Mongo】單節點升級為複製集再升級為分片加複製集Go