Mongodb3.4.7搭建高可用集群
Posted 原地没有风
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mongodb3.4.7搭建高可用集群相关的知识,希望对你有一定的参考价值。
部署Mongodb高可用集群
准备
按照官方说明,至少需要3个config server,2个mongos,2个shard,每个shard至少2个副本,就是4个shard,共需要9个Mongodb实例。这里只虚拟出2个机器节点,将不同的Mongodb实例部署在不同的端口上模拟实现。
config server | leo.zhi.1:10010,leo.zhi.2:10010 |
mongos server | leo.zhi.1:10020,leo.zhi.2:10020 |
shard server | leo.zhi.1:10001,leo.zhi.2:10002 |
leo.zhi.1和leo.zhi.2上分别创建文件夹
[[email protected] mongodb]# mkdir -p config/data [[email protected] mongodb]# mkdir -p config/log [[email protected] mongodb]# mkdir -p mongos/log [[email protected] mongodb]# mkdir -p shard1/data [[email protected] mongodb]# mkdir -p shard1/log [[email protected] mongodb]# mkdir -p shard2/data [[email protected] mongodb]# mkdir -p shard2/log
其中data目录存放数据,log目录存放日志
mongos服务不存放数据,所以不需要数据目录,只需要log目录
部署config server
Mongodb配置文件(采用YAML格式书写)
systemLog:
destination: file
path: "/usr/local/mongodb/config/log/mongod.log"
logAppend: true
storage:
dbPath: "/usr/local/mongodb/config/data"
journal:
enabled: true
directoryPerDB: true
net:
port: 10010
processManagement:
fork: true
pidFilePath: "/usr/local/mongodb/config/mongod.pid"
sharding:
clusterRole: configsvr
replication:
replSetName: configReplSet
1. 在每台服务器上分别启Mongodb实例
[[email protected] mongodb]# mongod --config config/mongod.conf about to fork child process, waiting until server is ready for connections. forked process: 3293 child process started successfully, parent exiting
2. 随便选个一个节点,登录mongo,初始化config server 的 replica set
[[email protected] mongodb]# mongo --port 10010 > rs.initiate( { _id: "configReplSet", configsvr: true, members:[ {_id:0,host:"leo.zhi.1:10010"}, {_id:1,host:"leo.zhi.2:10010"}] } )
... configReplSet:OTHER> exit [[email protected] mongodb]# mongo --port 10010 configReplSet:PRIMARY>
部署shard1
Mongodb配置文件(采用YAML格式书写)
systemLog:
destination: file
path: "/usr/local/mongodb/shard1/log/mongod.log"
logAppend: true
storage:
dbPath: "/usr/local/mongodb/shard1/data"
journal:
enabled: true
directoryPerDB: true
net:
port: 10001
processManagement:
fork: true
pidFilePath: "/usr/local/mongodb/shard1/mongod.pid"
sharding:
clusterRole: shardsvr
replication:
replSetName: shard1ReplSet
1. 在每个节点启动shard1
[[email protected] mongodb]# mongod --config shard1/mongod.conf
2. 随便选个一个节点,登录mongo,初始化shard1 的 replica set
[[email protected] mongodb]# mongo --port 10001 > rs.initiate( { _id: "shard1ReplSet", members:[ {_id:0,host:"leo.zhi.1:10001"}, {_id:1,host:"leo.zhi.2:10002"}] } ) ... shard1ReplSet:OTHER> exit [[email protected] mongodb]# mongo --port 10001 shard1ReplSet:PRIMARY>
部署shard2
Mongodb配置文件(采用YAML格式书写)
systemLog:
destination: file
path: "/usr/local/mongodb/shard2/log/mongod.log"
logAppend: true
storage:
dbPath: "/usr/local/mongodb/shard2/data"
journal:
enabled: true
directoryPerDB: true
net:
port: 10002
processManagement:
fork: true
pidFilePath: "/usr/local/mongodb/shard2/mongod.pid"
sharding:
clusterRole: shardsvr
replication:
replSetName: shard2ReplSet
其余操作步骤同部署shard1
部署mongos
以上是关于Mongodb3.4.7搭建高可用集群的主要内容,如果未能解决你的问题,请参考以下文章