网上关于三种集群方式的搭建方式很多,都是分开来介绍的。Replica Set (复制集成)主要是做主从库的,但是没法实现负载均衡的效果,真正实现这个的,是Sharding(分片集群),通过数据分布在每个分片上而实现。所以,如果只用分片,如果一个只有一个主库,那么挂了就真的挂了。所以在我尝试的集群部署采用的Replica Set+Sharding的方式。OS是Redhat_x64系统,客户端用的是Java客户端。Mongodb版本是mongodb-linux-x86_64-2.4.9.tgz。
要构建一个 MongoDB Sharding Cluster,需要三种角色:
l Shard Server: mongod 实例,用于存储实际的数据块,实际生产环境中一个shard server角色可由几台机器组个一个relica set承担,防止主机单点故障
l Config Server: mongod 实例,存储了整个 Cluster Metadata,其中包括 chunk 信息。
l Route Server: mongos 实例,前端路由,客户端由此接入,且让整个集群看上去像单一数据库,前端应用可以透明使用。
1. 分别在3台机器运行一个mongod实例(称为mongod shard11,mongod shard12,mongod shard13)组织replica set1,作为cluster的shard1
2. 1台机器运行一个mongod实例(称为mongod shard22,单机作为cluster的shard2
3. 每台机器运行一个mongod实例,作为3个config server
4. 每台机器运行一个mongs进程,用于客户端连接
| 主机 |
IP |
端口信息 |
| Server1 |
172.17.253.216 |
mongod shard11:27017 mongod config1:20000 mongs1:30000 |
| Server2 |
172.17.253.217 |
mongod shard12:27017 mongod shard22:27018 mongod config2:20000 mongs2:30000 |
| Server3 |
172.17.253.67 |
mongod shard13:27017 mongod config3:20000 mongs3:30000 |
2.1软件准备
1. 安装monodb软件
su – mongodb
tar zxvf mongodb-linux-x86_64-2.4.9.tgz
创建数据目录
根据本例sharding架构图所示,在各台sever上创建shard数据文件目录
Server1:
cd /opt/mongodb
mkdir -p data/shard11
Server2:
cd /opt/mongodb
mkdir -p data/shard12
mkdir -p data/shard22
Server3:
cd /opt/mongodb
mkdir -p data/shard13
2.2配置relica sets(复制集)
![\]()
![\]()
1. 配置shard1所用到的replica sets:
方式一:
Server1:
cd /opt/mongodb/bin
./mongod –shardsvr –replSet shard1 –port 27017 –dbpath /mongodb/data/shard11 –oplogSize 100 –logpath /mongodb/data/shard11.log –logappend –fork
Server2:
cd /opt/mongodb/bin
./mongod –shardsvr –replSet shard1 –port 27017 –dbpath /mongodb/data/shard12 –oplogSize 100 –logpath /mongodb/data/shard12.log –logappend –fork
Server3:
cd /opt/mongodb/bin
./mongod –shardsvr –replSet shard1 –port 27017 –dbpath /mongodb/data/shard13 –oplogSize 100 –logpath /mongodb/data/shard13.log –logappend –fork
方式二:
由于配置文件比较多,建议写在文件中
Server1:
vim /opt/mongodb/bin/shard11.conf
#shard11.conf
dbpath=/opt/mongodb/data/shard11
logpath = /opt/mongodb/data/shard11.log
pidfilepath = /opt/mongdb/shard11.pid
directoryperdb = true
logappend = true
replSet = shard1
bind_ip=172.17.253.216
port = 27017
oplogSize = 100
fork = true
noprealloc=true
cd /opt/mongodb/bin
./mongod -shardsvr -f shard11.conf
当看到下面的内容表示启动成功:
about to fork child process, waiting until server is ready for connections.
all output going to: /opt/mongodb/data/shard11.log
forked process: 14867
child process started successfully, parent exiting
Server2:同理
vim /opt/mongodb/bin/shard12.conf
#shard12.conf
dbpath=/opt/mongodb/data/shard12
logpath = /opt/mongodb/data/shard12.log
pidfilepath = /opt/mongdb/shard12.pid
directoryperdb = true
logappend = true
replSet = shard1
bind_ip=172.17.253.217
port = 27017
oplogSize = 100
fork = true
noprealloc=true
cd /opt/mongodb/bin
./mongod -shardsvr -f shard12.conf
Server3:同理
vim /opt/mongodb/bin/shard12.conf
#shard13.conf
dbpath=/opt/mongodb/data/shard13
logpath = /opt/mongodb/data/shard13.log
pidfilepath = /opt/mongdb/shard13.pid
directoryperdb = true
logappend = true
replSet = shard1
bind_ip=172.17.253.67
port = 27017
oplogSize = 100
fork = true
noprealloc=true
cd /opt/mongod