簡(jiǎn)介:
復(fù)制集:
1:標(biāo)準(zhǔn)節(jié)點(diǎn):
參與primary選舉
2:被動(dòng)節(jié)點(diǎn):
只能成為secend,不參與選舉
3:仲裁節(jié)點(diǎn):
負(fù)責(zé)投票選舉,不存放數(shù)據(jù)
我們提供的服務(wù)有:成都網(wǎng)站建設(shè)、網(wǎng)站建設(shè)、微信公眾號(hào)開(kāi)發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、魚峰ssl等。為上千企事業(yè)單位解決了網(wǎng)站和推廣的問(wèn)題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的魚峰網(wǎng)站制作公司
實(shí)驗(yàn)環(huán)境:2臺(tái)標(biāo)準(zhǔn)節(jié)點(diǎn) 1臺(tái)被動(dòng)節(jié)點(diǎn) 1臺(tái)仲裁點(diǎn)
具體實(shí)驗(yàn)步驟:(前半部分實(shí)驗(yàn)為上次實(shí)驗(yàn)操作)
[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
開(kāi)啟replication,名字自定義
replication:
replSetName: kgcrs
[root@localhost yum.repos.d]# mongod -f /etc/mongod.conf --shutdown #關(guān)閉服務(wù)
[root@localhost yum.repos.d]# mongod -f /etc/mongod.conf #開(kāi)啟服務(wù)
復(fù)制產(chǎn)生四個(gè)實(shí)例
[root@localhost yum.repos.d]# cp -p /etc/mongod.conf /etc/mongod2.conf
[root@localhost yum.repos.d]# cp -p /etc/mongod.conf /etc/mongod3.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
#將其他幾個(gè)都修改
[root@localhost yum.repos.d]# cp -p /etc/mongod2.conf /etc/mongod3.conf
[root@localhost yum.repos.d]# vim /etc/mongod3.conf
(還是那三條,序號(hào)改好即可)
[root@localhost yum.repos.d]# cp -p /etc/mongod2.conf /etc/mongod4.conf
[root@localhost yum.repos.d]# vim /etc/mongod4.conf
#修改完成后依次啟動(dòng)
[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
#查看端口開(kāi)啟
[root@localhost yum.repos.d]# netstat -ntap
#登錄MongoDB
#復(fù)制集
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)
}
}
}
-------------------------------------日志操作--------------------------------------------------
#創(chuàng)建庫(kù)kgc,并進(jìn)入kgc
kgcrs:PRIMARY> use kgc
switched to db kgc
在kgc庫(kù)中創(chuàng)建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 })
#查看數(shù)據(jù)
kgcrs:PRIMARY> db.t1.find()
{ "_id" : ObjectId("5b9a0d9efa7413d9cb629b39"), "id" : 1, "name" : "jack" }
{ "_id" : ObjectId("5b9a0dacfa7413d9cb629b3a"), "id" : 2, "name" : "tom" }
#數(shù)據(jù)庫(kù)操作結(jié)束后我們開(kāi)始日志操作
#進(jìn)入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()
(所有操作都記錄在這個(gè)日志中)
#我們都知道主節(jié)點(diǎn)能進(jìn)行操作,從節(jié)點(diǎn)只能充當(dāng)復(fù)制集(不能操作)那就不能查看,但有一條命令可以查看
#登錄到從節(jié)點(diǎn)后查看,發(fā)現(xiàn)查看不了
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()
#查看日志時(shí)間和大小
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)
#查看節(jié)點(diǎn)和復(fù)制時(shí)間
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
#調(diào)整日志文件大小(離線升級(jí))
#首先進(jìn)入admin
kgcrs:SECONDARY> use admin
switched to db admin
#關(guān)閉服務(wù)
kgcrs:SECONDARY> db.shutdownServer()
server should be down...
#注銷Replia set相關(guān)啟動(dòng)參數(shù),并修改port端口號(hào)成27028
#進(jìn)入配置文件
[root@localhost ~]# vim /etc/mongod2.conf
#replication:
#replSetName: kgcrs將這兩條先注釋掉(退出復(fù)制集)
將端口27018改成27028
#重啟服務(wù)
[root@localhost ~]# mongod -f /etc/mongod2.conf
#全備當(dāng)前節(jié)點(diǎn)的所有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
#創(chuàng)建新的,并設(shè)置大?。?048M)
db.runCommand({create:"oplog.rs",capped:true,size:(210241024*1024)})
{ "ok" : 1 }
#關(guān)閉服務(wù)
use admin
switched to db admin
db.shutdownServer()
#再進(jìn)配置文件
[root@localhost ~]# vim /etc/mongod2.conf
#去掉注釋,添加大小
replication:
replSetName: kgcrs
oplogSizeMB: 2048
#把端口改回27018
#開(kāi)啟服務(wù)
[root@localhost ~]# mongod -f /etc/mongod2.conf
#進(jìn)入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)
-------------------------------部署認(rèn)證復(fù)制---------------------------------------------
#j進(jìn)入主節(jié)點(diǎn)
kgcrs:PRIMARY> use admin
switched to db admin
#創(chuàng)建用戶設(shè)置密碼
kgcrs:PRIMARY> db.createUser({"user":"root","pwd":"123","roles":["root"]})
Successfully added user: { "user" : "root", "roles" : [ "root" ] }
#編輯配置文件
[root@localhost ~]# vim /etc/mongod.conf
#開(kāi)啟security添加兩條語(yǔ)句
security:
keyFile: /usr/bin/kgcrskeyl
clusterAuthMode: keyFile
#修改后面幾臺(tái)同步操作()
[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
#依次重啟四個(gè)節(jié)點(diǎn)
#進(jìn)入primary
kgcrs:PRIMARY> show dbs #無(wú)法查看數(shù)據(jù)庫(kù)
kgcrs:PRIMARY> rs.status() #無(wú)法查看復(fù)制集
kgcrs:PRIMARY>use admin #身份驗(yàn)證登錄
kgcrs:PRIMARY>db.auth("root","123")
#登錄成功,可以查看