教你如何利用MySQL学习MongoDB之备份和恢复

数据库 MySQL 其他数据库 MongoDB
在上文中,我们了解了教你如何利用MySQL学习MongoDB之授权和权限,本文中我们继续我们的学习之旅,学习两者的备份和恢复。

在上文中,我们了解了教你如何利用MySQL学习MongoDB之授权和权限,本文中我们继续我们的学习之旅,学习两者的备份和恢复。

在数据库表丢失或损坏的情况下,备份你的数据库是很重要的。如果发生系统崩溃,你肯定想能够将你的表尽可能丢失最少的数据恢复到崩溃发生时的状态。

1、MySQL备份和恢复

MySQL备份方式大体上分为以下3种:

直接拷贝数据库文件

使用mysqlhotcopy备份数据库

使用mysqldump备份数据库

(1)、直接拷贝数据库文件

最为直接、快速、方便,缺点是基本上不能实现增量备份。为了保证数据的一致性,需要在靠背文件前,执行以下 SQL 语句:

FLUSH TABLES WITH READ LOCK;

也就是把内存中的数据都刷新到磁盘中,同时锁定数据表,以保证拷贝过程中不会有新的数据写入。这种方法备份出来的数据恢复也很简单,直接拷贝回原来的数据库目录下即可。

但对于 Innodb 类型表来说,还需要备份其日志文件,即 ib_logfile* 文件。因为当 Innodb 表损坏时,就可以依靠这些日志文件来恢复。

(2)、使用mysqlhotcopy备份数据库

mysqlhotcopy 是perl程序。它使用 LOCK TABLES、FLUSH TABLES 和 cp 或 scp 来快速备份数据库。对于备份数据库或单个表来说它是最快的途径,但它只能运行在本地服务器上,且mysqlhotcopy 只能备份 MyISAM表,对于Innodb表则无招可施了。

(3)、使用mysqldump备份数据库

mysqldump 是SQL级别的备份,它将数据表导成 SQL 脚本文件,在不同的 MySQL 版本之间升级时相对比较合适,这也是最主流的备份方法。

2、MongoDB备份和恢复

MongoDB提供了两个命令来备份(mongodump )和恢复(mongorestore )数据库。

(1)、mongodump备份工具

我们先看一下此工具的帮助信息:

  1. [root@localhost bin]# ./mongodump --help  
  2. options:  
  3. --help produce help message  
  4. -v [ --verbose ] be more verbose (include multiple times for more  
  5. verbosity e.g. -vvvvv)  
  6. -h [ --host ] arg mongo host to connect to ( /s1,s2 for  
  7. sets)  
  8. --port arg server port. Can also use --host hostname:port  
  9. --ipv6 enable IPv6 support (disabled by default)  
  10. -u [ --username ] arg username  
  11. -p [ --password ] arg password  
  12. --dbpath arg directly access mongod database files in the given  
  13. path, instead of connecting to a mongod server -  
  14. needs to lock the data directory, so cannot be used  
  15. if a mongod is currently accessing the same path  
  16. --directoryperdb if dbpath specified, each db is in a separate  
  17. directory  
  18. -d [ --db ] arg database to use  
  19. -c [ --collection ] arg collection to use (some commands)  
  20. -o [ --out ] arg (=dump) output directory or "-" for stdout  
  21. -q [ --query ] arg json query  
  22. --oplog Use oplog for point-in-time snapshotting  
  23. --repair try to recover a crashed database  
  24. [root@localhost bin]#  

例如我们的系统中有一个叫做”foo”库,下面我们将演示如何将这个库备份出来:

  1. [root@localhost bin]# ./mongodump -d foo -o /data/dump  
  2. connected to: 127.0.0.1  
  3. DATABASE: foo to /data/dump/foo  
  4. foo.system.indexes to /data/dump/foo/system.indexes.bson  
  5. 3 objects  
  6. foo.system.users to /data/dump/foo/system.users.bson  
  7. 1 objects  
  8. foo.t2 to /data/dump/foo/t2.bson  
  9. 1 objects  
  10. foo.t1 to /data/dump/foo/t1.bson  
  11. 2 objects  
  12. [root@localhost bin]#  

通过工具返回信息,我们可以看到foo中的数据已经被备份成bson格式的文件了, 接下来我们到备份的目录下去验证一下:

  1. [root@localhost dump]# ll /data/dump/foo/  
  2. 总计 16  
  3. -rw-r--r-- 1 root root 193 04-22 11:55 system.indexes.bson  
  4. -rw-r--r-- 1 root root 91 04-22 11:55 system.users.bson  
  5. -rw-r--r-- 1 root root 66 04-22 11:55 t1.bson  
  6. -rw-r--r-- 1 root root 49 04-22 11:55 t2.bson  
  7. [root@localhost dump]#  

结果证明foo库中的表已经被成功备份出来,接下来我们将演示如何将备份恢复回去。

(2)、mongorestore恢复工具

