MongoDB的下载和安装
请参考我的博文:MongoDB实现复制集 中所述MongoDB下载和安装
分片复制集节点架构
创建分片复制集设置
准备三台虚拟机mongodb01,mongodb02,mongodb03。CentOS 7修改主机名:
$ hostnamectl set-hostname mongodb01
$ hostnamectl set-hostname mongodb02
$ hostnamectl set-hostname mongodb03
创建配置服务器
在mongodb01,mongodb02,mongodb03中分别启动配置服务器
$ mkdir -p /opt/datas/mongodb/cluster/config/data
$ mkdir -p /opt/datas/mongodb/cluster/config/logs
$ touch /opt/datas/mongodb/cluster/config/logs/config.log
$ bin/mongod --port 27052 --configsvr --dbpath /opt/datas/mongodb/cluster/config/data --fork --logpath /opt/datas/mongodb/cluster/config/logs/config.log
创建分片控制器
在mongodb01,mongodb02,mongodb03中分别启动分片控制器
$ touch /opt/datas/mongodb/cluster/config/logs/mongos.log
$ bin/mongos --configdb mongodb01:27052,mongodb02:27052,mongodb03:27052 --port 27051 --chunkSize 1 --fork --logpath /opt/datas/mongodb/sharding/config/logs/mongos.log
启动分片00复制集
在mongodb01,mongodb02,mongodb03中分别启动分片00
$ mkdir -p /opt/datas/mongodb/cluster/shard00/data
$ mkdir -p /opt/datas/mongodb/cluster/shard00/logs
$ touch /opt/datas/mongodb/cluster/shard00/logs/shard00.log
$ bin/mongod --dbpath /opt/datas/mongodb/cluster/shard00/data --port 27053 --replSet sh00 --fork --logpath /opt/datas/mongodb/cluster/shard00/logs/shard00.log
添加复制集
$ bin/mongo mongodb01:27053
> rs.initiate()
sh00:OTHER> rs.add("mongodb01:27053");
sh00:PRIMARY> rs.add("mongodb02:27053");
sh00:PRIMARY> rs.add("mongodb03:27053");
sh00:PRIMARY> rs.status();
启动分片01复制集
在mongodb01,mongodb02,mongodb03中分别启动分片01
$ mkdir -p /opt/datas/mongodb/cluster/shard01/data
$ mkdir -p /opt/datas/mongodb/cluster/shard01/logs
$ touch /opt/datas/mongodb/cluster/shard01/logs/shard01.log
$ bin/mongod --dbpath /opt/datas/mongodb/cluster/shard01/data --port 27054 --replSet sh01 --fork --logpath /opt/datas/mongodb/cluster/shard01/logs/shard01.log
添加复制集
$ bin/mongo mongodb01:27054
> rs.initiate()
sh01:OTHER> rs.add("mongodb01:27054");
sh01:PRIMARY> rs.add("mongodb02:27054");
sh01:PRIMARY> rs.add("mongodb03:27054");
sh01:PRIMARY> rs.status();
启动分片02复制集
在mongodb01,mongodb02,mongodb03中分别启动分片02
$ mkdir -p /opt/datas/mongodb/cluster/shard02/data
$ mkdir -p /opt/datas/mongodb/cluster/shard02/logs
$ touch /opt/datas/mongodb/cluster/shard02/logs/shard02.log
$ bin/mongod --dbpath /opt/datas/mongodb/cluster/shard02/data --port 27055 --replSet sh02 --fork --logpath /opt/datas/mongodb/cluster/shard02/logs/shard02.log
添加复制集
$ bin/mongo mongodb01:27055
> rs.initiate()
sh02:OTHER> rs.add("mongodb01:27055");
sh02:PRIMARY> rs.add("mongodb02:27055");
sh02:PRIMARY> rs.add("mongodb03:27055");
sh02:PRIMARY> rs.status();
添加分片集
登录到mongodb01分片控制器
$ bin/mongo mongodb01:27051
mongos> sh.addShard("sh00/mongodb01:27053,mongodb02:27053,mongodb03:27053");
mongos> sh.addShard("sh01/mongodb01:27054,mongodb02:27054,mongodb03:27054");
mongos> sh.addShard("sh02/mongodb01:27055,mongodb02:27055,mongodb03:27055");
MongoDB分片和复制集操作参考
使用MongoDB实现分片集
MongoDB实现复制集