准备三台服务器,部署方案如下:
1、安装mongodb,详细不用说;
2、创建实例分别创建目录config、configServer、shard1、shard2、shard3、logs,分别存放实例配置文件
3、配置服务器实例configServer.conf:
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
4、配置路由服务器 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
5、配置分片服务器shard1、shard2、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
6、分别启动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.conf
7、配置服务器/usr/local/server/mongodb/bin/mongo --port 29004
use admin
添加成员:
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 }
]
}
);
8、分别配置分片服务器
/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}
]});
其他分片服务器一样配置,略。。。
9、配置路由服务器
/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});
到此配置完成。