MongoDB復制集管理(後續)
復制集:
1:標準節點:
參與primary選舉
2:被動節點:
只能成為secend,不參與選舉
3:仲裁節點:
負責投票選舉,不存放數據
實驗環境:2臺標準節點 1臺被動節點 1臺仲裁點
具體實驗步驟:(前半部分實驗為上次實驗操作)
[root@localhost ~]# mkdir -p /data/mongodb/mongodb{2,3,4}
[root@localhost ~]# mkdir -p /data/mongodb/logs
[root@localhost ~]# touch /data/mongodb/logs/mongodb{2,3,4}.log
[root@localhost ~]# chmod 777 /data/mongodb/logs/*.log
[root@localhost logs]# vim /etc/mongod.conf
開啟replication,名字自定義
replication:
replSetName: kgcrs
[root@localhost yum.repos.d]# mongod -f /etc/mongod.conf --shutdown #關閉服務
[root@localhost yum.repos.d]# mongod -f /etc/mongod.conf #開啟服務
復制產生四個實例
[root@localhost yum.repos.d]# cp -p /etc/mongod.conf /etc/mongod2.conf
[root@localhost yum.repos.d]# cp -p /etc/mongod.conf /etc/mongod4.conf
[root@localhost yum.repos.d]# vim /etc/mongod2.conf
#修改三處
path: /data/mongodb/logs/mongodb2.log
dbPath: /data/mongodb/mongodb2
port: 27018
#將其他幾個都修改
[root@localhost yum.repos.d]# cp -p /etc/mongod2.conf /etc/mongod3.conf
(還是那三條,序號改好即可)
[root@localhost yum.repos.d]# cp -p /etc/mongod2.conf /etc/mongod4.conf
[root@localhost yum.repos.d]# vim /etc/mongod4.conf
#修改完成後依次啟動
[root@localhost yum.repos.d]# mongod -f /etc/mongod2.conf
[root@localhost yum.repos.d]# mongod -f /etc/mongod3.conf
[root@localhost yum.repos.d]# mongod -f /etc/mongod4.conf
#查看端口開啟
[root@localhost yum.repos.d]# netstat -ntap
#登錄MongoDB
#復制集
cfg={"_id":"kgcrs","members":[{"_id":0,"host":"192.168.120.136:27017","priority":100},{"_id":1,"host":"192.168.120.136:27018","priority":100},{"_id":2,"host":"192.168.120.136:27019","priority":0},{"_id":3,"host":"192.168.120.136:27020","arbiterOnly":true}]}
{
"_id" : "kgcrs",
"members" : [
{
"_id" : 0,
"host" : "192.168.120.136:27017",
"priority" : 100
},
{
"_id" : 1,
"host" : "192.168.120.136:27018",
"priority" : 100
},
{
"_id" : 2,
"host" : "192.168.120.136:27019",
"priority" : 0
},
{
"_id" : 3,
"host" : "192.168.120.136:27020",
"arbiterOnly" : true
}
]
}
#初始化
rs.initiate(cfg)
{
"ok" : 1,
"operationTime" : Timestamp(1536821356, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1536821356, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
#查看身份
kgcrs:PRIMARY> rs.isMaster()
{
"hosts" : [
"192.168.120.136:27017",
"192.168.120.136:27018"
],
"passives" : [
"192.168.120.136:27019"
],
"arbiters" : [
"192.168.120.136:27020"
],
"setName" : "kgcrs",
"setVersion" : 1,
"ismaster" : true,
"secondary" : false,
"primary" : "192.168.120.136:27017",
"me" : "192.168.120.136:27017",
"electionId" : ObjectId("7fffffff0000000000000001"),
"lastWrite" : {
"opTime" : {
"ts" : Timestamp(1536821508, 1),
"t" : NumberLong(1)
},
"lastWriteDate" : ISODate("2018-09-13T06:51:48Z"),
"majorityOpTime" : {
"ts" : Timestamp(1536821508, 1),
"t" : NumberLong(1)
},
"majorityWriteDate" : ISODate("2018-09-13T06:51:48Z")
},
"maxBsonObjectSize" : 16777216,
"maxMessageSizeBytes" : 48000000,
"maxWriteBatchSize" : 100000,
"localTime" : ISODate("2018-09-13T06:51:54.761Z"),
"logicalSessionTimeoutMinutes" : 30,
"minWireVersion" : 0,
"maxWireVersion" : 6,
"readOnly" : false,
"ok" : 1,
"operationTime" : Timestamp(1536821508, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1536821508, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
-------------------------------------日誌操作--------------------------------------------------
#創建庫kgc,並進入kgc
kgcrs:PRIMARY> use kgc
switched to db kgc
在kgc庫中創建t1集合,插入兩條信息
kgcrs:PRIMARY> db.t1.insert({"id":1,"name":"jack"})
WriteResult({ "nInserted" : 1 })
kgcrs:PRIMARY> db.t1.insert({"id":2,"name":"tom"})
WriteResult({ "nInserted" : 1 })
#查看數據
kgcrs:PRIMARY> db.t1.find()
{ "_id" : ObjectId("5b9a0d9efa7413d9cb629b39"), "id" : 1, "name" : "jack" }
{ "_id" : ObjectId("5b9a0dacfa7413d9cb629b3a"), "id" : 2, "name" : "tom" }
#數據庫操作結束後我們開始日誌操作
#進入local
kgcrs:PRIMARY> use local
switched to db local
#查看集合
kgcrs:PRIMARY> show collections
me
oplog.rs
replset.election
replset.minvalid
startup_log
system.replset
system.rollback.id
#查看日誌文件
kgcrs:PRIMARY> db.oplog.rs.find()
(所有操作都記錄在這個日誌中)
#我們都知道主節點能進行操作,從節點只能充當復制集(不能操作)那就不能查看,但有一條命令可以查看
#登錄到從節點後查看,發現查看不了
kgcrs:SECONDARY> show dbs
2018-09-13T15:28:51.529+0800 E QUERY [thread1] Error: listDatabases failed:{
"operationTime" : Timestamp(1536823728, 1),
"ok" : 0,
"errmsg" : "not master and slaveOk=false",
"code" : 13435,
"codeName" : "NotMasterNoSlaveOk",
"$clusterTime" : {
"clusterTime" : Timestamp(1536823728, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
} :_getErrorWithCode@src/mongo/shell/utils.js:25:13
br/>_getErrorWithCode@src/mongo/shell/utils.js:25:13
br/>shellHelper.show@src/mongo/shell/utils.js:849:19
br/>@(shellhelp2):1:1
kgcrs:SECONDARY> rs.slaveOk()
#查看一下(可以)
kgcrs:SECONDARY> show dbs
admin 0.000GB
config 0.000GB
kgc 0.000GB
#命令記不住,可以查看幫助
kgcrs:SECONDARY> rs.help()
#查看日誌時間和大小
kgcrs:SECONDARY> rs.printReplicationInfo()
configured oplog size: 990MB
log length start to end: 2812secs (0.78hrs)
oplog first event time: Thu Sep 13 2018 14:49:16 GMT+0800 (CST)
oplog last event time: Thu Sep 13 2018 15:36:08 GMT+0800 (CST)
now: Thu Sep 13 2018 15:36:12 GMT+0800 (CST)
#查看節點和復制時間
kgcrs:SECONDARY> rs.printSlaveReplicationInfo()
source: 192.168.120.136:27018
syncedTo: Thu Sep 13 2018 15:40:28 GMT+0800 (CST)
0 secs (0 hrs) behind the primary
source: 192.168.120.136:27019
syncedTo: Thu Sep 13 2018 15:40:28 GMT+0800 (CST)
0 secs (0 hrs) behind the primary
#調整日誌文件大小(離線升級)
#首先進入admin
kgcrs:SECONDARY> use admin
switched to db admin
#關閉服務
kgcrs:SECONDARY> db.shutdownServer()
server should be down...
#註銷Replia set相關啟動參數,並修改port端口號成27028
#進入配置文件
[root@localhost ~]# vim /etc/mongod2.conf
#replication:
#replSetName: kgcrs將這兩條先註釋掉(退出復制集)
將端口27018改成27028
#重啟服務
[root@localhost ~]# mongod -f /etc/mongod2.conf
#全備當前節點的所有oplog記錄
[root@localhost ~]# mongodump --port 27028 --db local --collection ‘oplog.rs‘
2018-09-13T16:01:34.259+0800 writing local.oplog.rs to
2018-09-13T16:01:34.280+0800 done dumping local.oplog.rs (347 documents)
[root@localhost ~]# mongo --port 27028
use local
switched to db local
#刪除原有的
db.oplog.rs.drop()
true
#創建新的,並設置大小(2048M)
db.runCommand({create:"oplog.rs",capped:true,size:(210241024*1024)})
{ "ok" : 1 }
#關閉服務
use admin
switched to db admin
db.shutdownServer()
#再進配置文件
[root@localhost ~]# vim /etc/mongod2.conf
#去掉註釋,添加大小
replication:
replSetName: kgcrs
oplogSizeMB: 2048
#把端口改回27018
#開啟服務
[root@localhost ~]# mongod -f /etc/mongod2.conf
#進入27018
#查看大小
kgcrs:SECONDARY> rs.printReplicationInfo()
configured oplog size: 2048MB
log length start to end: 100secs (0.03hrs)
oplog first event time: Thu Sep 13 2018 16:10:29 GMT+0800 (CST)
oplog last event time: Thu Sep 13 2018 16:12:09 GMT+0800 (CST)
now: Thu Sep 13 2018 16:12:16 GMT+0800 (CST)
-------------------------------部署認證復制---------------------------------------------
#j進入主節點
kgcrs:PRIMARY> use admin
switched to db admin
#創建用戶設置密碼
kgcrs:PRIMARY> db.createUser({"user":"root","pwd":"123","roles":["root"]})
Successfully added user: { "user" : "root", "roles" : [ "root" ] }
#編輯配置文件
[root@localhost ~]# vim /etc/mongod.conf
#開啟security添加兩條語句
security:
keyFile: /usr/bin/kgcrskeyl
clusterAuthMode: keyFile
#修改後面幾臺同步操作()
[root@localhost ~]# vim /etc/mongod2.conf
security:
keyFile: /usr/bin/kgcrskey2
clusterAuthMode: keyFile
[root@localhost ~]# vim /etc/mongod3.conf
security:
keyFile: /usr/bin/kgcrskey3
clusterAuthMode: keyFile
[root@localhost ~]# vim /etc/mongod4.conf
security:
keyFile: /usr/bin/kgcrskey4
clusterAuthMode: keyFile
#寫入
[root@localhost ~]# cd /usr/bin/
[root@localhost bin]# echo "kgcrs key" > kgcrskey1
[root@localhost bin]# echo "kgcrs key" > kgcrskey2
[root@localhost bin]# echo "kgcrs key" > kgcrskey4
[root@localhost bin]# echo "kgcrs key" > kgcrskey3
[root@localhost bin]# mongod -f /etc/mongod.conf --shutdown
killing process with pid: 50970
[root@localhost bin]# chmod 600 kgc*
[root@localhost bin]# mongod -f /etc/mongod.conf
#依次重啟四個節點
#進入primary
kgcrs:PRIMARY> show dbs #無法查看數據庫
kgcrs:PRIMARY> rs.status() #無法查看復制集
kgcrs:PRIMARY>use admin #身份驗證登錄
kgcrs:PRIMARY>db.auth("root","123")
#登錄成功,可以查看
MongoDB復制集管理(後續)