我们先看一下此工具的帮助信息:

  1. [root@localhost bin]# ./mongorestore --help  
  2. usage: ./mongorestore [options] [directory or filename to restore from]  
  3. options:  
  4. --help produce help message  
  5. -v [ --verbose ] be more verbose (include multiple times for more  
  6. verbosity e.g. -vvvvv)  
  7. -h [ --host ] arg mongo host to connect to ( /s1,s2 for sets)  
  8. --port arg server port. Can also use --host hostname:port  
  9. --ipv6 enable IPv6 support (disabled by default)  
  10. -u [ --username ] arg username  
  11. -p [ --password ] arg password  
  12. --dbpath arg directly access mongod database files in the given  
  13. path, instead of connecting to a mongod server -  
  14. needs to lock the data directory, so cannot be used  
  15. if a mongod is currently accessing the same path  
  16. --directoryperdb if dbpath specified, each db is in a separate  
  17. directory  
  18. -d [ --db ] arg database to use  
  19. -c [ --collection ] arg collection to use (some commands)  
  20. --objcheck validate object before inserting  
  21. --filter arg filter to apply before inserting  
  22. --drop drop each collection before import  
  23. --oplogReplay replay oplog for point-in-time restore  
  24. [root@localhost bin]# 

例如我们先将”foo”库删除了:

  1. [root@localhost bin]# ./mongo  
  2. MongoDB shell version: 1.8.1  
  3. connecting to: test  
  4. > use foo  
  5. switched to db foo  
  6. > db.dropDatabase();  
  7. "dropped" : "foo""ok" : 1 }  
  8. > show dbs  
  9. admin 0.0625GB  
  10. local (empty)  
  11. test 0.0625GB  

然后下面我们将演示如何恢复这个库:

  1. [root@localhost bin]# ./mongorestore --directoryperdb /data/dump  
  2. connected to: 127.0.0.1  
  3. Sun Apr 22 12:01:27 /data/dump/foo/t1.bson  
  4. Sun Apr 22 12:01:27 going into namespace [foo.t1]  
  5. Sun Apr 22 12:01:27 2 objects found  
  6. Sun Apr 22 12:01:27 /data/dump/foo/t2.bson  
  7. Sun Apr 22 12:01:27 going into namespace [foo.t2]  
  8. Sun Apr 22 12:01:27 1 objects found  
  9. Sun Apr 22 12:01:27 /data/dump/foo/system.users.bson  
  10. Sun Apr 22 12:01:27 going into namespace [foo.system.users]  
  11. Sun Apr 22 12:01:27 1 objects found  
  12. Sun Apr 22 12:01:27 /data/dump/foo/system.indexes.bson  
  13. Sun Apr 22 12:01:27 going into namespace [foo.system.indexes]  
  14. Sun Apr 22 12:01:27 { name"_id_", ns: "foo.system.users"key: { _id: 1 }, v: 0 }  
  15. Sun Apr 22 12:01:27 { name"_id_", ns: "foo.t2"key: { _id: 1 }, v: 0 }  
  16. Sun Apr 22 12:01:27 { name"_id_", ns: "foo.t1"key: { _id: 1 }, v: 0 }  
  17. Sun Apr 22 12:01:27 3 objects found  
  18. [root@localhost bin]# 

通过工具返回信息,我们可以看到foo中的数据已经被恢复回来了, 接下来我们到库里去验证一下:

  1. [root@localhost bin]# ./mongo  
  2. MongoDB shell version: 1.8.1  
  3. connecting to: test  
  4. > use foo  
  5. switched to db foo  
  6. > show collections;  
  7. system.indexes  
  8. system.users  
  9. t1  
  10. t2  

结果证明foo库表已经被成功恢复回来了。

 

【编辑推荐】

  1. 教你如何利用MySQL学习MongoDB之SQL语法
  2. 教你如何利用MySQL学习MongoDB之数据存储结构
  3. 如何解决PHP+MySQL出现乱码的现象
  4. 教你如何利用MySQL学习MongoDB之安装篇
  5. MySQL配置时提示无法连接到MySQL本地服务器
责任编辑:艾婧 来源: itpub
相关推荐

2011-05-24 09:51:07

MySQLMongoDB

2011-05-24 09:23:16

MySQLMongoDB

2011-05-23 09:23:19

MySQLMongoDB

2011-05-24 09:10:24

MySQLMongoDB

2011-05-23 13:30:00

MySQLMongoDB

2023-08-03 07:39:10

MongoDB数据备份

2010-04-22 18:37:18

Aix系统

2023-12-07 15:09:23

2010-05-26 13:50:15

MySQL备份

2010-03-31 10:39:40

RMANOracle

2017-06-22 08:41:58

MySQLibd文件恢复数据

2011-03-04 14:39:03

MySQL数据库mysqldump

2017-11-13 13:33:09

MySQL全备份恢复表

2010-05-21 18:15:41

MySQL 备份

2011-08-04 18:27:55

注册表

2015-10-21 14:07:17

Oracle备份Oracle恢复

2011-07-26 13:55:01

MongoDB备份与恢复

2017-07-10 14:26:03

Mysql数据备份数据恢复

2009-11-20 09:29:53

2023-12-01 10:21:00

机器学习算法
点赞
收藏

51CTO技术栈公众号