荐 配置mongoDB复制集群集
程序员文章站
2022-06-24 23:09:38
配置mongoDB复制集群集配置环境27017主节点27018 标准节点27019被动节点27020 仲裁节点一、配置复制集1)在配置复制集的时候需要先将mongoDB原本的数据清空:进入mongoDB数据存储文件中删除数据:(注意所有的实例都需要清空)[root@kgc6 ~]# cd /var/lib/mongo[root@kgc6 mongo]# rm -rf ./*2)创建多个实例复制27017配置文件并改名:(需要几个实例就复制几个实例)[root@kgc6 ~]# cp...
配置mongoDB复制集群集
配置环境
27017主节点
27018 标准节点
27019被动节点
27020 仲裁节点
一、配置复制集
1)在配置复制集的时候需要先将mongoDB原本的数据清空:
进入mongoDB数据存储文件中删除数据:(注意所有的实例都需要清空)
[root@kgc6 ~]# cd /var/lib/mongo
[root@kgc6 mongo]# rm -rf ./*
2)创建多个实例复制27017配置文件并改名:(需要几个实例就复制几个实例)
[root@kgc6 ~]# cp /etc/mongod.conf /etc/mongod1.conf //(所有的实例配置文件都要改)
修改配置文件中的内容:
path: /var/log/mongodb/mongod1.log (改为你对应的复制的配置文件名称)
dbPath: /var/lib/mongo1
pidFilePath: /var/run/mongodb/mongod1.pid
port: 27018 //修改端口号
replication:
oplogSizeMB: 2048(开启复制集,前面要格两个空格)
replSetName: kgcrs (复制集的名称)
3)创建数据文件日志文件可以自动生成:(所有的实例都需要创建)
[root@kgc6 ~]# mkdir -p /var/lib/mongo1
4)启动mongoDB服务:
[root@kgc6 lib]# cd /usr/bin/
[root@kgc6 bin]# mongod -f /etc/mongod.conf (所有的实例都需要开)
查看端口号:
[root@kgc6 bin]# netstat -lnt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:27018 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:27019 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:27020 0.0.0.0:* LISTEN
5)初始化配置复制集:
[root@kgc6 bin]# mongo //登录默认的mongodb数据库
> show dbs //查看全部表
2020-07-08T17:57:23.652+0800 E QUERY [thread1] Error: listDatabases failed:{
"ok" : 0,
"errmsg" : "not master and slaveOk=false",
"code" : 13435,
"codeName" : "NotMasterNoSlaveOk"
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
Mongo.prototype.getDBs@src/mongo/shell/mongo.js:62:1
shellHelper.show@src/mongo/shell/utils.js:814:19
shellHelper@src/mongo/shell/utils.js:704:15
@(shellhelp2):1:1 //(这里报错是正常情况因为我们还没有配置复制集)
> rs.status() //(查看复制集)
{
"info" : "run rs.initiate(...) if not yet done for the set",
"ok" : 0,
"errmsg" : "no replset config has been received",
"code" : 94,
"codeName" : "NotYetInitialized"
>config={"_id":"kgcrs","members":[{"_id":0,"host":"127.0.0.1:27017","priority":100},{"_id":1,"host":"127.0.0.1:27018","priority":100},{"_id":2,"host":"127.0.0.1:27019","priority":0},{"_id":3,"host":"127.0.0.1:27020","arbiterOnly":true}]} //初始化数据库并配置优先级
{
"_id" : "kgcrs",
"members" : [
{
"_id" : 0,
"host" : "127.0.0.1:27017",
"priority" : 100
},
{
"_id" : 1,
"host" : "127.0.0.1:27018",
"priority" : 100
},
{
"_id" : 2,
"host" : "127.0.0.1:27019",
"priority" : 0
},
{
"_id" : 3,
"host" : "127.0.0.1:27020",
"arbiterOnly" : true
}
]
}
> rs.initiate(config) //初始化数据库
{ "ok" : 1 }
查看复制集状态:
> rs.status()
{
"info" : "run rs.initiate(...) if not yet done for the set",
"ok" : 0,
"errmsg" : "no replset config has been received",
"code" : 94,
"codeName" : "NotYetInitialized"
}
> config={"_id":"kgcrs","members":[{"_id":0,"host":"127.0.0.1:27017","priority":100},{"_id":1,"host":"127.0.0.1:27018","priority":100},{"_id":2,"host":"127.0.0.1:27019","priority":0},{"_id":3,"host":"127.0.0.1:27020","arbiterOnly":true}]}
{
"_id" : "kgcrs",
"members" : [
{
"_id" : 0,
"host" : "127.0.0.1:27017",
"priority" : 100
},
{
"_id" : 1,
"host" : "127.0.0.1:27018",
"priority" : 100
},
{
"_id" : 2,
"host" : "127.0.0.1:27019",
"priority" : 0
},
{
"_id" : 3,
"host" : "127.0.0.1:27020",
"arbiterOnly" : true
}
]
}
> rs.initiate(config)
{ "ok" : 1 }
kgcrs:SECONDARY> rs.status()
{
"set" : "kgcrs",
"date" : ISODate("2020-07-08T02:23:13.981Z"),
"myState" : 1,
"term" : NumberLong(1),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1594174989, 1),
"t" : NumberLong(1)
},
"appliedOpTime" : {
"ts" : Timestamp(1594174989, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1594174989, 1),
"t" : NumberLong(1)
}
},
"members" : [
{
"_id" : 0,
"name" : "127.0.0.1:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY", //主节点
"uptime" : 148,
"optime" : {
"ts" : Timestamp(1594174989, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2020-07-08T02:23:09Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "could not find member to sync from",
"electionTime" : Timestamp(1594174918, 1),
"electionDate" : ISODate("2020-07-08T02:21:58Z"),
"configVersion" : 1,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 1,
"name" : "127.0.0.1:27018",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY", //被节点
"uptime" : 86,
"optime" : {
"ts" : Timestamp(1594174989, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1594174989, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2020-07-08T02:23:09Z"),
"optimeDurableDate" : ISODate("2020-07-08T02:23:09Z"),
"lastHeartbeat" : ISODate("2020-07-08T02:23:12.583Z"),
"lastHeartbeatRecv" : ISODate("2020-07-08T02:23:13.770Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "127.0.0.1:27017",
"syncSourceHost" : "127.0.0.1:27017",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 1
},
{
"_id" : 2,
"name" : "127.0.0.1:27019",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY", //被节点
"uptime" : 86,
"optime" : {
"ts" : Timestamp(1594174989, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1594174989, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2020-07-08T02:23:09Z"),
"optimeDurableDate" : ISODate("2020-07-08T02:23:09Z"),
"lastHeartbeat" : ISODate("2020-07-08T02:23:12.583Z"),
"lastHeartbeatRecv" : ISODate("2020-07-08T02:23:13.832Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "127.0.0.1:27017",
"syncSourceHost" : "127.0.0.1:27017",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 1
},
{
"_id" : 3,
"name" : "127.0.0.1:27020",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER", //仲裁节点
"uptime" : 86,
"lastHeartbeat" : ISODate("2020-07-08T02:23:12.583Z"),
"lastHeartbeatRecv" : ISODate("2020-07-08T02:23:09.455Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"configVersion" : 1
}
],
"ok" : 1
}
kgcrs:PRIMARY>
6)增加和删除节点:
kgcrs:PRIMARY> rs.add("127.0.0.1:27021")
查看复制集节点:
kgcrs:PRIMARY> rs.status()
{
"_id" : 4,
"name" : "127.0.0.1:27021",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 10,
"optime" : {
"ts" : Timestamp(1594175705, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1594175705, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2020-07-08T02:35:05Z"),
"optimeDurableDate" : ISODate("2020-07-08T02:35:05Z"),
"lastHeartbeat" : ISODate("2020-07-08T02:35:15.243Z"),
"lastHeartbeatRecv" : ISODate("2020-07-08T02:35:15.304Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"configVersion" : 2
}
],
"ok" : 1
}
删除复制集节点:
kgcrs:PRIMARY> rs.remove("127.0.0.1:27021")
{ "ok" : 1 }
查看复制集节点:
{
"_id" : 3,
"name" : "127.0.0.1:27020",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 992,
"lastHeartbeat" : ISODate("2020-07-08T02:38:18.900Z"),
"lastHeartbeatRecv" : ISODate("2020-07-08T02:38:19.919Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"configVersion" : 3
}
],
"ok" : 1
}
7)模拟故障自动转移:
之前的主节点是127.0.0.1:27017
关闭主节点:
[root@kgc6 bin]# mongod -f /etc/mongod.conf --shoutdown
登录被节点查看:(可以看到主节点自动切换到优先级为100的27018被节点上)
[root@kgc6 bin]# mongo --port 27018
kgcrs:PRIMARY> rs.status()
{
"set" : "kgcrs",
"date" : ISODate("2020-07-08T02:42:49.356Z"),
"myState" : 1,
"term" : NumberLong(2),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1594176089, 1),
"t" : NumberLong(1)
},
"appliedOpTime" : {
"ts" : Timestamp(1594176168, 1),
"t" : NumberLong(2)
},
"durableOpTime" : {
"ts" : Timestamp(1594176168, 1),
"t" : NumberLong(2)
}
},
"members" : [
{
"_id" : 0,
"name" : "127.0.0.1:27017",
"health" : 0,
"state" : 8,
"stateStr" : "(not reachable/healthy)",
"uptime" : 0,
"optime" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"optimeDurable" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"optimeDate" : ISODate("1970-01-01T00:00:00Z"),
"optimeDurableDate" : ISODate("1970-01-01T00:00:00Z"),
"lastHeartbeat" : ISODate("2020-07-08T02:42:48.899Z"),
"lastHeartbeatRecv" : ISODate("2020-07-08T02:41:37.412Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "Connection refused",
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"configVersion" : -1
},
{
"_id" : 1,
"name" : "127.0.0.1:27018",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 1321,
"optime" : {
"ts" : Timestamp(1594176168, 1),
"t" : NumberLong(2)
},
"optimeDate" : ISODate("2020-07-08T02:42:48Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "could not find member to sync from",
"electionTime" : Timestamp(1594176106, 1),
"electionDate" : ISODate("2020-07-08T02:41:46Z"),
"configVersion" : 3,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 2,
"name" : "127.0.0.1:27019",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 1259,
"optime" : {
"ts" : Timestamp(1594176168, 1),
"t" : NumberLong(2)
},
"optimeDurable" : {
"ts" : Timestamp(1594176168, 1),
"t" : NumberLong(2)
},
"optimeDate" : ISODate("2020-07-08T02:42:48Z"),
"optimeDurableDate" : ISODate("2020-07-08T02:42:48Z"),
"lastHeartbeat" : ISODate("2020-07-08T02:42:48.898Z"),
"lastHeartbeatRecv" : ISODate("2020-07-08T02:42:48.146Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "127.0.0.1:27018",
"syncSourceHost" : "127.0.0.1:27018",
"syncSourceId" : 1,
"infoMessage" : "",
"configVersion" : 3
},
{
"_id" : 3,
"name" : "127.0.0.1:27020",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 1259,
"lastHeartbeat" : ISODate("2020-07-08T02:42:48.898Z"),
"lastHeartbeatRecv" : ISODate("2020-07-08T02:42:45.207Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"configVersion" : 3
}
],
"ok" : 1
}
8)手动进行主从切换:
启动27017端口:
[root@kgc6 bin]# mongod -f /etc/mongod.conf
在27018主节点上配置:
kgcrs:PRIMARY> rs.stepDown(60,30) //交出主节点位置,维持被节点不少于60秒,同时等待30秒以使主节点和被节点日志同步:
kgcrs:SECONDARY> //可以看到27018节点已经变成了从节点
查看复制节点状态:(可以看到27017节点从新变成主节点)
kgcrs:SECONDARY> rs.status()
{
"set" : "kgcrs",
"date" : ISODate("2020-07-08T02:53:42.484Z"),
"myState" : 2,
"term" : NumberLong(3),
"syncingTo" : "127.0.0.1:27019",
"syncSourceHost" : "127.0.0.1:27019",
"syncSourceId" : 2,
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1594176816, 1),
"t" : NumberLong(3)
},
"appliedOpTime" : {
"ts" : Timestamp(1594176816, 1),
"t" : NumberLong(3)
},
"durableOpTime" : {
"ts" : Timestamp(1594176816, 1),
"t" : NumberLong(3)
}
},
"members" : [
{
"_id" : 0,
"name" : "127.0.0.1:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 410,
"optime" : {
"ts" : Timestamp(1594176816, 1),
"t" : NumberLong(3)
},
"optimeDurable" : {
"ts" : Timestamp(1594176816, 1),
"t" : NumberLong(3)
},
"optimeDate" : ISODate("2020-07-08T02:53:36Z"),
"optimeDurableDate" : ISODate("2020-07-08T02:53:36Z"),
"lastHeartbeat" : ISODate("2020-07-08T02:53:41.078Z"),
"lastHeartbeatRecv" : ISODate("2020-07-08T02:53:41.648Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"electionTime" : Timestamp(1594176614, 1),
"electionDate" : ISODate("2020-07-08T02:50:14Z"),
"configVersion" : 3
},
{
"_id" : 1,
"name" : "127.0.0.1:27018",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 1974,
"optime" : {
"ts" : Timestamp(1594176816, 1),
"t" : NumberLong(3)
},
"optimeDate" : ISODate("2020-07-08T02:53:36Z"),
"syncingTo" : "127.0.0.1:27019",
"syncSourceHost" : "127.0.0.1:27019",
"syncSourceId" : 2,
"infoMessage" : "",
"configVersion" : 3,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 2,
"name" : "127.0.0.1:27019",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 1913,
"optime" : {
"ts" : Timestamp(1594176816, 1),
"t" : NumberLong(3)
},
"optimeDurable" : {
"ts" : Timestamp(1594176816, 1),
"t" : NumberLong(3)
},
"optimeDate" : ISODate("2020-07-08T02:53:36Z"),
"optimeDurableDate" : ISODate("2020-07-08T02:53:36Z"),
"lastHeartbeat" : ISODate("2020-07-08T02:53:41.117Z"),
"lastHeartbeatRecv" : ISODate("2020-07-08T02:53:42.117Z"),
"pingMs" : NumberLong(3),
"lastHeartbeatMessage" : "",
"syncingTo" : "127.0.0.1:27017",
"syncSourceHost" : "127.0.0.1:27017",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 3
},
{
"_id" : 3,
"name" : "127.0.0.1:27020",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 1913,
"lastHeartbeat" : ISODate("2020-07-08T02:53:41.078Z"),
"lastHeartbeatRecv" : ISODate("2020-07-08T02:53:40.945Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"configVersion" : 3
}
],
"ok" : 1
}
选举的原理:
节点类型分为标准(host)节点、被动(passive)节点和仲裁(arbiter)节点:
(1)只有标准节点可能被选举为活跃节点(主节点)有选举权,被动节点有完整副本,不可能成为活跃节点,有选举权,仲裁节点不复制数据,不可能成为活跃节点,只有选举权。
(2)标准节点与被动节点的区别:priority值高者是标准节点,低者是被动节点
(3)选举规则是票数高者获胜,priority是优先权为0-1000的值,相当于额外增加0-1000的票数,选举结果:票数高者获胜,诺票数相同,数据新者获胜。
二、配置带认证的复制集:
创建密钥文件:
[root@kgc6 lib]# vim /var/lib/testKeyFile.file
生成密钥文件:
openssl rand -base64 756 > /var/lib/testKeyFile.file
修改权限:
[root@kgc6 lib]# chmod 400 /var/lib/testKeyFile.file
为个个实例生成密钥文件:
[root@kgc6 lib]# cp /var/lib/testKeyFile.file /var/lib/testKeyFile1.file
[root@kgc6 lib]# cp /var/lib/testKeyFile.file /var/lib/testKeyFile2.file
[root@kgc6 lib]# cp /var/lib/testKeyFile.file /var/lib/testKeyFile3.file
登录主节点添加权限用户:
[root@kgc6 bin]# mongo
添加root用户到admin组中成为超级管理员用户密码为:123
kgcrs:PRIMARY> db.createUser({user:"root",pwd:"123",roles:["root"]})
关闭各实例服务:
[root@kgc6 bin]# mongod -f /etc/mongod.conf --shutdown
killing process with pid: 4130
[root@kgc6 bin]# mongod -f /etc/mongod1.conf --shutdown
killing process with pid: 3624
[root@kgc6 bin]# mongod -f /etc/mongod2.conf --shutdown
killing process with pid: 3651
[root@kgc6 bin]# mongod -f /etc/mongod3.conf --shutdown
killing process with pid: 3678
修改各实例的配置文件加上用户认证和密钥文件位置:
[root@kgc6 bin]# vim /etc/mongod.conf 添加以下选项:
security:
keyFile: /var/lib/testKeyFile.file //这里是各个实例对应的密钥文件位置
authorization: enabled
[root@kgc6 bin]# vim /etc/mongod1.conf
security:
keyFile: /var/lib/testKeyFile1.file
authorization: enabled
启动各实例服务:
[root@kgc6 bin]# mongod -f /etc/mongod.conf
about to fork child process, waiting until server is ready for connections.
forked process: 4401
child process started successfully, parent exiting
[root@kgc6 bin]# mongod -f /etc/mongod1.conf
about to fork child process, waiting until server is ready for connections.
forked process: 4475
child process started successfully, parent exiting
[root@kgc6 bin]# mongod -f /etc/mongod2.conf
about to fork child process, waiting until server is ready for connections.
forked process: 4552
child process started successfully, parent exiting
[root@kgc6 bin]# mongod -f /etc/mongod3.conf
about to fork child process, waiting until server is ready for connections.
forked process: 4632
child process started successfully, parent exiting
验证群集:登录主节点服务器:
[root@kgc6 bin]# mongo
MongoDB shell version v3.4.24
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.24
kgcrs:PRIMARY> show dbs(查看所有数据库)
2020-07-08T11:26:46.700+0800 E QUERY [thread1] Error: listDatabases failed:{
"ok" : 0,
"errmsg" : "not authorized on admin to execute command { listDatabases: 1.0 }",
"code" : 13,
"codeName" : "Unauthorized"
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
Mongo.prototype.getDBs@src/mongo/shell/mongo.js:62:1
shellHelper.show@src/mongo/shell/utils.js:814:19
shellHelper@src/mongo/shell/utils.js:704:15
@(shellhelp2):1:1
kgcrs:PRIMARY> rs.status() (查看复制集)
{
"ok" : 0,
"errmsg" : "not authorized on admin to execute command { replSetGetStatus: 1.0 }",
"code" : 13,
"codeName" : "Unauthorized"
}
进入到admin库使用root用户登录:
kgcrs:PRIMARY> use admin
switched to db admin
kgcrs:PRIMARY> db.auth("root","123")
1
kgcrs:PRIMARY> show dbs (再次查看所有数据库)
admin 0.000GB
local 0.000GB
kgcrs:PRIMARY> rs.status() (查看复制集)
{
"set" : "kgcrs",
"date" : ISODate("2020-07-08T03:31:03.906Z"),
"myState" : 1,
"term" : NumberLong(4),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1594179053, 1),
"t" : NumberLong(4)
},
"appliedOpTime" : {
"ts" : Timestamp(1594179053, 1),
"t" : NumberLong(4)
},
"durableOpTime" : {
"ts" : Timestamp(1594179053, 1),
"t" : NumberLong(4)
}
},
"members" : [
{
"_id" : 0,
"name" : "127.0.0.1:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 322,
"optime" : {
"ts" : Timestamp(1594179053, 1),
"t" : NumberLong(4)
},
"optimeDate" : ISODate("2020-07-08T03:30:53Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"electionTime" : Timestamp(1594178752, 1),
"electionDate" : ISODate("2020-07-08T03:25:52Z"),
"configVersion" : 3,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 1,
"name" : "127.0.0.1:27018",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 317,
"optime" : {
"ts" : Timestamp(1594179053, 1),
"t" : NumberLong(4)
},
"optimeDurable" : {
"ts" : Timestamp(1594179053, 1),
"t" : NumberLong(4)
},
"optimeDate" : ISODate("2020-07-08T03:30:53Z"),
"optimeDurableDate" : ISODate("2020-07-08T03:30:53Z"),
"lastHeartbeat" : ISODate("2020-07-08T03:31:03.653Z"),
"lastHeartbeatRecv" : ISODate("2020-07-08T03:31:03.840Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "127.0.0.1:27017",
"syncSourceHost" : "127.0.0.1:27017",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 3
},
{
"_id" : 2,
"name" : "127.0.0.1:27019",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 312,
"optime" : {
"ts" : Timestamp(1594179053, 1),
"t" : NumberLong(4)
},
"optimeDurable" : {
"ts" : Timestamp(1594179053, 1),
"t" : NumberLong(4)
},
"optimeDate" : ISODate("2020-07-08T03:30:53Z"),
"optimeDurableDate" : ISODate("2020-07-08T03:30:53Z"),
"lastHeartbeat" : ISODate("2020-07-08T03:31:03.653Z"),
"lastHeartbeatRecv" : ISODate("2020-07-08T03:31:02.668Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "127.0.0.1:27018",
"syncSourceHost" : "127.0.0.1:27018",
"syncSourceId" : 1,
"infoMessage" : "",
"configVersion" : 3
},
{
"_id" : 3,
"name" : "127.0.0.1:27020",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 307,
"lastHeartbeat" : ISODate("2020-07-08T03:31:03.653Z"),
"lastHeartbeatRecv" : ISODate("2020-07-08T03:31:00.092Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"configVersion" : 3
}
],
"ok" : 1
}
登录从节点服务器:
[root@kgc6 bin]# mongo --port 27018
kgcrs:SECONDARY> use admin
kgcrs:SECONDARY> db.auth("root","123")
1
默认MongoDB复制集的从节点不能读取数据,可以使用rs.slaveOk()命令允许能够在从节点读取数据:
kgcrs:SECONDARY> rs.slaveOk()
kgcrs:SECONDARY> show dbs
admin 0.000GB
local 0.000GB
kgcrs:SECONDARY> rs.status()
{
"set" : "kgcrs",
"date" : ISODate("2020-07-08T03:44:43.345Z"),
"myState" : 2,
"term" : NumberLong(4),
"syncingTo" : "127.0.0.1:27019",
"syncSourceHost" : "127.0.0.1:27019",
"syncSourceId" : 2,
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1594179874, 1),
"t" : NumberLong(4)
},
"appliedOpTime" : {
"ts" : Timestamp(1594179874, 1),
"t" : NumberLong(4)
},
"durableOpTime" : {
"ts" : Timestamp(1594179874, 1),
"t" : NumberLong(4)
}
},
"members" : [
{
"_id" : 0,
"name" : "127.0.0.1:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 207,
"optime" : {
"ts" : Timestamp(1594179874, 1),
"t" : NumberLong(4)
},
"optimeDurable" : {
"ts" : Timestamp(1594179874, 1),
"t" : NumberLong(4)
},
"optimeDate" : ISODate("2020-07-08T03:44:34Z"),
"optimeDurableDate" : ISODate("2020-07-08T03:44:34Z"),
"lastHeartbeat" : ISODate("2020-07-08T03:44:43.037Z"),
"lastHeartbeatRecv" : ISODate("2020-07-08T03:44:42.458Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"electionTime" : Timestamp(1594178752, 1),
"electionDate" : ISODate("2020-07-08T03:25:52Z"),
"configVersion" : 3
},
{
"_id" : 1,
"name" : "127.0.0.1:27018",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 208,
"optime" : {
"ts" : Timestamp(1594179874, 1),
"t" : NumberLong(4)
},
"optimeDate" : ISODate("2020-07-08T03:44:34Z"),
"syncingTo" : "127.0.0.1:27019",
"syncSourceHost" : "127.0.0.1:27019",
"syncSourceId" : 2,
"infoMessage" : "",
"configVersion" : 3,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 2,
"name" : "127.0.0.1:27019",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 207,
"optime" : {
"ts" : Timestamp(1594179874, 1),
"t" : NumberLong(4)
},
"optimeDurable" : {
"ts" : Timestamp(1594179874, 1),
"t" : NumberLong(4)
},
"optimeDate" : ISODate("2020-07-08T03:44:34Z"),
"optimeDurableDate" : ISODate("2020-07-08T03:44:34Z"),
"lastHeartbeat" : ISODate("2020-07-08T03:44:43.037Z"),
"lastHeartbeatRecv" : ISODate("2020-07-08T03:44:42.381Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "127.0.0.1:27017",
"syncSourceHost" : "127.0.0.1:27017",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 3
},
{
"_id" : 3,
"name" : "127.0.0.1:27020",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 207,
"lastHeartbeat" : ISODate("2020-07-08T03:44:43.037Z"),
"lastHeartbeatRecv" : ISODate("2020-07-08T03:44:41.023Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"configVersion" : 3
}
],
"ok" : 1
}
本文地址:https://blog.****.net/weixin_45947267/article/details/107314716
推荐阅读
-
MongoDB安装、主从、复制集 博客分类: MongoDB MongoDB安装主从复制集
-
mongodb副本集的搭建 博客分类: mongodb学习 mongodb复制集mongodb复制集
-
搭建MongoDB主从复制(Master-Slave)集
-
MongoDB从入门到提高【第一集】---------MongdoDB配置参数说明及示例
-
【MongoDB】windows平台搭建Mongo数据库复制集(类似集群)
-
MongoDB运行时添加分片复制集
-
MongoDB复制集(ReplicationSets)简介
-
MongoDB复制集迁移到TokuMX方案
-
MySQL Cluster 与 MongoDB 复制群集分片设计及原理
-
配置mongodb复制集时遇见的坑