Windows下搭建Mongo4.0复制集群
2019-08-09 本文已影响0人
renmen2000
背景:
MongoDB在4.0版本,移除了主从数据库相关配置,官方推荐采用 replica set (副本集群)来代替
根据MongoDB官网文档的描述,当系统数据量较大时,不建议采用 mongodump 和 mongorestore 命令来备份和恢复,使用replica set更为合理。一般是3个MongoDB服务作为一组集群
环境:
Windows10
下载地址:
https://www.mongodb.com/download-center/community
image选择需要的版本下载见上图
安装文档参考:
https://docs.mongodb.com/manual/tutorial/install-mongodb-on-windows/
安装完成后设置环境变量
image将MONGO_HOME\bin目录添加到path路径里
image集群准备:
创建三个目录分别用于存放节点数据和日志
我创建的路径是E:\MongoDB\rs0-2
image在rs0、rs1、rs2下分别创建配置文件mongod.cfg以及空文件夹data、log
三个mongod.cfg配置文件内容分别为
rs0-mongod.cfg
# mongod.conf
# for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/
# Where and how to store data.
storage:
dbPath: E:/MongoDB/rs0/data
journal:
enabled: true
# engine:
# mmapv1:
# wiredTiger:
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: E:/MongoDB/rs0/log/mongod.log
# network interfaces
net:
port: 27017
bindIp: 127.0.0.1
#processManagement:
#security:
#operationProfiling:
replication:
replSetName: rs0
#sharding:
## Enterprise-Only Options:
#auditLog:
#snmp:
rs1-mongod.cfg
# mongod.conf
# for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/
# Where and how to store data.
storage:
dbPath: E:/MongoDB/rs1/data
journal:
enabled: true
# engine:
# mmapv1:
# wiredTiger:
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: E:/MongoDB/rs1/log/mongod.log
# network interfaces
net:
port: 27018
bindIp: 127.0.0.1
#processManagement:
#security:
#operationProfiling:
replication:
replSetName: rs0
#sharding:
## Enterprise-Only Options:
#auditLog:
#snmp:
rs2-mongod.cfg
# mongod.conf
# for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/
# Where and how to store data.
storage:
dbPath: E:/MongoDB/rs2/data
journal:
enabled: true
# engine:
# mmapv1:
# wiredTiger:
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: E:/MongoDB/rs2/log/mongod.log
# network interfaces
net:
port: 27019
bindIp: 127.0.0.1
#processManagement:
#security:
#operationProfiling:
replication:
replSetName: rs0
#sharding:
## Enterprise-Only Options:
#auditLog:
#snmp:
分别进入rs0,rs1,rs2目录启动mongod启动命令为:
mongod --config mongod.cfg
启动完成后进入mongo安装目录启动mongo客户端连接r0服务的端口27017
成功连接后,开始初始化集群
集群初始化
在客户端命令行窗口输入如下配置
rsconf = {
_id: "rs0",
members: [
{
_id: 0,
host: "127.0.0.1:27017"
},
{
_id: 1,
host: "127.0.0.1:27018"
},
{
_id: 2,
host: "127.0.0.1:27019"
}
]
}
执行命令
rs.initiate( rsconf )
输入命令(查看集群状态)
rs.conf()
可见rs0为主节点,rs1和rs2为复制节点
End