官方在4.0版本后不建议再使用主从架构,mongodb4.0后废弃主从复制
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.6.23.tgz
ip地址 | 说明 |
---|---|
192.168.122.246 | master |
192.168.122.245 | slave |
master和副本集不能同时存在
1.master
[root@master 27017]# cat mongod.conf
port=28017
dbpath=/data/mongodb_data/27017/data
logpath=/data/mongodb_data/27017/logs/mongod.log
pidfilepath=/data/mongodb_data/27017/mongod.pid
fork=true
logappend=true
logRotate=reopen
journal=true
storageEngine=wiredTiger
bind_ip = 0.0.0.0
master = true
source = 192.168.122.245:28017
#auth = true
[root@master 27017]# cat mongod.conf
port=28017
dbpath=/data/mongodb_data/27017/data
logpath=/data/mongodb_data/27017/logs/mongod.log
pidfilepath=/data/mongodb_data/27017/mongod.pid
fork=true
logappend=true
logRotate=reopen
journal=true
storageEngine=wiredTiger
bind_ip = 0.0.0.0
master = true
source = 192.168.122.245:28017
#auth = true
- 启动
/usr/local/mongo/bin/mongod --config /data/mongodb_data/27017/mongod.conf
/usr/local/mongo/bin/mongod --config /data/mongodb_data/27017/mongod.conf
2.slave
- 配置
[root@slave 27017]# cat mongod.conf
port=28017
dbpath=/data/mongodb_data/27017/data
logpath=/data/mongodb_data/27017/logs/mongod.log
pidfilepath=/data/mongodb_data/27017/mongod.pid
fork=true
logappend=true
logRotate=reopen
journal=true
storageEngine=wiredTiger
bind_ip = 0.0.0.0
slave = true
source = 192.168.122.246:28017
#auth = true
[root@slave 27017]# cat mongod.conf
port=28017
dbpath=/data/mongodb_data/27017/data
logpath=/data/mongodb_data/27017/logs/mongod.log
pidfilepath=/data/mongodb_data/27017/mongod.pid
fork=true
logappend=true
logRotate=reopen
journal=true
storageEngine=wiredTiger
bind_ip = 0.0.0.0
slave = true
source = 192.168.122.246:28017
#auth = true
- 启动
/usr/local/mongo/bin/mongod --config /data/mongodb_data/27017/mongod.conf
/usr/local/mongo/bin/mongod --config /data/mongodb_data/27017/mongod.conf
测试
在master上面执行
/usr/local/mongo/bin/mongo 127.0.0.1:28017
创建新库 插入数据
> use new10
switched to db new10
> db.new10.save({"name":22200})
WriteResult({ "nInserted" : 1 })
> db.new10.find()
/usr/local/mongo/bin/mongo 127.0.0.1:28017
创建新库 插入数据
> use new10
switched to db new10
> db.new10.save({"name":22200})
WriteResult({ "nInserted" : 1 })
> db.new10.find()
在slave上查看
/usr/local/mongo/bin/mongo 127.0.0.1:28017
>rs.slaveOk()
> show dbs;
admin 0.000GB
config 0.000GB
local 0.000GB
test 0.000GB
> use test;
switched to db test
> show tables;
new10
> db.new10.find()
{ "_id" : ObjectId("61c9275b5745d52b72d861c5"), "name" : 22200 }
/usr/local/mongo/bin/mongo 127.0.0.1:28017
>rs.slaveOk()
> show dbs;
admin 0.000GB
config 0.000GB
local 0.000GB
test 0.000GB
> use test;
switched to db test
> show tables;
new10
> db.new10.find()
{ "_id" : ObjectId("61c9275b5745d52b72d861c5"), "name" : 22200 }
3.cmd
主从相关命令:
判断master
db.isMaster():主从都可执行,用这个命令判断是不是master
> db.isMaster()
{
"ismaster" : true,
"maxBsonObjectSize" : 16777216,
"maxMessageSizeBytes" : 48000000,
"maxWriteBatchSize" : 100000,
"localTime" : ISODate("2021-12-27T03:03:09.118Z"),
"logicalSessionTimeoutMinutes" : 30,
"minWireVersion" : 0,
"maxWireVersion" : 6,
"readOnly" : false,
"ok" : 1
}
> db.isMaster()
{
"ismaster" : true,
"maxBsonObjectSize" : 16777216,
"maxMessageSizeBytes" : 48000000,
"maxWriteBatchSize" : 100000,
"localTime" : ISODate("2021-12-27T03:03:09.118Z"),
"logicalSessionTimeoutMinutes" : 30,
"minWireVersion" : 0,
"maxWireVersion" : 6,
"readOnly" : false,
"ok" : 1
}
获取主从信息master
db.getReplicationInfo():主上执行,获取主从信息
#master
> db.getReplicationInfo()
{
"logSizeMB" : 1292.5505857467651,
"usedMB" : 0.02,
"timeDiff" : 1634,
"timeDiffHours" : 0.45,
"tFirst" : "Sun Dec 26 2021 21:35:17 GMT-0500 (EST)",
"tLast" : "Sun Dec 26 2021 22:02:31 GMT-0500 (EST)",
"now" : "Sun Dec 26 2021 22:02:36 GMT-0500 (EST)"
}
#master
> db.getReplicationInfo()
{
"logSizeMB" : 1292.5505857467651,
"usedMB" : 0.02,
"timeDiff" : 1634,
"timeDiffHours" : 0.45,
"tFirst" : "Sun Dec 26 2021 21:35:17 GMT-0500 (EST)",
"tLast" : "Sun Dec 26 2021 22:02:31 GMT-0500 (EST)",
"now" : "Sun Dec 26 2021 22:02:36 GMT-0500 (EST)"
}
获取主从信息master/slave
db.printReplicationInfo():主从都可执行,获取主从信息
查看主从延时
db.printSlaveReplicationInfo();:从库执行,查看主从延时
> db.printSlaveReplicationInfo();
WARNING: printSlaveReplicationInfo is deprecated and may be removed in the next major release. Please use printSecondaryReplicationInfo instead.
source: 192.168.122.246:28017
syncedTo: Sun Dec 26 2021 22:04:01 GMT-0500 (EST)
2 secs (0 hrs) behind the freshest member (no primary available at the moment)
> db.printSlaveReplicationInfo();
WARNING: printSlaveReplicationInfo is deprecated and may be removed in the next major release. Please use printSecondaryReplicationInfo instead.
source: 192.168.122.246:28017
syncedTo: Sun Dec 26 2021 22:04:01 GMT-0500 (EST)
2 secs (0 hrs) behind the freshest member (no primary available at the moment)
同步原理:
同步就是master上把对数据的更改操作记录到oplog中,然后slave抓取master的oplog执行。从这点看Oplog的功能和mysql的mysql-bin.的功能类似。Mysql-bin以二进制日志的形式存在,但是oplog是以一个mongodb的表的形式存在,该表在local库表名为oplog.$main,该表为循环写入形,所以不用定时清理