mongodb集群搭建
前置准备(安装包)
1.下载社区版 MongoDB 4.1.3
去官网下载对应的MongoDB 然后上传到Linux虚拟机
2.将压缩包解压即可
tar -zxvf MongoDB-linux-x86_64-4.1.3.tgz
第三步和第四步可不操作
3.启动
./bin/mongod
4.指定配置文件方式的启动
./bin/mongod -f mongo.conf
配置文件样例:
dbpath=/data/mongo/
port=27017
bind_ip=0.0.0.0
fork=true
logpath = /data/mongo/MongoDB.log
logappend = true
auth=false
以下是百度云盘的jar地址
image-20210129101045451.pnghttps://pan.baidu.com/s/1t4mGWxq4L9sDtjwnnGsnuQ
z9ek
一、架构
image-20210129110615733.png二、配置
2.1 配置 并启动config 节点集群
节点1 mongo_37017.conf
# 进入mondb 解压后目录
cd /root/mongodb-linux-x86_64-4.1.3
vim mongo_37017.conf
# 主节点配置
dbpath=/data/mongo/data/server1
bind_ip=0.0.0.0
port=37017
fork=true
logpath=/data/mongo/logs/server1.log
replSet=lagouCluster
节点2 mongo_37018.conf
cp mongo_37017.conf mongo_37018.conf
vim mongo_37018.conf
修改
dbpath=/data/mongo/data/server2
logpath=/data/mongo/logs/server2.log
port = 37018
节点3 confifig-17019.conf
cp mongo_37017.conf mongo_37019.conf
vim mongo_37019.conf
修改
dbpath=/data/mongo/data/server3
logpath=/data/mongo/logs/server3.log
port = 37019
创建日志目录
#-p 是多级创建
mkdir /data/mongo/data/server1 -p
mkdir /data/mongo/data/server2 -p
mkdir /data/mongo/data/server3 -p
mkdir /data/mongo/logs -p
2.2 启动配置节点
./bin/mongod -f mongo_37017.conf
./bin/mongod -f mongo_37018.conf
./bin/mongod -f mongo_37019.conf
2.3 初始化节点配置
# 进入mongo 节点
./bin/mongo --port 37017
# 配置节点集群,注意host 和端口号改成自己的服务器
var cfg ={"_id":"lagouCluster", "protocolVersion" : 1, "members":[ {"_id":1,"host":"192.168.31.100:37017"}, {"_id":2,"host":"192.168.31.100:37018"}, {"_id":3,"host":"192.168.31.100:37019"}] };
rs.initiate(cfg)
rs.status()
image-20210129112331393.png
2.4 查看集群状态:rs.status()
{
"set" : "lagouCluster",
"date" : ISODate("2021-01-29T03:21:53.946Z"),
"myState" : 1,
"term" : NumberLong(1),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1611890503, 1),
"t" : NumberLong(1)
},
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1611890503, 1),
"t" : NumberLong(1)
},
"appliedOpTime" : {
"ts" : Timestamp(1611890503, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1611890503, 1),
"t" : NumberLong(1)
}
},
"lastStableRecoveryTimestamp" : Timestamp(1611890502, 1),
"lastStableCheckpointTimestamp" : Timestamp(1611890502, 1),
"members" : [
{
"_id" : 1,
"name" : "192.168.31.100:37017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 548,
"optime" : {
"ts" : Timestamp(1611890503, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2021-01-29T03:21:43Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "could not find member to sync from",
"electionTime" : Timestamp(1611890500, 1),
"electionDate" : ISODate("2021-01-29T03:21:40Z"),
"configVersion" : 1,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 2,
"name" : "192.168.31.100:37018",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 23,
"optime" : {
"ts" : Timestamp(1611890503, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1611890503, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2021-01-29T03:21:43Z"),
"optimeDurableDate" : ISODate("2021-01-29T03:21:43Z"),
"lastHeartbeat" : ISODate("2021-01-29T03:21:52.746Z"),
"lastHeartbeatRecv" : ISODate("2021-01-29T03:21:53.243Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "192.168.31.100:37017",
"syncSourceHost" : "192.168.31.100:37017",
"syncSourceId" : 1,
"infoMessage" : "",
"configVersion" : 1
},
{
"_id" : 3,
"name" : "192.168.31.100:37019",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 23,
"optime" : {
"ts" : Timestamp(1611890503, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1611890503, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2021-01-29T03:21:43Z"),
"optimeDurableDate" : ISODate("2021-01-29T03:21:43Z"),
"lastHeartbeat" : ISODate("2021-01-29T03:21:52.746Z"),
"lastHeartbeatRecv" : ISODate("2021-01-29T03:21:53.243Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "192.168.31.100:37017",
"syncSourceHost" : "192.168.31.100:37017",
"syncSourceId" : 1,
"infoMessage" : "",
"configVersion" : 1
}
],
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1611890503, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1611890503, 1)
}
2.4 节点的动态增删
#删除slave 节点
rs.remove("192.168.31.100:37019")
#增加节点
rs.add("192.168.31.100:37019")
2.5 测试
主库测试(可以添加,查询)
use lg_resume
db.lg_resume.insert({name:'test',salary:1234});
db.lg_resume.find();
进入从库测试(无法查询,可以添加)
#新开启一个虚拟机窗口
./bin/mongo --port 37019
use lg_resume
db.lg_resume.find()
image-20210129112810934.png
使用rs.slaveOk解决
rs.slaveOk()
db.lg_resume.find()
image-20210129113107082.png
主节点宕机测试
rs.status() 查看主节点
rs.status()
image-20210129113728232.png
开启一个新虚拟机窗口
ps -ef |grep java
image-20210129113833563.png
停用主机
kill -9 10201
./bin/mongo --port 37019
image-20210129114018923.png
网友评论