mongodb备份与恢复备份恢复mongodb备份与恢复备份mongodump --host=127.0.0.1 --port=27017 -u root -p passwd --authenticationDatabase admin -d dbname -o /data/mongodb_data恢复mongorestore --host 127.0.0.1 --port 27017 -u root -p passwd --authenticationDatabase admin -d dbname /data/mongodb_data/dbnameCSDN地址：https://blog.csdn.net/qq_16125927/article/details/119111402
1、mongodb副本集部署服务器信息1.1、 集群拓扑图1.2、下载二进制包1.3、规划数据目录1.4、其他服务器部署1.5、创建mongodb配置文件1.6、 分别启动mongodb1.7、登陆任意一个节点1.8、测试1.9、开启用户认证1.10、启动集群1.11、验证集群状态1.12、导入数据测试账户密码认证及数据是否同步1、mongodb副本集部署服务器信息序号hostnameip配置1mongodb01192.168.0.2164核8g1T2mongodb02192.168.0.2174核8g1T3mongodb03192.168.0.2184核8g1T1.1、 集群拓扑图1.2、下载二进制包本次部署集群所使用mongodb版本为3.6.20官网下载地址：https://www.mongodb.com/try/download/community1.3、规划数据目录在mongodb_01上创建安装目录[root@mongodb_01 ~]# mkdir /hdata/mongo_data/{data,log,conf,keyfile} -pv mkdir: 已创建目录 "/hdata" mkdir: 已创建目录 "/hdata/mongo_data" mkdir: 已创建目录 "/hdata/mongo_data/data" mkdir: 已创建目录 "/hdata/mongo_data/log" mkdir: 已创建目录 "/hdata/mongo_data/conf" mkdir: 已创建目录 "/hdata/mongo_data/keyfile"软件路径[root@mongodb_01 ~]# mkdir /opt/software -pv mkdir: 已创建目录 "/opt/software"将mongodb二进制文件解压到/opt/software[root@mongodb_01 software]# tar xf mongodb-linux-x86_64-rhel70-3.6.20.tgz -C /opt/software/ [root@mongodb_01 software]# ll 总用量 119116 drwxr-xr-x. 3 root root 135 9月 21 15:03 mongodb-linux-x86_64-rhel70-3.6.20 -rw-r--r--. 1 root root 121973145 9月 21 15:00 mongodb-linux-x86_64-rhel70-3.6.20.tgz1.4、其他服务器部署分别在mongodb02和03上安装第五章3节3.3操作1.5、创建mongodb配置文件创建配置文件[root@mongodb_01 ~]# vim /hdata/mongo_data/conf/mongod.conf # mongod.conf systemLog: destination: file logAppend: true path: /hdata/mongo_data/log/mongod.log storage: dbPath: /hdata/mongo_data/data journal: enabled: true processManagement: fork: true # fork and run in background pidFilePath: /hdata/mongo_data/log/mongod.pid # location of pidfile timeZoneInfo: /usr/share/zoneinfo net: port: 27018 bindIp: 0.0.0.0 # Listen to local interface only, comment to listen on all interfaces. #security: # authorization: enabled # keyFile: /home/mongodb/37017/keyfile/keyfile # 设置副本集名称 replication: replSetName: "rs01"将mongodb_01的配置文件拷贝到另外2个节点# 拷贝到mongodb_02 [root@mongodb_01 ~]# scp /hdata/mongo_data/conf/mongod.conf mongodb_02:/hdata/mongo_data/conf/ # 拷贝到mongodb_03上 [root@mongodb_01 ~]# scp /hdata/mongo_data/conf/mongod.conf mongodb_03:/hdata/mongo_data/conf/1.6、 分别启动mongodbmongodb_01[root@mongodb_01 ~]# /opt/software/mongodb-linux-x86_64-rhel70-3.6.20/bin/mongod -f /hdata/mongo_data/conf/mongod.conf about to fork child process, waiting until server is ready for connections. forked process: 2402 child process started successfully, parent exiting [root@mongodb_01 ~]# ss -nutl | grep 27018 tcp LISTEN 0 128 *:27018 *:*mongodb_02[root@mongodb_02 ~]# /opt/software/mongodb-linux-x86_64-rhel70-3.6.20/bin/mongod -f /hdata/mongo_data/conf/mongod.conf about to fork child process, waiting until server is ready for connections. forked process: 4418 child process started successfully, parent exiting [root@mongodb_02 ~]# ss -nutl | grep 27018 tcp LISTEN 0 128 *:27018 *:*mongodb_03[root@mongodb_03 ~]# /opt/software/mongodb-linux-x86_64-rhel70-3.6.20/bin/mongod -f /hdata/mongo_data/conf/mongod.conf about to fork child process, waiting until server is ready for connections. forked process: 4453 child process started successfully, parent exiting [root@mongodb_03 ~]# ss -nutl | grep 27018 tcp LISTEN 0 128 *:27018 *:*1.7、登陆任意一个节点a> 开启副本集集群[root@mongodb_01 ~]# /opt/software/mongodb-linux-x86_64-rhel70-3.6.20/bin/mongo --port 27018 # 创建副本集配置文件 > conf={"_id":"rs01","members":[{"_id":0,"host":"192.168.0.216:27018"},{"_id":1,"host":"192.168.0.217:27018"},{"_id":2,"host":"192.168.0.218:27018"}]} { "_id" : "rs01", "members" : [ { "_id" : 0, "host" : "192.168.0.216:27018" }, { "_id" : 1, "host" : "192.168.0.217:27018" }, { "_id" : 2, "host" : "192.168.0.218:27018" } ] } # 初始化副本集配置文件 > rs.initiate(conf) { "ok" : 1, "operationTime" : Timestamp(1600674253, 1), "$clusterTime" : { "clusterTime" : Timestamp(1600674253, 1), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } } }b> 查看副本集集群状态rs01:SECONDARY> rs.status() { "set" : "rs01", "date" : ISODate("2020-09-21T07:45:25.575Z"), "myState" : 1, "term" : NumberLong(1), "syncingTo" : "", "syncSourceHost" : "", "syncSourceId" : -1, "heartbeatIntervalMillis" : NumberLong(2000), "optimes" : { "lastCommittedOpTime" : { "ts" : Timestamp(1600674316, 1), "t" : NumberLong(1) }, "readConcernMajorityOpTime" : { "ts" : Timestamp(1600674316, 1), "t" : NumberLong(1) }, "appliedOpTime" : { "ts" : Timestamp(1600674316, 1), "t" : NumberLong(1) }, "durableOpTime" : { "ts" : Timestamp(1600674316, 1), "t" : NumberLong(1) } }, "members" : [ { "_id" : 0, #第一个节点的id号 "name" : "192.168.0.216:27018", #第一个节点的ip+port "health" : 1, #第一个节点的健康情况：1表示正常，0表示不正常 "state" : 1, "stateStr" : "PRIMARY", #第一个节点在副本集的集群状态，primary表示主节点，secondary表示从节点 "uptime" : 1087, "optime" : { "ts" : Timestamp(1600674316, 1), "t" : NumberLong(1) }, "optimeDate" : ISODate("2020-09-21T07:45:16Z"), "syncingTo" : "", "syncSourceHost" : "", "syncSourceId" : -1, "infoMessage" : "could not find member to sync from", "electionTime" : Timestamp(1600674264, 1), "electionDate" : ISODate("2020-09-21T07:44:24Z"), "configVersion" : 1, "self" : true, "lastHeartbeatMessage" : "" }, { "_id" : 1, "name" : "192.168.0.217:27018", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 72, "optime" : { "ts" : Timestamp(1600674316, 1), "t" : NumberLong(1) }, "optimeDurable" : { "ts" : Timestamp(1600674316, 1), "t" : NumberLong(1) }, "optimeDate" : ISODate("2020-09-21T07:45:16Z"), "optimeDurableDate" : ISODate("2020-09-21T07:45:16Z"), "lastHeartbeat" : ISODate("2020-09-21T07:45:24.593Z"), "lastHeartbeatRecv" : ISODate("2020-09-21T07:45:25.379Z"), "pingMs" : NumberLong(0), "lastHeartbeatMessage" : "", "syncingTo" : "192.168.31.11:27017", "syncSourceHost" : "192.168.31.11:27017", "syncSourceId" : 0, "infoMessage" : "", "configVersion" : 1 }, { "_id" : 2, "name" : "192.168.0.218:27018", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 72, "optime" : { "ts" : Timestamp(1600674316, 1), "t" : NumberLong(1) }, "optimeDurable" : { "ts" : Timestamp(1600674316, 1), "t" : NumberLong(1) }, "optimeDate" : ISODate("2020-09-21T07:45:16Z"), "optimeDurableDate" : ISODate("2020-09-21T07:45:16Z"), "lastHeartbeat" : ISODate("2020-09-21T07:45:24.592Z"), "lastHeartbeatRecv" : ISODate("2020-09-21T07:45:25.379Z"), "pingMs" : NumberLong(0), "lastHeartbeatMessage" : "", "syncingTo" : "192.168.31.11:27017", "syncSourceHost" : "192.168.31.11:27017", "syncSourceId" : 0, "infoMessage" : "", "configVersion" : 1 } ], "ok" : 1, "operationTime" : Timestamp(1600674316, 1), "$clusterTime" : { "clusterTime" : Timestamp(1600674316, 1), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } } }c> 查看副本集集群配置rs01:PRIMARY> rs.conf() { "_id" : "rs01", "version" : 1, "protocolVersion" : NumberLong(1), "members" : [ { "_id" : 0, "host" : "192.168.0.216:27018", "arbiterOnly" : false, "buildIndexes" : true, "hidden" : false, "priority" : 1, #该节点的优先级 "tags" : { }, "slaveDelay" : NumberLong(0), "votes" : 1 }, { "_id" : 1, "host" : "192.168.0.217:27018", "arbiterOnly" : false, "buildIndexes" : true, "hidden" : false, "priority" : 1, "tags" : { }, "slaveDelay" : NumberLong(0), "votes" : 1 }, { "_id" : 2, "host" : "192.168.0.218:27018", "arbiterOnly" : false, "buildIndexes" : true, "hidden" : false, "priority" : 1, "tags" : { }, "slaveDelay" : NumberLong(0), "votes" : 1 } ], "settings" : { "chainingAllowed" : true, "heartbeatIntervalMillis" : 2000, "heartbeatTimeoutSecs" : 10, "electionTimeoutMillis" : 10000, "catchUpTimeoutMillis" : -1, "catchUpTakeoverDelayMillis" : 30000, "getLastErrorModes" : { }, "getLastErrorDefaults" : { "w" : 1, "wtimeout" : 0 }, "replicaSetId" : ObjectId("5f6859cde1d641b3427af2a7") } }d> 修改mongodb01和02优先级，03优先级保持不变，只参与选举及数据同步#自定义服务配置信息 rs01:PRIMARY> cfg=rs.conf() { "_id" : "rs01", "version" : 1, "protocolVersion" : NumberLong(1), "members" : [ { "_id" : 0, "host" : "192.168.0.216:27018", "arbiterOnly" : false, "buildIndexes" : true, "hidden" : false, "priority" : 1, "tags" : { }, "slaveDelay" : NumberLong(0), "votes" : 1 }, { "_id" : 1, "host" : "192.168.0.217:27018", "arbiterOnly" : false, "buildIndexes" : true, "hidden" : false, "priority" : 1, "tags" : { }, "slaveDelay" : NumberLong(0), "votes" : 1 }, { "_id" : 2, "host" : "192.168.0.218:27018", "arbiterOnly" : false, "buildIndexes" : true, "hidden" : false, "priority" : 1, "tags" : { }, "slaveDelay" : NumberLong(0), "votes" : 1 } ], "settings" : { "chainingAllowed" : true, "heartbeatIntervalMillis" : 2000, "heartbeatTimeoutSecs" : 10, "electionTimeoutMillis" : 10000, "catchUpTimeoutMillis" : -1, "catchUpTakeoverDelayMillis" : 30000, "getLastErrorModes" : { }, "getLastErrorDefaults" : { "w" : 1, "wtimeout" : 0 }, "replicaSetId" : ObjectId("5f6859cde1d641b3427af2a7") } } # mongodb_02服务器优先级，id=1，优先级是2 或 如果做高可用，1宕机，2提升主，3一直为SECONDARY，可以把1和2的优先级都设置为2,3还是1，保持不变 # 这样就可以实现1宕机，2提升主，3参与选举，并且1恢复后以从节点加入，避免不必要的主从切换，确保2的数据能及时同步到1 rs01:PRIMARY> cfg.members[1].priority=2 2 rs01:PRIMARY> cfg.members[0].priority=2 2 # 重新加载配置信息 rs01:PRIMARY> rs.reconfig(cfg) { "ok" : 1, "operationTime" : Timestamp(1600675177, 1), "$clusterTime" : { "clusterTime" : Timestamp(1600675177, 1), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } } }e> 重新查看集群配置信息rs01:SECONDARY> rs.conf() { "_id" : "rs01", "version" : 2, "protocolVersion" : NumberLong(1), "members" : [ { "_id" : 0, "host" : "192.168.0.216:27018", "arbiterOnly" : false, "buildIndexes" : true, "hidden" : false, "priority" : 1, "tags" : { }, "slaveDelay" : NumberLong(0), "votes" : 1 }, { "_id" : 1, "host" : "192.168.0.217:27018", "arbiterOnly" : false, "buildIndexes" : true, "hidden" : false, "priority" : 2, "tags" : { }, "slaveDelay" : NumberLong(0), "votes" : 1 }, { "_id" : 2, "host" : "192.168.0.218:27018", "arbiterOnly" : false, "buildIndexes" : true, "hidden" : false, "priority" : 1, "tags" : { }, "slaveDelay" : NumberLong(0), "votes" : 1 } ], "settings" : { "chainingAllowed" : true, "heartbeatIntervalMillis" : 2000, "heartbeatTimeoutSecs" : 10, "electionTimeoutMillis" : 10000, "catchUpTimeoutMillis" : -1, "catchUpTakeoverDelayMillis" : 30000, "getLastErrorModes" : { }, "getLastErrorDefaults" : { "w" : 1, "wtimeout" : 0 }, "replicaSetId" : ObjectId("5f6859cde1d641b3427af2a7") } }f> 重新查看集群状态rs01:SECONDARY> rs.status() { "set" : "rs01", "date" : ISODate("2020-09-21T08:04:54.966Z"), "myState" : 2, "term" : NumberLong(2), "syncingTo" : "192.168.0.216:27018", "syncSourceHost" : "192.168.0.216:27018", "syncSourceId" : 2, "heartbeatIntervalMillis" : NumberLong(2000), "optimes" : { "lastCommittedOpTime" : { "ts" : Timestamp(1600675488, 1), "t" : NumberLong(2) }, "readConcernMajorityOpTime" : { "ts" : Timestamp(1600675488, 1), "t" : NumberLong(2) }, "appliedOpTime" : { "ts" : Timestamp(1600675488, 1), "t" : NumberLong(2) }, "durableOpTime" : { "ts" : Timestamp(1600675488, 1), "t" : NumberLong(2) } }, "members" : [ { "_id" : 0, "name" : "192.168.0.216:27018", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 2256, "optime" : { "ts" : Timestamp(1600675488, 1), "t" : NumberLong(2) }, "optimeDate" : ISODate("2020-09-21T08:04:48Z"), "syncingTo" : "192.168.0.216:27018", "syncSourceHost" : "192.168.0.216:27018", "syncSourceId" : 2, "infoMessage" : "", "configVersion" : 2, "self" : true, "lastHeartbeatMessage" : "" }, { "_id" : 1, "name" : "192.168.0.217:27018", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", #id为1的服务已提升为主节点 "uptime" : 1241, "optime" : { "ts" : Timestamp(1600675488, 1), "t" : NumberLong(2) }, "optimeDurable" : { "ts" : Timestamp(1600675488, 1), "t" : NumberLong(2) }, "optimeDate" : ISODate("2020-09-21T08:04:48Z"), "optimeDurableDate" : ISODate("2020-09-21T08:04:48Z"), "lastHeartbeat" : ISODate("2020-09-21T08:04:53.510Z"), "lastHeartbeatRecv" : ISODate("2020-09-21T08:04:54.489Z"), "pingMs" : NumberLong(0), "lastHeartbeatMessage" : "", "syncingTo" : "", "syncSourceHost" : "", "syncSourceId" : -1, "infoMessage" : "", "electionTime" : Timestamp(1600675187, 1), "electionDate" : ISODate("2020-09-21T07:59:47Z"), "configVersion" : 2 }, { "_id" : 2, "name" : "192.168.0.218:27018", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 1241, "optime" : { "ts" : Timestamp(1600675488, 1), "t" : NumberLong(2) }, "optimeDurable" : { "ts" : Timestamp(1600675488, 1), "t" : NumberLong(2) }, "optimeDate" : ISODate("2020-09-21T08:04:48Z"), "optimeDurableDate" : ISODate("2020-09-21T08:04:48Z"), "lastHeartbeat" : ISODate("2020-09-21T08:04:53.436Z"), "lastHeartbeatRecv" : ISODate("2020-09-21T08:04:52.966Z"), "pingMs" : NumberLong(0), "lastHeartbeatMessage" : "", "syncingTo" : "192.168.0.216:27018", "syncSourceHost" : "192.168.0.216:27018", "syncSourceId" : 1, "infoMessage" : "", "configVersion" : 2 } ], "ok" : 1, "operationTime" : Timestamp(1600675488, 1), "$clusterTime" : { "clusterTime" : Timestamp(1600675488, 1), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } } }1.8、测试主节点插入数据rs01:PRIMARY> for(var i=0;i<10000;i++){db.customer.insert({"name":"user"+i})} WriteResult({ "nInserted" : 1 }) rs01:PRIMARY> db.customer.count() 10000从节点查数据rs01:SECONDARY> rs.secondaryOk() #开启从节点可查询条件 rs01:SECONDARY> db.customer.count() 100001.9、开启用户认证a> 在主节点创建用户rs01:PRIMARY> use admin switched to db admin rs01:PRIMARY> db.createUser({user:"root",pwd:"root@0321",roles:[{role:"root",db:"admin"}]}) Successfully added user: { "user" : "root", "roles" : [ { "role" : "root", "db" : "admin" } ] }b> 创建安全认证依次停止mongodb副本集集群首先停从节点，然后停止主节点#主节点是mongodb_02,从节点是mongodb_01和mongodb_03 #停止从节点 /opt/software/mongodb-linux-x86_64-rhel70-3.6.20/bin/mongod -shutdown -dbpath=/hdata/mongo_data/data #停止主节点 /opt/software/mongodb-linux-x86_64-rhel70-3.6.20/bin/mongod -shutdown -dbpath=/hdata/mongo_data/datac> 创建keyfile文件在任意一台服务器上创建，再复制到其他服务器上[root@mongodb_01 keyfile]# pwd /hdata/mongo_data/keyfile [root@mongodb_01 keyfile]# openssl rand -base64 756 > keyfile [root@mongodb_01 keyfile]# ll 总用量 4 -rw-r--r--. 1 root root 1024 9月 21 16:32 keyfile [root@mongodb_01 keyfile]# chmod 600 keyfile [root@mongodb_01 keyfile]# ll 总用量 4 -rw-------. 1 root root 1024 9月 21 16:32 keyfile [root@mongodb_01 keyfile]# scp keyfile 192.168.0.217:/hdata/mongo_data/keyfile/ root@192.168.0.217's password: keyfile 100% 1024 1.1MB/s 00:00 [root@mongodb_01 keyfile]# scp keyfile 192.168.0.218:/hdata/mongo_data/keyfile/ root@192.168.0.218's password: keyfile 100% 1024 994.4KB/s 00:00 [root@mongodb_01 keyfile]#配置mongodb配置文件，然后分发到其他2台服务器上vim /hdata/mongo_data/conf/mongod.conf # mongod.conf systemLog: destination: file logAppend: true path: /hdata/mongo_data/log/mongod.log storage: dbPath: /hdata/mongo_data/data journal: enabled: true processManagement: fork: true # fork and run in background pidFilePath: /hdata/mongo_data/log/mongod.pid # location of pidfile timeZoneInfo: /usr/share/zoneinfo net: port: 27018 bindIp: 0.0.0.0 # Listen to local interface only, comment to listen on all interfaces. #开启认证 security: authorization: enabled keyFile: /hdata/mongo_data/keyfile/keyfile # 设置副本集名称 replication: replSetName: "rs01"1.10、启动集群先启动主节点，再启动从节点启动mongodb02主节点/opt/software/mongodb-linux-x86_64-rhel70-3.6.20/bin/mongod -f /hdata/mongo_data/conf/mongod.conf启动从节点01和03/opt/software/mongodb-linux-x86_64-rhel70-3.6.20/bin/mongod -f /hdata/mongo_data/conf/mongod.conf1.11、验证集群状态rs01:PRIMARY> use admin switched to db admin rs01:PRIMARY> db.auth("root","root@0321") 1 rs01:PRIMARY> rs.status()1.12、导入数据测试账户密码认证及数据是否同步主节点导入数据#主节点导入数据 [root@mongodb_02 mongo_data]# /opt/software/mongodb-linux-x86_64-rhel70-3.6.20/bin/mongoimport --host 127.0.0.1 --port 27017 -d testa -u root -p root@0321 --authenticationDatabase admin -c thumb_image --file thumb_image.json 2020-09-21T16:52:28.555+0800 connected to: 127.0.0.1:27017 2020-09-21T16:52:31.328+0800 imported 178 documents检查从节点数据rs01:SECONDARY> db.auth("root","root@0321") 1 rs01:SECONDARY> rs.secondaryOk() rs01:SECONDARY> show dbs admin 0.000GB config 0.000GB local 0.027GB test 0.000GB testa 0.026GB rs01:SECONDARY> use testa switched to db testa rs01:SECONDARY> show collections thumb_image