在6台机器上使用MongDB3.4副本集分片集群搭建

一、运行环境

操作系统:虚拟机+windows
mongodb:3.4

二、机器和端口分配


在6台机器上使用MongDB3.4副本集分片集群搭建_第1张图片


在6台机器上使用MongDB3.4副本集分片集群搭建_第2张图片


三、创建相关目录

创建相关目录:
三台主机分别新建如下目录:
d:/mongodb/config/data
d:/mongodb/config/log/
d:/mongodb/mongos/log
六台机器分别创建 表1 对应的分片,每台机器三个分片
d:/mongodb/shard0X/data
d:/mongodb/shard0X/log

四、配置环境变量

MONGODB_HOME=E:\MongoDB\Server\3.4
Path中追加 ;%MONGODB_HOME%\bin;

五、启动参数放于配置文件

config.conf

dbpath=D:/mongodb/config/data
logpath=D:/mongodb/config/log/config.log
port=17000
configsvr=true
replSet=cfgsvr

mongos.conf

logpath=D:/mongodb/mongos/log/mongos.log
port=18000
configdb=cfgsvr/192.168.10.102:17000,192.168.10.44:17000,192.168.10.45:17000  #必须是1个或则3个配置 。
logappend=true

shard01.conf

shardsvr=true
replSet=shard01
port=18001
dbpath=D:/mongodb/shard01/data
logpath=D:/mongodb/shard01/log/shard01.log

shard02.conf

shardsvr=true
replSet=shard02
port=18002
dbpath=D:/mongodb/shard02/data
logpath=D:/mongodb/shard02/log/shard02.log

shard03.conf

shardsvr=true
replSet=shard03
port=18003
dbpath=D:/mongodb/shard03/data
logpath=D:/mongodb/shard03/log/shard03.log

shard04.conf

shardsvr=true
replSet=shard04
port=18004
dbpath=D:/mongodb/shard04/data
logpath=D:/mongodb/shard04/log/shard04.log

shard05.conf

shardsvr=true
replSet=shard05
port=18005
dbpath=D:/mongodb/shard05/data
logpath=D:/mongodb/shard05/log/shard05.log

shard06.conf

shardsvr=true
replSet=shard06
port=18006
dbpath=D:/mongodb/shard06/data
logpath=D:/mongodb/shard06/log/shard06.log

六、启动命令(将文件夹、配置文件启动命令的bat拷贝到相应的计算器上面)

mongod -f d:/mongodb/config.conf
mongos -f d:/mongodb/mongos.conf
mongod -f d:/mongodb/shard02.conf
mongod -f d:/mongodb/shard02.conf
mongod -f d:/mongodb/shard03.conf
mongod -f d:/mongodb/shard04.conf
mongod -f d:/mongodb/shard05.conf
mongod -f d:/mongodb/shard06.conf

七、启动顺序

1、启动机器1.2.3的Config Server
2、启动1.2.3的mongos
3、按照规划,分别启动各个机器的shard节点

八、配置

在其中一台机器上面执行即可
mongo 192.168.10.43:18001/admin
config = { _id:"shard01", members:[
{_id:0,host:"192.168.10.43:18001"},
{_id:1,host:"192.168.10.48:18001"},
{_id:2,host:"192.168.10.47:18001"}]
};
rs.initiate(config);
exit

mongo 192.168.10.44:18002/admin
config = { _id:"shard02", members:[
{_id:0,host:"192.168.10.44:18002"},
{_id:1,host:"192.168.10.43:18002"},
{_id:2,host:"192.168.10.48:18002"}]
};
rs.initiate(config);
exit

mongo 192.168.10.45:18003/admin
config = { _id:"shard03", members:[
{_id:0,host:"192.168.10.45:18003"},
{_id:1,host:"192.168.10.44:18003"},
{_id:2,host:"192.168.10.43:18003"}]
};
rs.initiate(config);
exit

mongo 192.168.10.46:18004/admin
config = { _id:"shard04", members:[
{_id:0,host:"192.168.10.46:18004"},
{_id:1,host:"192.168.10.45:18004"},
{_id:2,host:"192.168.10.44:18004"}]
};
rs.initiate(config);
exit

mongo 192.168.10.47:18005/admin
config = { _id:"shard05", members:[
{_id:0,host:"192.168.10.47:18005"},
{_id:1,host:"192.168.10.46:18005"},
{_id:2,host:"192.168.10.45:18005"}]
};
rs.initiate(config);
exit

mongo 192.168.10.48:18006/admin
config = { _id:"shard06", members:[
{_id:0,host:"192.168.10.48:18006"},
{_id:1,host:"192.168.10.47:18006"},
{_id:2,host:"192.168.10.46:18006"}]
};
rs.initiate(config);

#任意一台配置服务器
mongo 192.168.10.43:17000
 config = {_id:"cfgsvr", configsvr:true, members:[
{_id:0, host:"192.168.10.43:17000"},
{_id:0, host:"192.168.10.44:17000"},
{_id:0, host:"192.168.10.45:17000"}
]}
rs.initiate(config)

#任意一台路由服务器
mongo 192.168.10.43:18000
use  admin;
sh.addShard("shard01/192.168.10.43:18001,192.168.10.48:18001,192.168.10.47:18001");
sh.addShard("shard02/192.168.10.44:18002,192.168.10.43:18002,192.168.10.48:18002");
sh.addShard("shard03/192.168.10.45:18003,192.168.10.44:18003,192.168.10.43:18003");
sh.addShard("shard04/192.168.10.46:18004,192.168.10.45:18004,192.168.10.44:18004");
sh.addShard("shard05/192.168.10.47:18005,192.168.10.46:18005,192.168.10.45:18005");
sh.addShard("shard06/192.168.10.48:18006,192.168.10.47:18006,192.168.10.46:18006");

九、数据库和片键

mongo 127.0.0.1:18000
sh.enableSharding("testdb")
#hashed能保证数据平均分布
sh.shardCollection("testdb.car2",{id:"hashed"})
#查看数据库、数据表片键和分片情况
sh.status()

十、设置从节点可读

shard02:SECONDARY> db.getMongo().setSlaveOk();
或者
rs.slaveOk()

十一、查看表的分片状态

db.car2.stats()


你可能感兴趣的:(mongodb)