1.配置允許在從節(jié)點(diǎn)讀取數(shù)據(jù)
成都創(chuàng)新互聯(lián)為企業(yè)級(jí)客戶提高一站式互聯(lián)網(wǎng)+設(shè)計(jì)服務(wù),主要包括網(wǎng)站制作、成都網(wǎng)站制作、重慶APP開發(fā)、小程序制作、宣傳片制作、LOGO設(shè)計(jì)等,幫助客戶快速提升營(yíng)銷能力和企業(yè)形象,創(chuàng)新互聯(lián)各部門都有經(jīng)驗(yàn)豐富的經(jīng)驗(yàn),可以確保每一個(gè)作品的質(zhì)量和創(chuàng)作周期,同時(shí)每年都有很多新員工加入,為我們帶來(lái)大量新的創(chuàng)意。
默認(rèn)MongoDB 復(fù)制集的從節(jié)點(diǎn)不能讀取數(shù)據(jù),可以使用 rs.slaveOk() 命令允許能夠在從節(jié)點(diǎn)讀取數(shù)據(jù)。
abc:PRIMARY> show dbs #在主節(jié)點(diǎn)上可以讀取數(shù)據(jù)
admin 0.000GB
config 0.000GB
local 0.000GB
school 0.000GB
abc:PRIMARY> exit
bye
[root@localhost logs]# mongo --port 27018 #進(jìn)入端口為27018 的從節(jié)點(diǎn)
MongoDB shell version v3.6.7
connecting to: mongodb://127.0.0.1:27018/
abc:SECONDARY> show dbs #查看數(shù)據(jù)庫(kù)
2018-09-13T14:55:03.037+0800 E QUERY [thread1] Error: listDatabases failed:{ #無(wú)法讀取數(shù)據(jù)
"operationTime" : Timestamp(1536821694, 1),
"ok" : 0,
"errmsg" : "not master and slaveOk=false",
abc:SECONDARY> rs.slaveOk() #使用命令 rs.slaveOk() 命令允許能夠在從節(jié)點(diǎn)讀取數(shù)據(jù)
abc:SECONDARY> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
school 0.000GB
abc:SECONDARY>
2.查看復(fù)制狀態(tài)信息
abc:SECONDARY> rs.help()
rs.printReplicationInfo() check oplog size and time range
rs.printSlaveReplicationInfo() check replica set members and replication lagabc:SECONDARY> rs.printReplicationInfo() #查看日志大小和時(shí)間范圍
configured oplog size: 990MB
log length start to end: 3482secs (0.97hrs)
oplog first event time: Thu Sep 13 2018 14:12:02 GMT+0800 (CST)
oplog last event time: Thu Sep 13 2018 15:10:04 GMT+0800 (CST)
now: Thu Sep 13 2018 15:10:06 GMT+0800 (CST)
abc:SECONDARY> rs.printSlaveReplicationInfo() #查看那些從節(jié)點(diǎn)復(fù)制數(shù)據(jù)
source: 192.168.213.184:27018
syncedTo: Thu Sep 13 2018 15:11:54 GMT+0800 (CST)
0 secs (0 hrs) behind the primary
source: 192.168.213.184:27019
syncedTo: Thu Sep 13 2018 15:11:54 GMT+0800 (CST)
0 secs (0 hrs) behind the primary由此可看出仲裁節(jié)點(diǎn)并不具備數(shù)據(jù)復(fù)制
3. .更改oplog 大小
oplog 即 opreations 的縮寫,存儲(chǔ)在 local 數(shù)據(jù)庫(kù)中。oplog 中新操作會(huì)自動(dòng)替換舊的操作,以保證 oplog 不會(huì)超過(guò)預(yù)設(shè)的大小。默認(rèn)情況下。oplog 大小會(huì)占用64位的實(shí)例5% 的磁盤空間。盡量保證主節(jié)點(diǎn)的oplog 足夠大,能夠存放相當(dāng)長(zhǎng)時(shí)間的操作記錄。
(1)首先關(guān)閉從節(jié)點(diǎn)服務(wù)器,從復(fù)制集中退出,暫時(shí)成為單實(shí)例
abc:SECONDARY> use admin
switched to db adminabc:SECONDARY> db.shutdownServer() #關(guān)閉服務(wù)
server should be down...[root@localhost logs]# vim /etc/mongod2.conf #更改實(shí)例2 的配置文件
port: 27028 #端口號(hào)更改
#replication:
# replSetName: abc #注銷復(fù)制集
(2)以端口號(hào)為 27028 進(jìn)入數(shù)據(jù)庫(kù)
[root@localhost logs]# mongod -f /etc/mongod2.conf
about to fork child process, waiting until server is ready for connections.
forked process: 40575
child process started successfully, parent exiting
[root@localhost logs]# mongo --port 27028
MongoDB shell version v3.6.7
connecting to: mongodb://127.0.0.1:27028/
(3)對(duì)oplog 進(jìn)行完全備份
[root@localhost logs]# mongodump --port 27028 --db local --collection 'oplog.rs'
2018-09-13T15:30:19.876+0800 writing local.oplog.rs to
2018-09-13T15:30:19.881+0800 done dumping local.oplog.rs (376 documents)
(4)刪除原有的日志文件
> use local
switched to db local
> show tables
me
oplog.rs
replset.election
replset.minvalid
replset.oplogTruncateAfterPoint
startup_log
system.replset
system.rollback.id
> db.oplog.rs.drop()
true
> db.runCommand({create:"oplog.rs",capped:true,size:(2 * 1024 * 1024 * 1024)}) #原型創(chuàng)建 oplog.rs 指定大小
{ "ok" : 1 }
> use admin
switched to db admin
> db.shutdownServer() #關(guān)閉服務(wù)
server should be down...
(5)把獨(dú)立的實(shí)例 mongodb2 恢復(fù)到復(fù)制集,登錄。
> exit
bye
[root@localhost logs]# vim /etc/mongod2.conf #把獨(dú)立的實(shí)例 mongodb2 恢復(fù)到復(fù)制集port: 27018 #把端口號(hào)改回為27018
replication: #啟用復(fù)制集
replSetName: abc
oplogSizeMB: 2048 #指定 oplog 大小[root@localhost logs]# mongod -f /etc/mongod2.conf
about to fork child process, waiting until server is ready for connections.
forked process: 40835
child process started successfully, parent exiting
[root@localhost logs]# mongo --port 27018
MongoDB shell version v3.6.7
connecting to: mongodb://127.0.0.1:27018/abc:SECONDARY> rs.printReplicationInfo()
configured oplog size: 2048MB
log length start to end: 90secs (0.03hrs)
oplog first event time: Thu Sep 13 2018 15:44:15 GMT+0800 (CST)
oplog last event time: Thu Sep 13 2018 15:45:45 GMT+0800 (CST)
now: Thu Sep 13 2018 15:45:54 GMT+0800 (CST)
4 .部署認(rèn)證復(fù)制
(1)
abc:PRIMARY> use admin
switched to db admin
abc:PRIMARY> db.createUser({"user":"root","pwd":"123","roles":["root"]}) #創(chuàng)建用戶root 設(shè)置密碼為 123
Successfully added user: { "user" : "root", "roles" : [ "root" ] }
(2)在每個(gè)實(shí)例的配置文件中開啟認(rèn)證功能
abc:PRIMARY> exit
bye
[root@localhost logs]# vim /etc/mongod.confsecurity:
keyFile: /usr/bin/abckey1 #驗(yàn)證文件路徑
clusterAuthMode: keyFile #驗(yàn)證模式,文件驗(yàn)證[root@localhost logs]# vim /etc/mongod2.conf
security:
keyFile: /usr/bin/abckey2
clusterAuthMode:keyFile[root@localhost logs]# vim /etc/mongod3.conf
security:
keyFile: /usr/bin/abckey3
clusterAuthMode:keyFile[root@localhost logs]# vim /etc/mongod4.conf
security:
keyFile: /usr/bin/abckey4
clusterAuthMode:keyFile
[root@localhost logs]# cd /usr/bin/
[root@localhost bin]# echo "abc key" > abckey1 #生成4個(gè)密鑰文件
[root@localhost bin]# echo "abc key" > abckey2
[root@localhost bin]# echo "abc key" > abckey3
[root@localhost bin]# echo "abc key" > abckey4
(3)重啟4個(gè)實(shí)例
[root@localhost bin]# chmod 600 abc* # 把文件 abc 權(quán)限設(shè)置為600
[root@localhost bin]# mongod -f /etc/mongod.conf #啟動(dòng)服務(wù)
about to fork child process, waiting until server is ready for connections.
forked process: 41828
child process started successfully, parent exiting
[root@localhost bin]# mongod -f /etc/mongod2.conf --shutdown
killing process with pid: 40835
[root@localhost bin]# mongod -f /etc/mongod2.conf
about to fork child process, waiting until server is ready for connections.
forked process: 42252
child process started successfully, parent exiting
[root@localhost bin]# mongod -f /etc/mongod3.conf --shutdown
killing process with pid: 4881
[root@localhost bin]# mongod -f /etc/mongod3.conf
about to fork child process, waiting until server is ready for connections.
forked process: 42451
child process started successfully, parent exiting
[root@localhost bin]# mongod -f /etc/mongod4.conf --shutdown
killing process with pid: 4909
[root@localhost bin]# mongod -f /etc/mongod4.conf
about to fork child process, waiting until server is ready for connections.
forked process: 42634
child process started successfully, parent exiting
(4)登錄主節(jié)點(diǎn)服務(wù)器驗(yàn)證
[root@localhost bin]# mongo --port 27018
MongoDB shell version v3.6.7
connecting to: mongodb://127.0.0.1:27018/
MongoDB server version: 3.6.7
abc:PRIMARY> show dbs #在主節(jié)點(diǎn)查看數(shù)據(jù)庫(kù)
2018-09-13T16:49:14.542+0800 E QUERY [thread1] Error: listDatabases failed:{ #無(wú)法查詢
"operationTime" : Timestamp(1536828545, 1),
"ok" : 0,abc:PRIMARY> rs.status() #查看各節(jié)點(diǎn)狀態(tài),也無(wú)法查詢
{
"operationTime" : Timestamp(1536828575, 1),
"ok" : 0,
"errmsg" : "not authorized on admin to execute command { replSetGetStatus: 1.0, $clusterTime: { clusterTime: Timestamp(1536828545, 1), signature: { hash: BinData(0, 40060B8D2AC8AC1AE68D47E9332835D2040120C2), keyId: 6600587920397041666 } }, $db: \"admin\" }",
"code" : 13,
"codeName" : "Unauthorized",
"$clusterTime" : {
"clusterTime" : Timestamp(1536828575, 1),
"signature" : {
"hash" : BinData(0,"gSi7raqiqfKJKSF42wlgu2rvggE="),
"keyId" : NumberLong("6600587920397041666")
}
}
}abc:PRIMARY> use admin #進(jìn)入admin 數(shù)據(jù)庫(kù)
switched to db admin
abc:PRIMARY> db.auth("root","123") #進(jìn)行身份驗(yàn)證
1
abc:PRIMARY> show dbs #再查看數(shù)據(jù)庫(kù)
admin 0.000GB
config 0.000GB
local 0.000GB
school 0.000GB
(5)進(jìn)入從節(jié)點(diǎn)服務(wù)器進(jìn)行驗(yàn)證
[root@localhost bin]# mongo --port 27019
MongoDB shell version v3.6.7
connecting to: mongodb://127.0.0.1:27019/
MongoDB server version: 3.6.7
abc:SECONDARY> show dbs #查看數(shù)據(jù)庫(kù)
2018-09-13T16:55:14.429+0800 E QUERY [thread1] Error: listDatabases failed:{
"operationTime" : Timestamp(1536828905, 1),
"ok" : 0,abc:SECONDARY> rs.slaveOk()
abc:SECONDARY> use admin #進(jìn)行身份驗(yàn)證
switched to db admin
abc:SECONDARY> db.auth("root","123")
1abc:SECONDARY> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
school 0.000GB