Mongo--03 mongo副本集、备份与恢复

时间:2022-06-09 00:24:19

一、mongo副本集配置

1.创建节点目录和数据目录

#先进到普通用户
su - mongo
#关机
[mongo@db01 ~]$ mongod -f /opt/mongo_27017/conf/mongodb.conf --shutdown
killing process with pid: 17656
[mongo@db01 ~]$ mkdir -p /opt/mongo_2801{7,8,9}/{conf,log,pid}
[mongo@db01 ~]$ mkdir -p /data/mongo_2801{7,8,9}

2.创建配置文件

cat >/opt/mongo_28017/conf/mongo_28017.conf <<EOF
systemLog:
destination: file
logAppend: true
path: /opt/mongo_28017/log/mongodb.log storage:
journal:
enabled: true
dbPath: /data/mongo_28017
directoryPerDB: true
wiredTiger:
engineConfig:
cacheSizeGB: 0.5
directoryForIndexes: true
collectionConfig:
blockCompressor: zlib
indexConfig:
prefixCompression: true processManagement:
fork: true
pidFilePath: /opt/mongo_28017/pid/mongod.pid net:
port: 28017
bindIp: 127.0.0.1,10.0.0.51 replication:
oplogSizeMB: 1024
replSetName: dba
EOF

3.复制配置文件到其他节点

[mongo@db01 ~]$ cp /opt/mongo_28017/conf/mongo_28017.conf /opt/mongo_28018/conf/mongo_28018.conf
[mongo@db01 ~]$ cp /opt/mongo_28017/conf/mongo_28017.conf /opt/mongo_28019/conf/mongo_28019.conf

4.替换端口号

[mongo@db01 ~]$ sed -i 's#28017#28018#g' /opt/mongo_28018/conf/mongo_28018.conf
[mongo@db01 ~]$ sed -i 's#28017#28019#g' /opt/mongo_28019/conf/mongo_28019.conf

5.启动所有节点

mongod -f /opt/mongo_28017/conf/mongo_28017.conf
mongod -f /opt/mongo_28018/conf/mongo_28018.conf
mongod -f /opt/mongo_28019/conf/mongo_28019.conf

6.初始化集群

config = {
_id : "dba",
members : [
{_id : 0, host : "db01:28017"},
{_id : 1, host : "db01:28018"},
{_id : 2, host : "db01:28019"},
]}
rs.initiate(config)

登录db01 :28017 ,执行命令,再开两个窗口,登录db01:28018,db0:28019

(可以发送到所有会话,看下变化)

[mongo@db01 ~]$  mongo db01:28017
> config = {
... ... _id : "dba58",
... ... members : [
... ... {_id : 0, host : " db01:28017"},
... ... {_id : 1, host : " db01:28018"},
... ... {_id : 2, host : " db01:28019"},
... ... ] } > rs.initiate(config)
{
"ok" : 0,
"errmsg" : "Attempting to initiate a replica set with name dba58, but command line reports dba; rejecting",
"code" : 93,
"codeName" : "InvalidReplicaSetConfig"
}

7.插入数据

db.inventory.insertMany( [
{ "item": "journal", "qty": 25, "size": { "h": 14, "w": 21, "uom": "cm" }, "status": "A" },
{ "item": "notebook", "qty": 50, "size": { "h": 8.5, "w": 11, "uom": "in" }, "status": "A" },
{ "item": "paper", "qty": 100, "size": { "h": 8.5, "w": 11, "uom": "in" }, "status": "D" },
{ "item": "planner", "qty": 75, "size": { "h": 22.85, "w": 30, "uom": "cm" }, "status": "D" },
{ "item": "postcard", "qty": 45, "size": { "h": 10, "w": 15.25, "uom": "cm" }, "status": "A" }
]);

8.副本节点登录查看数据

rs.slaveOk()
use test
db.inventory.find()

副本节点无法查看,设置之后可以

