mongodb集群方式-分片+副本集方式


 

分片就是水平的擴展,將數據分拆到不同的機器上,以達到存儲更多的數據,處理更大的負載。可以選定將按照指定的文檔鍵值進行分片。

配置

整體部署方案:

啟動三個分片服務,兩個做數據存儲,另一個作為config,

配置分片的副本集,創建管理員用戶,關閉mongod,打開keyfile,啟動mongos,配置分片服務。

角色

分片一般有三個組成部分:

分片服務(Shard Server),mongod 實例,2個以上,負責存儲實際的數據分片,生產環境中一個Shard Server可由幾台服務器組成一個Replica Set代替,避免主機單點故障;
路由服務(Routing Process),mongos實例,1個以上,它負責管理分片,客戶端由此前端路由接入,且讓整個集群看起來像單一數據庫,客戶端應用可以透明使用,Routing Process不存儲數據,數據來自Config Server;

配置服務(Config Server),mongod 實例,1個以上,負責存儲整個集群的配置信息:即數據和片的對應關系。

 

因為測試資源有限, 采取三台虛擬機的方式,

 

分片的架構圖如下:

192.168.100.101所有配置如下:

############config-1############
configsvr = true
replSet = config
port = 30001
dbpath = /opt/mongo/data/config-1
logpath = /opt/mongo/logs/config-1.log
logappend = true
fork = true
profile = 1
slowms = 500
keyFile = /opt/mongo/config/keyfile
maxConns=20000
storageEngine = wiredTiger
############route############
configdb = config/192.168.100.101:30001,192.168.100.102:30002,192.168.100.103:30003
port = 20000
logpath = /opt/mongo/logs/route.log
logappend = true
fork = true
#chunkSize = 256
keyFile = /opt/mongo/config/keyfile
maxConns=20000
############rs1-1############
port = 10001
fork = true
dbpath = /opt/mongo/data/rs1-1
logpath = /opt/mongo/logs/rs1-1.log
replSet = test1
logappend = true
profile = 1
slowms = 500
directoryperdb = true
keyFile = /opt/mongo/config/keyfile
maxConns=20000
storageEngine = wiredTiger
############rs2-a############
port = 20003
fork = true
dbpath = /opt/mongo/data/rs2-a
logpath = /opt/mongo/logs/rs2-a.log
replSet = test2
logappend = true
profile = 1
slowms = 500
directoryperdb = true
keyFile = /opt/mongo/config/keyfile
maxConns=20000
storageEngine = wiredTiger

192.168.100.102所有配置如下:

############config-2############
configsvr = true
replSet = config
port = 30002
dbpath = /opt/mongo/data/config-2
logpath = /opt/mongo/logs/config-2.log
logappend = true
fork = true
profile = 1
slowms = 500
keyFile = /opt/mongo/config/keyfile
maxConns=20000
storageEngine = wiredTiger
############route############
configdb = config/192.168.100.101:30001,192.168.100.102:30002,192.168.100.103:30003
port = 20000
logpath = /opt/mongo/logs/route.log
logappend = true
fork = true
#chunkSize = 256
keyFile = /opt/mongo/config/keyfile
maxConns=20000
############rs1-2############
port = 10002
fork = true
dbpath = /opt/mongo/data/rs1-2
logpath = /opt/mongo/logs/rs1-2.log
replSet = test1
logappend = true
profile = 1
slowms = 500
directoryperdb = true
keyFile = /opt/mongo/config/keyfile
maxConns=20000
storageEngine = wiredTiger
############rs2-2############
port = 20002
fork = true
dbpath = /opt/mongo/data/rs2-2
logpath = /opt/mongo/logs/rs2-2.log
replSet = test2
logappend = true
profile = 1
slowms = 500
directoryperdb = true
keyFile = /opt/mongo/config/keyfile
maxConns=20000
storageEngine = wiredTiger

192.168.100.103所有配置如下:

############config-3############
configsvr = true
replSet = config
port = 30003
dbpath = /opt/mongo/data/config-3
logpath = /opt/mongo/logs/config-3.log
logappend = true
fork = true
profile = 1
slowms = 500
keyFile = /opt/mongo/config/keyfile
maxConns=20000
storageEngine = wiredTiger
############route############
configdb = config/192.168.100.101:30001,192.168.100.102:30002,192.168.100.103:30003
port = 20000
logpath = /opt/mongo/logs/route.log
logappend = true
fork = true
#chunkSize = 256
keyFile = /opt/mongo/config/keyfile
maxConns=20000
############rs1-a############
port = 10003
fork = true
dbpath = /opt/mongo/data/rs1-a
logpath = /opt/mongo/logs/rs1-a.log
replSet = test1
logappend = true
profile = 1
slowms = 500
directoryperdb = true
keyFile = /opt/mongo/config/keyfile
maxConns=20000
storageEngine = wiredTiger
############rs2-1############
port = 20001
fork = true
dbpath = /opt/mongo/data/rs2-1
logpath = /opt/mongo/logs/rs2-1.log
replSet = test2
logappend = true
profile = 1
slowms = 500
directoryperdb = true
keyFile = /opt/mongo/config/keyfile
maxConns=20000
storageEngine = wiredTiger

 

openfile 可以使用命令生成:

openssl rand -base64 500 > keyfile
chmod 400 keyfile

確保每台機器的keyfile一致

 

依次按照上面配置文件的內容創建配置文件和存儲目錄日志目錄

創建完成依次啟動所有的mongod節點

注意此時要把keyfile選項注釋掉,否則啟動之后未創建角色就要認證,無法進入操作

ls |grep -v keyfile| xargs sed -i "s/^keyFile/#keyFile/g"

啟動之后依次連接每個分片的主執行以下操作:

>config = {
    _id: "test1",
    members: [{
        _id: 0,
        host: "192.168.100.101:10001"
    }, {
        _id: 1,
        host: "192.168.100.102:10002"
    }, {
        _id: 2,
        host: "192.168.100.103:10003",
        arbiterOnly: true
    }]
}
>rs.initiate(config)
>rs.status()
>db.isMaster( )
>use admin;
>db.createRole({role:"superman", privileges:[{resource:{anyResource: true}, actions:["anyAction"]}], roles:["root"]})
>db.createUser({user:"test",pwd:"test",roles:[{role:"superman", db:"admin"}]})

依次執行成功之后,

停止所有的mongod節點:

for i in `seq 10`;do killall mongod ;done

開啟keyfile

ls |grep -v keyfile| xargs sed -i "s/^#keyFile/keyFile/g"

之后依次啟動mongod節點

 

然后啟動mongos節點

連接mongos節點

mongos>use admin;
mongos>db.auth("test","test")
mongos>sh.addShard("test1/192.168.100.101:10001")
mongos>sh.addShard("test2/192.168.100.103:20001")
mongos>sh.status()

至此分片配置完成。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM