此博客借鉴于:https://blog.csdn.net/zhangcongyi420/article/details/92738646
Replica Set 模式比较稳定,可靠,同时在一定的情况下能够实现自动容错的机制。
主备节点存储数据,仲裁节点不存储数据。客户端同时连接主节点与备节点,不连接仲裁节点。
默认设置下,主节点提供所有增删查改服务,备节点不提供任何服务。但是可以通过设置使备节点提供查询服务,这样就可以减少主节点的压力,当客户端进行数据查询时,请求自动转到备节点上。这个设置叫做Read Preference Modes,同时Java客户端提供了简单的配置方式,可以不必直接对数据库进行操作。
仲裁节点是一种特殊的节点,它本身并不存储数据,主要的作用是决定哪一个备节点在主节点挂掉之后提升为主节点,所以客户端不需要连接此节点。这里虽然只有一个备节点,但是仍然需要一个仲裁节点来提升备节点级别。
实验服务器ip及端口:
- 主节点:192.168.105.100 27917
- 从节点:192.168.105.101 27917
- 仲裁节点:192.168.105.101 27918
关闭防火墙:
- 查看状态 firewall-cmd --state
- 关闭firewalld systemctl stop firewalld.service
- 禁止开机启动 systemctl disable firewalld.service
关闭selinux
- 执行 gedit /etc/sysconfig/selinux
- vi /etc/sysconfig/selinux
- #SELINUX=enforcing 改为 SELINUX=disabled
- 执行 setenforce 0
安装目录:
- /data/mongodb 主从
- /data/mongodb-2 仲裁
mongodb下载地址:
- https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.0.19.tgz
解压:
tar -zxvf mongodb-linux-x86_64-rhel70-4.0.19.tgz
mv mongodb-linux-x86_64-rhel70-4.0.19 mongodb
mv mongodb-linux-x86_64-rhel70-4.0.19 mongodb-2
mv mongodb /data/
mv mongodb-2 /data/
创建目录
主节点:
cd /data/mongodb/
mkdir data
mkdir logs
mkdir conf
cd logs
touch master.log
cd ..
cd conf
touch mongodb.conf
从节点:
cd /data/mongodb/
mkdir data
mkdir logs
mkdir conf
cd logs
touch slave.log
cd ..
cd conf
touch mongodb.conf
仲裁节点:
cd /data/mongodb-2/
mkdir data
mkdir logs
mkdir conf
cd logs
touch arbite.log
cd ..
cd conf
touch mongodb.conf
编辑配置文件
主节点:
#cat /data/mongodb/conf/mongodb.conf
storage:
dbPath: "/data/mongodb/data"
engine: "wiredTiger"
wiredTiger:
engineConfig:
cacheSizeGB: 2
collectionConfig:
blockCompressor: snappy
systemLog:
destination: file
path: "/data/mongodb/logs/master.log"
logAppend: true
timeStampFormat: iso8601-utc
processManagement:
fork: true
net:
port: 27917
bindIp : 192.168.105.100
replication:
replSetName: test
从节点:
#cat /data/mongodb/conf/mongodb.conf
storage:
dbPath: "/data/mongodb/data"
engine: "wiredTiger"
wiredTiger:
engineConfig:
cacheSizeGB: 2
collectionConfig:
blockCompressor: snappy
systemLog:
destination: file
path: "/data/mongodb/logs/slave.log"
logAppend: true
timeStampFormat: iso8601-utc
processManagement:
fork: true
net:
port: 27917
bindIp : 192.168.105.101
replication:
replSetName: test
仲裁节点:
#cat /data/mongodb-2/conf/mongodb.conf
storage:
dbPath: "/data/mongodb-2/data"
engine: "wiredTiger"
wiredTiger:
engineConfig:
cacheSizeGB: 2
collectionConfig:
blockCompressor: snappy
systemLog:
destination: file
path: "/data/mongodb-2/logs/arbite.log"
logAppend: true
timeStampFormat: iso8601-utc
processManagement:
fork: true
net:
port: 27918
bindIp : 192.168.105.101
replication:
replSetName: test
启动
# /data/mongodb/bin/mongod -f /data/mongodb/conf/mongodb.conf
about to fork child process, waiting until server is ready for connections.
forked process: 32813
child process started successfully, parent exiting
# /data/mongodb-2/bin/mongod -f /data/mongodb-2/conf/mongodb.conf
about to fork child process, waiting until server is ready for connections.
forked process: 38667
child process started successfully, parent exiting
查看端口
ss -lnp |grep 27917
ss -lnp |grep 27918
配置集群集群(三个节点都执行一次):
#登陆
#/data/mongodb/bin/mongo 192.168.105.100:27917
#不用在意告警,不报错即可
#配置集群
> cfg={ _id:"test", members:[ {_id:0,host:'192.168.105.100:27917',priority:2}, {_id:1,host:'192.168.105.101:27917',priority:1}, {_id:2,host:'192.168.105.101:27918',arbiterOnly:true}] };
#执行成功输出如下
{
"_id" : "test",
"members" : [
{
"_id" : 0,
"host" : "192.168.105.100:27917",
"priority" : 2
},
{
"_id" : 1,
"host" : "192.168.105.101:27917",
"priority" : 1
},
{
"_id" : 2,
"host" : "192.168.105.101:27918",
"arbiterOnly" : true
}
]
}
初始化:
> rs.initiate(cfg);
#输出以下成功
{
"ok" : 1,
"operationTime" : Timestamp(1596783845, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1596783845, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
查看状态:
test:PRIMARY> rs.status()
{
"set" : "test",
"date" : ISODate("2020-08-07T07:30:13.365Z"),
"myState" : 1,
"term" : NumberLong(1),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1596785407, 1),
"t" : NumberLong(1)
},
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1596785407, 1),
"t" : NumberLong(1)
},
"appliedOpTime" : {
"ts" : Timestamp(1596785407, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1596785407, 1),
"t" : NumberLong(1)
}
},
"lastStableCheckpointTimestamp" : Timestamp(1596785357, 1),
"electionCandidateMetrics" : {
"lastElectionReason" : "electionTimeout",
"lastElectionDate" : ISODate("2020-08-07T07:04:16.975Z"),
"electionTerm" : NumberLong(1),
"lastCommittedOpTimeAtElection" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"lastSeenOpTimeAtElection" : {
"ts" : Timestamp(1596783845, 1),
"t" : NumberLong(-1)
},
"numVotesNeeded" : 2,
"priorityAtElection" : 2,
"electionTimeoutMillis" : NumberLong(10000),
"numCatchUpOps" : NumberLong(0),
"newTermStartDate" : ISODate("2020-08-07T07:04:16.980Z"),
"wMajorityWriteAvailabilityDate" : ISODate("2020-08-07T07:04:17.664Z")
},
"members" : [
{
"_id" : 0,
"name" : "192.168.105.100:27917",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 2098,
"optime" : {
"ts" : Timestamp(1596785407, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2020-08-07T07:30:07Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"electionTime" : Timestamp(1596783856, 1),
"electionDate" : ISODate("2020-08-07T07:04:16Z"),
"configVersion" : 1,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 1,
"name" : "192.168.105.101:27917",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 1567,
"optime" : {
"ts" : Timestamp(1596785407, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1596785407, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2020-08-07T07:30:07Z"),
"optimeDurableDate" : ISODate("2020-08-07T07:30:07Z"),
"lastHeartbeat" : ISODate("2020-08-07T07:30:12.217Z"),
"lastHeartbeatRecv" : ISODate("2020-08-07T07:30:12.818Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "192.168.105.100:27917",
"syncSourceHost" : "192.168.105.100:27917",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 1
},
{
"_id" : 2,
"name" : "192.168.105.101:27918",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 1567,
"lastHeartbeat" : ISODate("2020-08-07T07:30:12.217Z"),
"lastHeartbeatRecv" : ISODate("2020-08-07T07:30:12.719Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"configVersion" : 1
}
],
"ok" : 1,
"operationTime" : Timestamp(1596785407, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1596785407, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
测试数据同步
主节点插入数据
# 查看所有库
test:PRIMARY> show dbs
admin 0.000GB
config 0.000GB
flower 0.000GB
local 0.000GB
test 0.000GB
# 创建库
test:PRIMARY> use aaa
switched to db aaa
# 再次查看库发现并没有刚刚创建的库,因为库中没有数据所以不会显示在当前列表
test:PRIMARY> show dbs
admin 0.000GB
config 0.000GB
flower 0.000GB
local 0.000GB
test 0.000GB
# 使用db可以查看到库
test:PRIMARY> db
aaa
# 插入一条数据
test:PRIMARY> db.aaa.insert({"name":"aaa"})
WriteResult({ "nInserted" : 1 })
# 再次查看库已经可以显示了
test:PRIMARY> show dbs
aaa 0.000GB
admin 0.000GB
config 0.000GB
flower 0.000GB
local 0.000GB
test 0.000GB
从节点查看
会发现报错:not master and slaveOk=false
这是正常的,因为从节点默认不允许读写,可以解决
test:SECONDARY> show dbs
2020-08-07T15:36:09.265+0800 E QUERY [js] Error: listDatabases failed:{
"operationTime" : Timestamp(1596785767, 1),
"ok" : 0,
"errmsg" : "not master and slaveOk=false",
"code" : 13435,
"codeName" : "NotMasterNoSlaveOk",
"$clusterTime" : {
"clusterTime" : Timestamp(1596785767, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
Mongo.prototype.getDBs@src/mongo/shell/mongo.js:139:1
shellHelper.show@src/mongo/shell/utils.js:882:13
shellHelper@src/mongo/shell/utils.js:766:15
@(shellhelp2):1:1
执行rs.slaveOk();解决
数据同步成功
test:SECONDARY> rs.slaveOk();
test:SECONDARY> show dbs
aaa 0.000GB
admin 0.000GB
config 0.000GB
flower 0.000GB
local 0.000GB
test 0.000GB
停止mongodb
使用kill -9 会导致正在写入的数据终端,造成文件混乱,甚至崩溃,所以使用下面两种停止操作
第一种:使用 kill -15,killall mongod 命令关闭MongoDb服务
第二种:登录到MongoDb控制台,use admin,执行db.shutdownServer()
使用普通用户运行mongodb
注意一定不能使用 kill -9 停止,否则普通用户无法启动
创建普通用户 mongodb
修改属主属组 chown -R mongodb:mongodb /data/mongodb/
su - mongodb
启动即可
集群搭建完成。。。