準備三臺服務器,部署方案以下:mongodb
一、安裝mongodb,詳細不用說;數據庫
二、建立實例分別建立目錄config、configServer、shard一、shard二、shard三、logs,分別存放實例配置文件服務器
三、配置服務器實例configServer.conf:spa
systemLog: destination: file logAppend: true path: /usr/local/server/mongodb/logs/configServer.log storage: dbPath: /usr/local/server/mongodb/configServer/data journal: enabled: true directoryPerDB: true #是否一個庫一個文件夾 engine: wiredTiger #數據引擎 wiredTiger: #WT引擎配置 engineConfig: cacheSizeGB: 2 #設置爲4G,默認爲物理內存的一半 directoryForIndexes: true #是否將索引也按數據庫名單獨存儲 journalCompressor: zlib collectionConfig: #表壓縮配置 blockCompressor: zlib indexConfig: #索引配置 prefixCompression: true processManagement: #進程管理 fork: true pidFilePath: /usr/local/server/mongodb/configServer/mongod.pid net: port: 29004 bindIp: 0.0.0.0 sharding: clusterRole: configsvr replication: replSetName: configReplSet
四、配置路由服務器 mongos.conf
systemLog: destination: file logAppend: true path: /usr/local/server/mongodb/logs/mongos.log processManagement: fork: true pidFilePath: /usr/local/server/mongodb/mongos/mongod.pid net: port: 29005 bindIp: 0.0.0.0 sharding: configdb: configReplSet/192.168.131.8:29004,192.168.131.9:29004,192.168.131.10:29004
五、配置分片服務器shard一、shard二、shard3
配置文件 shard1.conf:
systemLog: destination: file logAppend: true path: /usr/local/server/mongodb/logs/shard1.log storage: dbPath: /usr/local/server/mongodb/shard1/data journal: enabled: true directoryPerDB: true engine: wiredTiger wiredTiger: engineConfig: cacheSizeGB: 2 directoryForIndexes: true journalCompressor: zlib collectionConfig: blockCompressor: zlib indexConfig: prefixCompression: true processManagement: fork: true pidFilePath: /usr/local/server/mongodb/shard1/mongod.pid net: port: 29001 bindIp: 0.0.0.0 replication: replSetName: shard1 sharding: clusterRole: shardsvr archiveMovedChunks: true
配置shard2.conf:
systemLog: destination: file logAppend: true path: /usr/local/server/mongodb/logs/shard2.log storage: dbPath: /usr/local/server/mongodb/shard2/data journal: enabled: true directoryPerDB: true #是否一個庫一個文件夾 engine: wiredTiger #數據引擎 wiredTiger: engineConfig: cacheSizeGB: 2 directoryForIndexes: true journalCompressor: zlib collectionConfig: blockCompressor: zlib indexConfig: prefixCompression: true processManagement: #進程管理 fork: true pidFilePath: /usr/local/server/mongodb/shard2/mongod.pid net: port: 29002 bindIp: 0.0.0.0 replication: replSetName: shard2 sharding: clusterRole: shardsvr archiveMovedChunks: true
配置shard3.conf:
systemLog: destination: file logAppend: true path: /usr/local/server/mongodb/logs/shard3.log storage: dbPath: /usr/local/server/mongodb/shard3/data journal: enabled: true directoryPerDB: true #是否一個庫一個文件夾 engine: wiredTiger #數據引擎 wiredTiger: engineConfig: cacheSizeGB: 2 directoryForIndexes: true journalCompressor: zlib collectionConfig: blockCompressor: zlib indexConfig: prefixCompression: true processManagement: #進程管理 fork: true pidFilePath: /usr/local/server/mongodb/shard3/mongod.pid net: port: 29003 bindIp: 0.0.0.0 replication: replSetName: shard3 sharding: clusterRole: shardsvr archiveMovedChunks: true
六、分別啓動mongodb啓動實例
/usr/local/server/mongodb/bin/mongos --config /usr/local/server/mongodb/config/mongos.conf
/usr/local/server/mongodb/bin/mongod --config /usr/local/server/mongodb/config/configServer.conf
/usr/local/server/mongodb/bin/mongod --config /usr/local/server/mongodb/config/shard1.conf
/usr/local/server/mongodb/bin/mongod --config /usr/local/server/mongodb/config/shard2.conf
/usr/local/server/mongodb/bin/mongod --config /usr/local/server/mongodb/config/shard3.conf3d
七、配置服務器/usr/local/server/mongodb/bin/mongo --port 29004server
use adminblog
添加成員:索引
rs.initiate(
{
_id: "configReplSet",
configsvr: true,
members: [
{ _id : 1, host : "192.168.131.8:29004", priority: 30 },
{ _id : 2, host : "192.168.131.10:29004", priority: 20 },
{ _id : 3, host : "192.168.131.9:29004", priority: 10 }
]
}
);進程
八、分別配置分片服務器內存
/usr/local/server/mongodb/bin/mongo --port 29001 鏈接shard1
use admin;
rs.initiate({_id:'shard1',members:[
{_id:1,host:'192.168.131.8:29001', priority: 30},
{_id:2,host:'192.168.131.10:29001', priority: 20},
{_id:3,host:'192.168.131.9:29001', priority: 0,arbiterOnly: true}
]});
其餘分片服務器同樣配置,略。。。
九、配置路由服務器
/usr/local/server/mongodb/bin/mongo --port 29005;
use admin;
路由服務器註冊分片:
db.runCommand({addshard: "shard1/192.168.131.8:29001,192.168.131.9:29001,192.168.131.10:29001"});
db.runCommand({addshard: "shard2/192.168.131.8:29002,192.168.131.9:29002,192.168.131.10:29002"});
db.runCommand({addshard: "shard3/192.168.131.8:29003,192.168.131.9:29003,192.168.131.10:29003"});
# 查看
db.printShardingStatus()
啓動分片數據庫test:
db.runCommand({enablesharding: "test"});
設置分片鍵:
sh.shardCollection("test.use",{"usename":1});
到此配置完成。