mongo 副本集+分片 配置
服务器规划如下:
副本集名称|服务器IP |
192.168.56.111 | 192.168.56.112 | 192.168.56.113 |
shard1 | 3201 | 3201 | 3201 |
shard2 | 3202 | 3202 | 3202 |
shred3 | 3203 | 3203 | 3203 |
mongos | 3200 | 3200 | |
conf1 | 3888 | 3888 | 3888 |
192.168.56.111,192.168.56.112,192.168.56.113 新建目录 mkdir -p /data/mongodb/shard1/db mkdir -p /data/mongodb/shard1/log mkdir -p /data/mongodb/shard2/db mkdir -p /data/mongodb/shard2/log mkdir -p /data/mongodb/shard3/db mkdir -p /data/mongodb/shard3/log mkdir -p /data/mongodb/mongos/db mkdir -p /data/mongodb/mongos/log mkdir -p /data/mongodb/conf1/db mkdir -p /data/mongodb/conf1/log
第一部分:配置服务器conf1 配置如下:
conf.yml配置文件: systemLog: destination: file logAppend: true path: /data/mongodb/conf1/log/mongo.log # Where and how to store data. storage: dbPath: /data/mongodb/conf1/db journal: enabled: true # how the process runs processManagement: fork: true pidFilePath: /data/mongodb/configsvr.pid # network interfaces net: port: 3888 #operationProfiling: replication: replSetName: config sharding: clusterRole: configsvr
启动config server ,启动3台
#启动命名 /usr/local/mongodb/bin/mongod -f conf.yml #登录任意一台配置服务器,初始化配置副本集 config={_id : 'config',members : [{_id : 1, host : '192.168.56.111:3888'},{_id : 2, host : '192.168.56.112:3888'},{_id : 3, host : '192.168.56.113:3888'}]} ; rs.initiate(config);
第二部分:分片+副本集
shard1配置文件,shard2,shard3配置文件参考shard1配置文件:
systemLog: destination: file logAppend: true path: /data/mongodb/shard1/log/mongo.log # Where and how to store data. storage: dbPath: /data/mongodb/shard1/db journal: enabled: true wiredTiger: engineConfig: cacheSizeGB: 1 # how the process runs processManagement: fork: true pidFilePath: /data/mongodb/shard1.pid # network interfaces net: port: 3201 #operationProfiling: replication: replSetName: shard1 sharding: clusterRole: shardsvr
启动shard1 副本集: shard2,shard3启动参考shard1
/usr/local/mongodb/bin/mongod -f shard1.yml 登录任意一台配置服务器,初始化shard1,shard2,shard3副本集 config={_id : 'shard1',members : [{_id : 1, host : '192.168.56.111:3201'},{_id : 2, host : '192.168.56.112:3201'},{_id : 3, host : '192.168.56.113:3201'}]}; rs.initiate(config); config={_id : 'shard2',members : [{_id : 1, host : '192.168.56.111:3202'},{_id : 2, host : '192.168.56.112:3202'},{_id : 3, host : '192.168.56.113:3202'}]}; rs.initiate(config); config={_id : 'shard3',members : [{_id : 1, host : '192.168.56.111:3203'},{_id : 2, host : '192.168.56.112:3203'},{_id : 3, host : '192.168.56.113:3203'}]}; rs.initiate(config);
第三部分:配置路由服务器
mongos配置文件:
systemLog: destination: file logAppend: true path: /data/mongodb/mongos/log/mongo.log processManagement: fork: true # network interfaces net: port: 3200 sharding: configDB: config/192.168.56.111:3888,192.168.56.112:3888,192.168.56.113:3888 #config配置服务器副本集名称 192.168.56.112:3888
启动路由服务:
/usr/local/mongodb/bin/mongos -f mongos.yml #登录路由服务器添加分片: mongo 192.168.56.111:3200 use admin; sh.addShard("shard1/192.168.56.111:3201,192.168.56.112:3201,192.168.56.113:3201"); sh.addShard("shard2/192.168.56.111:3202,192.168.56.112:3202,192.168.56.113:3202"); sh.addShard("shard3/192.168.56.111:3203,192.168.56.112:3203,192.168.56.113:3203"); sh.status();