mongo版本:mongodb-linux-x86_64-4.0.16.tgz
三臺(tái)虛擬機(jī)
192.168.150.133 192.168.150.134 192.168.150.135
集群環(huán)境
2個(gè)分片復(fù)制集
shard1(192.168.150.133:27017 192.168.150.134:27017 192.168.150.135:27017)
shard2(192.168.150.133:27018 192.168.150.134:27018 192.168.150.135:27018)
解壓/opt/mongodb目錄下
tar -zxvf mongodb-linux-x86_64-4.0.16.tgz
創(chuàng)建/opt/mongo配置目錄
添加(yidian_repl)復(fù)制集配置文件:mongo.conf (133/134/135)
#守護(hù)進(jìn)程模式
fork = true
##數(shù)據(jù)目錄
dbpath=/opt/mongo/data/db
##端口
port = 27017
bind_ip=0.0.0.0
##日志文件
logpath=/opt/mongo/logs/mongodb1.log
##日志追加
logappend=true
##復(fù)制集的名字
replSet=yidian_repl
smallfiles=true
#分片集群必須要的屬性
shardsvr=true
添加(yidian_repl2)復(fù)制集配置文件:mongo2.conf
(133/134/135)
#守護(hù)進(jìn)程模式
fork = true
##數(shù)據(jù)目錄
dbpath=/opt/mongo/data/db
##端口
port = 27018
bind_ip=0.0.0.0
##日志文件
logpath=/opt/mongo/logs/mongodb2.log
##日志追加
logappend=true
##復(fù)制集的名字
replSet=yidian_repl2
smallfiles=true
#分片集群必須要的屬性
shardsvr=true
啟動(dòng)副本集(yidian_repl)
/opt/mongodb/mongodb-linux-x86_64-4.0.16/bin/mongod -f /opt/mongo/mongo.conf
啟動(dòng)副本集(yidian_repl2)
/opt/mongodb/mongodb-linux-x86_64-4.0.16/bin/mongod -f /opt/mongo/mongo2.conf
登錄復(fù)制集,添加初始化配置
進(jìn)入mongo客戶端
配置(yidian_repl)
/opt/mongodb/mongodb-linux-x86_64-4.0.16/bin/mongo -port 27017
進(jìn)入27017客戶端后,執(zhí)行初始化命令
var rsconf = { _id:"yidian_repl", members:[
{_id:1,host:"192.168.150.133:27017"},
{_id:2,host:"192.168.150.134:27017"},
{_id:3,host:"192.168.150.135:27017"}
]
}
rs.initiate(rsconf);
rs.status();//查看狀態(tài)
配置(yidian_rep2)
/opt/mongodb/mongodb-linux-x86_64-4.0.16/bin/mongo -port 27018
進(jìn)入27018客戶端后,執(zhí)行初始化命令
var rsconf = { _id:"yidian_repl2", members:[
{_id:1,host:"192.168.150.133:27018"},
{_id:2,host:"192.168.150.134:27018"},
{_id:3,host:"192.168.150.135:27018"}
]
}
rs.initiate(rsconf);
搭建config節(jié)點(diǎn)復(fù)制集
創(chuàng)建config節(jié)點(diǎn)配置文件:mongo-cfg.conf (133/134/135)
systemLog:
destination: file
#日志存儲(chǔ)位置
path: "/opt/mongo/mongo-cfg/logs/mongodb.log"
logAppend: true
storage:
journal:
enabled: true
#數(shù)據(jù)文件存儲(chǔ)位置
dbPath: "/opt/mongo/mongo-cfg/data"
#是否一個(gè)庫一個(gè)文件夾
directoryPerDB: true
#WT引擎配置
wiredTiger:
engineConfig:
#WT最大使用cache(根據(jù)服務(wù)器實(shí)際情況調(diào)節(jié))
cacheSizeGB: 1
#是否將索引也按數(shù)據(jù)庫名單獨(dú)存儲(chǔ)
directoryForIndexes: true
#表壓縮配置
collectionConfig:
blockCompressor: zlib
#索引配置
indexConfig:
prefixCompression: true
#端口配置
net:
bindIp: 192.168.150.133
port: 28018
replication:
oplogSizeMB: 2048
replSetName: csvr
sharding:
clusterRole: configsvr
processManagement:
fork: true
啟動(dòng)配置復(fù)制集
/opt/mongodb/mongodb-linux-x86_64-4.0.16/bin/mongod -f /opt/mongo/mongo-cfg.conf
初始化配置節(jié)點(diǎn)
登錄(任意一臺(tái))
/opt/mongodb/mongodb-linux-x86_64-4.0.16/bin/mongo -host 192.168.133 -port 28018
初始化命令
rs.initiate( {
_id : "csvr",
configsvr: true,
members: [
{ _id: 0, host: "192.168.150.133:28018" },
{ _id: 1, host: "192.168.150.134:28018" },
{ _id: 2, host: "192.168.150.135:28018" }
]
})
mongos節(jié)點(diǎn)配置
mongos配置文件:mongos.conf
systemLog:
destination: file
path: "/opt/mongo/mongos/log/mongos.log"
logAppend: true
net:
bindIp: 192.168.150.133
port: 28017
# 將confige server 添加到路由
sharding:
configDB: csvr/192.168.150.133:28018,192.168.150.134:28018,192.168.150.135:28018
processManagement:
fork: true
啟動(dòng)mongos
/opt/mongodb/mongodb-linux-x86_64-4.0.16/bin/mongos -config /opt/mongo/mongos/mongos.conf
登錄mongos節(jié)點(diǎn)
/opt/mongodb/mongodb-linux-x86_64-4.0.16/bin/mongo 192.168.150.133:28017
添加集群中的分片節(jié)點(diǎn)
切換admin:
use admin
添加shard1復(fù)制集
db.runCommand( { addshard : "yidian_repl/192.168.150.133:27017,192.168.150.134:27017,192.168.150.135:27017",name:"shard1"} )
添加shard2復(fù)制集
db.runCommand( { addshard : "yidian_repl2/192.168.150.133:27018,192.168.150.134:27018,192.168.150.135:27018",name:"shard2"} )
mongos查看分片狀態(tài):
sh.status()