关闭 x
IT技术网
    技 采 号
    ITJS.cn - 技术改变世界
    • 实用工具
    • 菜鸟教程
    IT采购网 中国存储网 科技号 CIO智库

    IT技术网

    IT采购网
    • 首页
    • 行业资讯
    • 系统运维
      • 操作系统
        • Windows
        • Linux
        • Mac OS
      • 数据库
        • MySQL
        • Oracle
        • SQL Server
      • 网站建设
    • 人工智能
    • 半导体芯片
    • 笔记本电脑
    • 智能手机
    • 智能汽车
    • 编程语言
    IT技术网 - ITJS.CN
    首页 » MySQL »教你如何利用MySQL学习MongoDB之备份和恢复

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

    2011-05-24 10:11:00 出处:ITJS
    分享

    微信扫一扫:分享

    Scan me!

    微信里点“发现”,扫一下

    二维码便可将本文分享至朋友圈。

    前一篇文章里,,我们了解了教你如何利用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备份工具

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

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

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

    [chinastor.com-root@localhost bin]# ./mongodump -d foo -o /data/dump  connected to: 127.0.0.1  DATABASE: foo to /data/dump/foo  foo.system.indexes to /data/dump/foo/system.indexes.bson  3 objects  foo.system.users to /data/dump/foo/system.users.bson  1 objects  foo.t2 to /data/dump/foo/t2.bson  1 objects  foo.t1 to /data/dump/foo/t1.bson  2 objects  [chinastor.com-root@localhost bin]#  

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

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

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

    (2)、mongorestore恢复工具

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

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

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

    [chinastor.com-root@localhost bin]# ./mongo  MongoDB shell version: 1.8.1  connecting to: test  > use foo  switched to db foo  > db.dropDatabase();  { "dropped" : "foo", "ok" : 1 }  > show dbs  admin 0.0625GB  local (empty)  test 0.0625GB  > 

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

    [chinastor.com-root@localhost bin]# ./mongorestore --directoryperdb /data/dump  connected to: 127.0.0.1  Sun Apr 22 12:01:27 /data/dump/foo/t1.bson  Sun Apr 22 12:01:27 going into namespace [foo.t1]  Sun Apr 22 12:01:27 2 objects found  Sun Apr 22 12:01:27 /data/dump/foo/t2.bson  Sun Apr 22 12:01:27 going into namespace [foo.t2]  Sun Apr 22 12:01:27 1 objects found  Sun Apr 22 12:01:27 /data/dump/foo/system.users.bson  Sun Apr 22 12:01:27 going into namespace [foo.system.users]  Sun Apr 22 12:01:27 1 objects found  Sun Apr 22 12:01:27 /data/dump/foo/system.indexes.bson  Sun Apr 22 12:01:27 going into namespace [foo.system.indexes]  Sun Apr 22 12:01:27 { name: "_id_", ns: "foo.system.users", key: { _id: 1 }, v: 0 }  Sun Apr 22 12:01:27 { name: "_id_", ns: "foo.t2", key: { _id: 1 }, v: 0 }  Sun Apr 22 12:01:27 { name: "_id_", ns: "foo.t1", key: { _id: 1 }, v: 0 }  Sun Apr 22 12:01:27 3 objects found  [chinastor.com-root@localhost bin]# 

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

    [chinastor.com-root@localhost bin]# ./mongo  MongoDB shell version: 1.8.1  connecting to: test  > use foo  switched to db foo  > show collections;  system.indexes  system.users  t1  t2  > 

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

    上一篇返回首页 下一篇

    声明: 此文观点不代表本站立场;转载务必保留本文链接;版权疑问请联系我们。

    别人在看

    抖音安全与信任开放日:揭秘推荐算法,告别单一标签依赖

    ultraedit编辑器打开文件时,总是提示是否转换为DOS格式,如何关闭?

    Cornell大神Kleinberg的经典教材《算法设计》是最好入门的算法教材

    从 Microsoft 下载中心安装 Windows 7 SP1 和 Windows Server 2008 R2 SP1 之前要执行的步骤

    Llama 2基于UCloud UK8S的创新应用

    火山引擎DataTester:如何使用A/B测试优化全域营销效果

    腾讯云、移动云继阿里云降价后宣布大幅度降价

    字节跳动数据平台论文被ICDE2023国际顶会收录,将通过火山引擎开放相关成果

    这个话题被围观超10000次,火山引擎VeDI如此解答

    误删库怎么办?火山引擎DataLeap“3招”守护数据安全

    IT头条

    平替CUDA!摩尔线程发布MUSA 4性能分析工具

    00:43

    三起案件揭开侵犯个人信息犯罪的黑灰产业链

    13:59

    百度三年开放2.1万实习岗,全力培育AI领域未来领袖

    00:36

    工信部:一季度,电信业务总量同比增长7.7%,业务收入累计完成4469亿元

    23:42

    Gartner:2024年全球半导体营收6559亿美元,AI助力英伟达首登榜首

    18:04

    技术热点

    iOS 8 中如何集成 Touch ID 功能

    windows7系统中鼠标滑轮键(中键)的快捷应用

    MySQL数据库的23个特别注意的安全事项

    Kruskal 最小生成树算法

    Ubuntu 14.10上安装新的字体图文教程

    Ubuntu14更新后无法进入系统卡在光标界面解怎么办?

      友情链接:
    • IT采购网
    • 科技号
    • 中国存储网
    • 存储网
    • 半导体联盟
    • 医疗软件网
    • 软件中国
    • ITbrand
    • 采购中国
    • CIO智库
    • 考研题库
    • 法务网
    • AI工具网
    • 电子芯片网
    • 安全库
    • 隐私保护
    • 版权申明
    • 联系我们
    IT技术网 版权所有 © 2020-2025,京ICP备14047533号-20,Power by OK设计网

    在上方输入关键词后,回车键 开始搜索。Esc键 取消该搜索窗口。