說明:3臺(tái)機(jī)器10.10.73.225,10.10.73.186,10.10.73.226
3個(gè)分片寒跳。每個(gè)分片一主一副一仲裁
mongo 版本 mongodb-linux-x86_64-3.2.7.tgz
- 每個(gè)機(jī)器上創(chuàng)建如下目錄
mkdir -p /data/mongodb/mongos/log
mkdir -p /data/mongodb/config/{data,log}
mkdir -p /data/mongodb/shard{1,2,3}/{data,log}
2.配置文件
config.conf
systemLog:
destination: file
###日志存儲(chǔ)位置
path: /data/mongodb/config/log/config.log
logAppend: true
storage:
##journal配置
journal:
enabled: true
##數(shù)據(jù)文件存儲(chǔ)位置
dbPath: /data/mongodb/config/data/
##是否一個(gè)庫一個(gè)文件夾
directoryPerDB: true
##數(shù)據(jù)引擎
engine: wiredTiger
##WT引擎配置
wiredTiger:
engineConfig:
##WT最大使用cache(根據(jù)服務(wù)器實(shí)際情況調(diào)節(jié))
cacheSizeGB: 4
##是否將索引也按數(shù)據(jù)庫名單獨(dú)存儲(chǔ)
directoryForIndexes: true
##表壓縮配置
collectionConfig:
blockCompressor: zlib
##索引配置
indexConfig:
prefixCompression: true
##端口配置
net:
port: 21000
##分片配置
sharding:
##分片角色
clusterRole: configsvr
mongos.conf
##日志配置
systemLog:
destination: file
##日志位置
path: /data/mongodb/mongos/log/mongos.log
logAppend: true
##網(wǎng)路配置
net:
##端口配置
port: 20000
##分片配置
sharding:
##指定config server
configDB: 10.10.73.225:21000,10.10.73.186:21000,10.10.73.226:21000
shard1.conf
systemLog:
destination: file
###日志存儲(chǔ)位置
path: /data/mongodb/shard1/log/shard1.log
logAppend: true
storage:
##journal配置
journal:
enabled: true
##數(shù)據(jù)文件存儲(chǔ)位置
dbPath: /data/mongodb/shard1/data/
##是否一個(gè)庫一個(gè)文件夾
directoryPerDB: true
##數(shù)據(jù)引擎
engine: wiredTiger
##WT引擎配置
wiredTiger:
engineConfig:
##WT最大使用cache(根據(jù)服務(wù)器實(shí)際情況調(diào)節(jié))
cacheSizeGB: 4
##是否將索引也按數(shù)據(jù)庫名單獨(dú)存儲(chǔ)
directoryForIndexes: true
##表壓縮配置
collectionConfig:
blockCompressor: zlib
##索引配置
indexConfig:
prefixCompression: true
##端口配置
net:
port: 22001
replication:
##oplog大小
oplogSizeMB: 20480
##復(fù)制集名稱
replSetName: shard1
##分片配置
sharding:
##分片角色
clusterRole: shardsvr
shard2.conf
systemLog:
destination: file
###日志存儲(chǔ)位置
path: /data/mongodb/shard2/log/shard2.log
logAppend: true
storage:
##journal配置
journal:
enabled: true
##數(shù)據(jù)文件存儲(chǔ)位置
dbPath: /data/mongodb/shard2/data/
##是否一個(gè)庫一個(gè)文件夾
directoryPerDB: true
##數(shù)據(jù)引擎
engine: wiredTiger
##WT引擎配置
wiredTiger:
engineConfig:
##WT最大使用cache(根據(jù)服務(wù)器實(shí)際情況調(diào)節(jié))
cacheSizeGB: 4
##是否將索引也按數(shù)據(jù)庫名單獨(dú)存儲(chǔ)
directoryForIndexes: true
##表壓縮配置
collectionConfig:
blockCompressor: zlib
##索引配置
indexConfig:
prefixCompression: true
##端口配置
net:
port: 22002
replication:
##oplog大小
oplogSizeMB: 20480
##復(fù)制集名稱
replSetName: shard2
##分片配置
sharding:
##分片角色
clusterRole: shardsvr
shard3.conf
systemLog:
destination: file
###日志存儲(chǔ)位置
path: /data/mongodb/shard3/log/shard3.log
logAppend: true
storage:
##journal配置
journal:
enabled: true
##數(shù)據(jù)文件存儲(chǔ)位置
dbPath: /data/mongodb/shard3/data/
##是否一個(gè)庫一個(gè)文件夾
directoryPerDB: true
##數(shù)據(jù)引擎
engine: wiredTiger
##WT引擎配置
wiredTiger:
engineConfig:
##WT最大使用cache(根據(jù)服務(wù)器實(shí)際情況調(diào)節(jié))
cacheSizeGB: 4
##是否將索引也按數(shù)據(jù)庫名單獨(dú)存儲(chǔ)
directoryForIndexes: true
##表壓縮配置
collectionConfig:
blockCompressor: zlib
##索引配置
indexConfig:
prefixCompression: true
##端口配置
net:
port: 22003
replication:
##oplog大小
oplogSizeMB: 20480
##復(fù)制集名稱
replSetName: shard3
##分片配置
sharding:
##分片角色
clusterRole: shardsvr
3.安裝
每臺(tái)機(jī)器執(zhí)行
a.啟動(dòng)configserver 和 mongos
./mongod -f ../conf/config.conf -fork
./mongos -f ../conf/mongos.conf -fork
b.啟動(dòng)分片
./mongod -f ../conf/shard1.conf -fork
./mongod -f ../conf/shard2.conf -fork
./mongod -f ../conf/shard3.conf -fork
任意一臺(tái)機(jī)器執(zhí)行(除了仲裁節(jié)點(diǎn))
a.初始化分片配置--應(yīng)當(dāng)設(shè)置優(yōu)先級(jí)
./mongo --port 22001
use admin
config = {_id:"shard1",members:[{_id:0,host:"10.10.73.225:22001","priority":9},{_id:1,host:"10.10.73.186:22001","priority":8},{_id:2,host:"10.10.73.226:22001",arbiterOnly:true}]}
rs.initiate(config);
./mongo --port 22002
use admin
config2 = {_id:"shard2",members:[{_id:0,host:"10.10.73.186:22002","priority":9},{_id:1,host:"10.10.73.226:22002","priority":8},{_id:2,host:"10.10.73.225:22002",arbiterOnly:true}]}
rs.initiate(config2);
./mongo --port 22003
use admin
config3 = {_id:"shard3",members:[{_id:0,host:"10.10.73.226:22003","priority":9},{_id:1,host:"10.10.73.186:22003",arbiterOnly:true},{_id:2,host:"10.10.73.225:22003","priority":8}]}
rs.initiate(config3);
b.串聯(lián)路由服務(wù)器與分配副本集
./mongo --port 20000
use admin
db.runCommand({addshard:"shard1/10.10.73.225:22001,10.10.73.186:22001,10.10.73.226:22001"});
db.runCommand({addshard:"shard2/10.10.73.225:22002,10.10.73.186:22002,10.10.73.226:22002"});
db.runCommand({addshard:"shard3/10.10.73.225:22003,10.10.73.186:22003,10.10.73.226:22003"});
c.查看分片服務(wù)器的配置
db.runCommand({listshards:1});
安裝完成
4.插入測試
./mongo --port 20000
use admin
創(chuàng)建數(shù)據(jù)庫绣的,指定數(shù)據(jù)庫'testdb'進(jìn)行分片生效
db.runCommand( { enablesharding :"testdb"});
指定數(shù)據(jù)庫需要分片鍵片
db.runCommand({shardcollection:"testdb.table1",key:{_id:'hashed'}})
###創(chuàng)建Hash片鍵遭顶,指定'testdb'數(shù)據(jù)庫中的'table1'表中的數(shù)據(jù)進(jìn)行分片
##插入
for(var i=1;i<10000000;i++) db.table1.save({"a":i+"wwe","acId":"45334"+i,"areaCode":"3346"+i,"deliverd":true,"destId":"13346"+i,"doneTime":"6334"+i,"dup":0,"gwId":"2","gwMsgId":"6334"+i,"mobile":"166673","msgType":"UmsReportReq"+i,"operator":"ChinaMobile","priority":0,"reqId":"1","revTime":"245"+i,"smscSeq":0,"srcReqId":"06235"+i,"status":"DELIVRD","subTime":"26230"+i,"submitTime":"1062348"+i,"traceNo":"92363"+i});
##查看狀態(tài)
db.table1.stats();