#查看库,报错
dba:SECONDARY> show dbs
2019-12-20T09:19:39.357+0800 E QUERY [thread1] Error: listDatabases failed:{
"operationTime" : Timestamp(1576804775, 1),
"ok" : 0,
"errmsg" : "not master and slaveOk=false",
"code" : 13435,
"codeName" : "NotMasterNoSlaveOk",
"$clusterTime" : {
"clusterTime" : Timestamp(1576804775, 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:67:1
shellHelper.show@src/mongo/shell/utils.js:860:19
shellHelper@src/mongo/shell/utils.js:750:15
@(shellhelp2):1:1
#设置
dba:SECONDARY> rs.slaveOk()
#再次查看
dba:SECONDARY> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
test 0.000GB

9.设置副本可读

方法1:临时生效
rs.slaveOk() 方法2:写入启动文件,永久生效
echo "rs.slaveOk()" > ~/.mongorc.js

二、查看副本集状态

查看副本集状态

rs.slaveOk()	#让副本可以读
rs.status() #查看副本集详细状态
rs.isMaster() #查看当前的主节点是谁
rs.printReplicationInfo() #oplog记录信息
rs.printSlaveReplicationInfo() #查看复制延迟信息
rs.config() #打印当前副本集的配置信息

三、副本集权重调整

模拟故障转移

Mongo--03  mongo副本集、备份与恢复

mongod -f /opt/mongo_28017/conf/mongo_28017.conf --shutdown
mongod -f /opt/mongo_28017/conf/mongo_28017.conf 或者进到admin,db.shutdownServ er()关闭,但这个有限制,必须以本地的localhost登陆点的主机端口生效
dba:PRIMARY> use admin
switched to db admin
dba:PRIMARY> db.shutdownServer()

1.查看当前副本集配置

rs.conf()

2.设置权重

config=rs.conf()
config.members[0].priority=100
rs.reconfig(config)

3.主节点主动降级

rs.stepDown()

4.恢复成默认的权重

config=rs.conf()
config.members[0].priority=1
rs.reconfig(config)

四、创建节点

1.创建新节点并启动

mkdir -p /opt/mongo_28010/{conf,log,pid}
mkdir -p /data/mongo_28010
cp /opt/mongo_28017/conf/mongo_28017.conf /opt/mongo_28010/conf/mongo_28010.conf
sed -i 's#28017#28011#g' /opt/mongo_28010/conf/mongo_28010.conf
mongod -f /opt/mongo_28011/conf/mongo_28010.conf
mongo db01:28010

2.将节点加入集群

rs.add("db01:28010")

移除

rs.remove("db01:28010")

五、仲裁节点

1.创建新节点并启动

mkdir -p /opt/mongo_28011/{conf,log,pid}
mkdir -p /data/mongo_28011
cp /opt/mongo_28017/conf/mongo_28017.conf /opt/mongo_28011/conf/mongo_28011.conf
sed -i 's#28017#28011#g' /opt/mongo_28011/conf/mongo_28011.conf
mongod -f /opt/mongo_28011/conf/mongo_28011.conf
mongo db01:28011

2.将仲裁节点加入集群

rs.addArb("db01:28011")

六、mongo备份与恢复

1.工具介绍

(1)mongoexport/mongoimport
(2)mongodump/mongorestore

2.应用场景

1.异构平台迁移  mysql <---> mongodb
2.同平台,跨大版本:mongodb 2 ----> mongodb 3
mongoexport/mongoimport:json csv 日常备份恢复时使用.
mongodump/mongorestore

3.导出工具mongoexport

单表备份
mongoexport --port 27017 -d test -c inventory -o /data/inventory.json 单表备份至csv格式
mongoexport --port 27017 -d test -c user_info --type=csv -f name,age,ad -o /data/user_info.csv

4.恢复

mongoimport --port 27017 -d test -c inventory /data/inventory.json
mongoimport --port 27017 -d test -c user_info --type=csv --headerline --file /data/user_info.csv

5.mysql数据迁移到mongo

select * from world.city into outfile '/tmp/city.csv' fields terminated by ',';
编辑csv文件,添加列名
ID,Name,CountryCode,District,Population mongoimport --port 27017 -d world -c city --type=csv --headerline --file /data/city.csv
mongoexport --port 27017 -d world -c city -o /data/city.json

6.导出与恢复

mongodump  --port 27017 -o /data/backup
mongorestore --port 27017 -d world /data/backup/world/ --drop
mongorestore --port 27017 /data/backup/ --drop

七、准备测试数据

use backup
db.backup.insertMany( [
{ "id": 1},
{ "id": 2},
{ "id": 3},
]);

全备环境

rm -rf /data/backup/*
mongodump --port 28017 --oplog -o /data/backup 增加新数据
mongo db01:28017
use backup
db.backup.insertMany( [
{ "id": 4},
{ "id": 5},
{ "id": 6},
]);

模拟删除集合

mongo db01:28017
use backup
db.backup.drop()

备份oplog

mongodump --port 28017 -d local -c oplog.rs  -o /data/backup

查找误操作时间点

use local
db.oplog.rs.find({ns:"backup.$cmd"}).pretty();

找到时间点信息

"ts" : Timestamp(1575023546, 1),

恢复数据

cd /data/backup/local/
cp oplog.rs.bson ../oplog.bson
rm -rf /data/backup/local/
mongorestore --port 28017 --oplogReplay --oplogLimit "1575023546:1" --drop /data/backup/