show dbs &nbs..."/>
溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

MongoDB 復(fù)制集管理

發(fā)布時間:2020-02-26 05:46:52 來源:網(wǎng)絡(luò) 閱讀:939 作者:LIUZabc123 欄目:MongoDB數(shù)據(jù)庫


1.配置允許在從節(jié)點讀取數(shù)據(jù)

默認MongoDB 復(fù)制集的從節(jié)點不能讀取數(shù)據(jù),可以使用 rs.slaveOk()  命令允許能夠在從節(jié)點讀取數(shù)據(jù)。

abc:PRIMARY> show dbs                         #在主節(jié)點上可以讀取數(shù)據(jù)
admin   0.000GB
config  0.000GB
local   0.000GB
school  0.000GB
abc:PRIMARY> exit
bye
[root@localhost logs]# mongo --port 27018                        #進入端口為27018 的從節(jié)點
MongoDB shell version v3.6.7
connecting to: mongodb://127.0.0.1:27018/
abc:SECONDARY> show dbs                                             #查看數(shù)據(jù)庫
2018-09-13T14:55:03.037+0800 E QUERY    [thread1] Error: listDatabases failed:{        #無法讀取數(shù)據(jù)
     "operationTime" : Timestamp(1536821694, 1),
     "ok" : 0,
     "errmsg" : "not master and slaveOk=false",


    abc:SECONDARY> rs.slaveOk()                   #使用命令 rs.slaveOk() 命令允許能夠在從節(jié)點讀取數(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 lag  

abc:SECONDARY> rs.printReplicationInfo()                                    #查看日志大小和時間范圍
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é)點復(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é)點并不具備數(shù)據(jù)復(fù)制

3. .更改oplog 大小

         oplog 即 opreations 的縮寫,存儲在 local 數(shù)據(jù)庫中。oplog 中新操作會自動替換舊的操作,以保證 oplog 不會超過預(yù)設(shè)的大小。默認情況下。oplog 大小會占用64位的實例5% 的磁盤空間。盡量保證主節(jié)點的oplog 足夠大,能夠存放相當長時間的操作記錄。

(1)首先關(guān)閉從節(jié)點服務(wù)器,從復(fù)制集中退出,暫時成為單實例

abc:SECONDARY> use admin
switched to db admin

abc:SECONDARY> db.shutdownServer()                     #關(guān)閉服務(wù)
server should be down...

[root@localhost logs]# vim /etc/mongod2.conf              #更改實例2 的配置文件

port: 27028                                               #端口號更改

#replication:
#  replSetName: abc
                             #注銷復(fù)制集

(2)以端口號為 27028 進入數(shù)據(jù)庫

[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)對oplog 進行完全備份

[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)把獨立的實例 mongodb2 恢復(fù)到復(fù)制集,登錄。

> exit
bye
[root@localhost logs]# vim /etc/mongod2.conf          #把獨立的實例 mongodb2 恢復(fù)到復(fù)制集

port: 27018                      #把端口號改回為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 .部署認證復(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)在每個實例的配置文件中開啟認證功能

abc:PRIMARY> exit
bye
[root@localhost logs]# vim /etc/mongod.conf

security:
    keyFile: /usr/bin/abckey1                         #驗證文件路徑
    clusterAuthMode: keyFile                          #驗證模式,文件驗證

[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個密鑰文件
[root@localhost bin]# echo "abc key" > abckey2
[root@localhost bin]# echo "abc key" > abckey3
[root@localhost bin]# echo "abc key" > abckey4

(3)重啟4個實例

[root@localhost bin]# chmod 600 abc*              # 把文件 abc 權(quán)限設(shè)置為600             
[root@localhost bin]# mongod -f /etc/mongod.conf                #啟動服務(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é)點服務(wù)器驗證  

[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é)點查看數(shù)據(jù)庫
2018-09-13T16:49:14.542+0800 E QUERY    [thread1] Error: listDatabases failed:{           #無法查詢
     "operationTime" : Timestamp(1536828545, 1),
     "ok" : 0,

abc:PRIMARY> rs.status()                #查看各節(jié)點狀態(tài),也無法查詢
{
     "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               #進入admin 數(shù)據(jù)庫
switched to db admin
abc:PRIMARY> db.auth("root","123")           #進行身份驗證
1
abc:PRIMARY> show dbs                 #再查看數(shù)據(jù)庫
admin   0.000GB
config  0.000GB
local   0.000GB
school  0.000GB

(5)進入從節(jié)點服務(wù)器進行驗證

[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ù)庫
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                #進行身份驗證
switched to db admin
abc:SECONDARY> db.auth("root","123")
1

abc:SECONDARY> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
school  0.000GB

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI