侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

MySQL备份与恢复之热备(3)

数据库  /  管理员 发布于 6年前   211

       在上两篇文章(MySQL备份与恢复之冷备,MySQL备份与恢复之真实环境使用冷备)中,我们提到了冷备和真实环境中使用冷备。那从这篇文章开始我们看下热备。显然热备和冷备是两个相对的概念,冷备是把数据库服务,比如MySQL,Oracle停下来,然后使用拷贝、打包或者压缩命令对数据目录进行备份;那么我们很容易想到热备就是在MySQL或者其他数据库服务在运行的情况下进行备份。但是,这里存在一个问题,因为生产库在运行的情况下,有对该库的读写,读写频率有可能高,也可能低,不管频率高低,总会就会造成备份出来的数据和生产库中的数据不一致的情况。热备这段时间,其他人不可以操作是不现实的,因为你总不可能终止用户访问Web程序。要解决这个问题,可以采用指定备份策略,比如哪个时间段进行备份,备份哪些数据等等,总之,保证数据的完整性和一致性,切记,备份重于一切!!!
       热备可以对多个库进行备份,可以对单张表或者某几张表进行备份。但是无法同时备份多个库多个表,只有分开备份。下面我们看下热备的示意图,并进行热备模拟。
示意图

 

热备模拟

1、对单个库进行备份
第一步,移除LVM快照。(如果没有创建,忽略此步)

[root@serv01 data]# lvremove /dev/data/smydata Do you really want to remove active logical volume smydata? [y/n]: y Logical volume "smydata" successfully removed

第二步,设置MySQL的密码

mysql> set password=password("123456");Query OK, 0 rows affected (0.00 sec)

第三步,查看MySQL是否启动。因为是热备,所以要求MySQL服务启动

[root@serv01 data]# /etc/init.d/mysqld status SUCCESS! MySQL running (2664)

第四步,导出单个数据库

[root@serv01 data]# cd /databackup/#本质是导出为SQL[root@serv01 databackup]# mysqldump -uroot -p123456 --database larrydb-- MySQL dump 10.13 Distrib 5.5.29, for Linux (x86_64)---- Host: localhost Database: larrydb-- -------------------------------------------------------- Server version 5.5.29-log/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;/*!40101 SET NAMES utf8 */;/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;/*!40103 SET TIME_ZONE='+00:00' */;/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;---- Current Database: `larrydb`--CREATE DATABASE /*!32312 IF NOT EXISTS*/ `larrydb` /*!40100 DEFAULT CHARACTER SET latin1 */;USE `larrydb`;---- Table structure for table `class`--DROP TABLE IF EXISTS `class`;/*!40101 SET @saved_cs_client = @@character_set_client */;/*!40101 SET character_set_client = utf8 */;CREATE TABLE `class` ( `cid` int(11) DEFAULT NULL, `cname` varchar(30) DEFAULT NULL) ENGINE=InnoDB DEFAULT CHARSET=latin1;/*!40101 SET character_set_client = @saved_cs_client */;---- Dumping data for table `class`--LOCK TABLES `class` WRITE;/*!40000 ALTER TABLE `class` DISABLE KEYS */;INSERT INTO `class` VALUES (1,'linux'),(2,'oracle');/*!40000 ALTER TABLE `class` ENABLE KEYS */;UNLOCK TABLES;---- Table structure for table `stu`--DROP TABLE IF EXISTS `stu`;/*!40101 SET @saved_cs_client = @@character_set_client */;/*!40101 SET character_set_client = utf8 */;CREATE TABLE `stu` ( `sid` int(11) DEFAULT NULL, `sname` varchar(30) DEFAULT NULL, `cid` int(11) DEFAULT NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1;/*!40101 SET character_set_client = @saved_cs_client */;---- Dumping data for table `stu`--LOCK TABLES `stu` WRITE;/*!40000 ALTER TABLE `stu` DISABLE KEYS */;INSERT INTO `stu` VALUES (1,'larry01',1),(2,'larry02',2);/*!40000 ALTER TABLE `stu` ENABLE KEYS */;UNLOCK TABLES;/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; Dump completed on 2013-09-10 18:56:06#将输出结果保存到文件中[root@serv01 databackup]# mysqldump -uroot -p123456 --database larrydb > larrydb.sql

第五步,模拟数据丢失,进入MySQL,删除数据库

[root@serv01 data]# mysql -uroot -p123456Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 4Server version: 5.5.29-log Source distributionCopyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> show databases;+--------------------+| Database  |+--------------------+| information_schema || crm  || game  || hello  || larrydb  || mnt  || mysql  || performance_schema || test  |+--------------------+9 rows in set (0.00 sec)mysql> drop database larrydb;Query OK, 2 rows affected (0.01 sec)mysql> show databases;+--------------------+| Database  |+--------------------+| information_schema || crm  || game  || hello  || mnt  || mysql  || performance_schema || test  |+--------------------+8 rows in set (0.00 sec)mysql> exitBye

第六步,导入数据

[root@serv01 databackup]# mysql -uroot -p123456 <larrydb.sql

 
第七步,登录MySQL,查看数据是否正常

[root@serv01 data]# mysql -uroot -p123456Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 6Server version: 5.5.29-log Source distributionCopyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> show database;ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'database' at line 1mysql> show databases;+--------------------+| Database  |+--------------------+| information_schema || crm  || game  || hello  || larrydb  || mnt  || mysql  || performance_schema || test  |+--------------------+9 rows in set (0.00 sec)mysql> use larrydb;Database changedmysql> select * from class;+------+--------+| cid | cname |+------+--------+| 1 | linux || 2 | oracle |+------+--------+2 rows in set (0.00 sec)mysql> select * from stu;+------+---------+------+| sid | sname | cid |+------+---------+------+| 1 | larry01 | 1 || 2 | larry02 | 2 |+------+---------+------+2 rows in set (0.00 sec)

对多个库进行备份
第一步,查看有哪些数据库

mysql> show databases;+--------------------+| Database  |+--------------------+| information_schema || crm  || game  || hello  || larrydb  || mnt  || mysql  || performance_schema || test  |+--------------------+9 rows in set (0.00 sec)mysql> use game;Database changedmysql> show tables;+----------------+| Tables_in_game |+----------------+| country || fight  || hero  |+----------------+3 rows in set (0.00 sec)mysql> select * from country;+-----+---------+----------+| cno | cname | location |+-----+---------+----------+| 10 | caowei | luoyang || 20 | shuhan | chengdou || 30 | sunwu | nanjing || 40 | houhan | luoyang || 50 | beisong | kaifeng || 60 | 魏国 | 洛阳 |+-----+---------+----------+6 rows in set (0.00 sec)

第二步,备份多个库

[root@serv01 databackup]# mysqldump -uroot -p123456 --databases larrydb game > larrydb_game.sql[root@serv01 databackup]# ll larrydb_game.sql -rw-r--r--. 1 root root 6159 Sep 10 19:05 larrydb_game.sql

第三步,模拟数据丢失。

mysql> drop database game;Query OK, 3 rows affected (0.01 sec)mysql> drop database larrydb;Query OK, 2 rows affected (0.00 sec)mysql> use crm;Database changedmysql> show tables;+---------------+| Tables_in_crm |+---------------+| test  |+---------------+1 row in set (0.00 sec)mysql> select * from test;Empty set (0.00 sec)mysql> drop database crm;Query OK, 1 row affected (0.00 sec)

第四步,恢复数据

[root@serv01 databackup]# mysql -uroot -p123456 < larrydb_game.sql 

 
第五步,查看数据是否正常

[root@serv01 data]# mysql -uroot -p123456Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 9Server version: 5.5.29-log Source distributionCopyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> show databases;+--------------------+| Database  |+--------------------+| information_schema || game  || hello  || larrydb  || mnt  || mysql  || performance_schema || test  |+--------------------+8 rows in set (0.00 sec)mysql> use game;Database changedmysql> select * from country;+-----+---------+----------+| cno | cname | location |+-----+---------+----------+| 10 | caowei | luoyang || 20 | shuhan | chengdou || 30 | sunwu | nanjing || 40 | houhan | luoyang || 50 | beisong | kaifeng || 60 | 魏国 | 洛阳 |+-----+---------+----------+6 rows in set (0.00 sec)mysql> use larrydb;Database changedmysql> select * from class;+------+--------+| cid | cname |+------+--------+| 1 | linux || 2 | oracle |+------+--------+2 rows in set (0.00 sec)

 
备份所有的库

[root@serv01 databackup]# mysqldump --help | grep all-databaseOR mysqldump [OPTIONS] --all-databases [OPTIONS] -A, --all-databases Dump all the databases. This will be same as --databases   --databases= or --all-databases), the logs will be   --all-databases or --databases is given.all-databases   FALSE[root@serv01 databackup]# mysqldump -uroot -p123456 --all-databases > all_databases.sql[root@serv01 databackup]# ll all_databases.sql -h-rw-r--r--. 1 root root 506K Sep 10 19:16 all_databases.sql

备份某张表或者某几张表
第一步,备份某张表和某几张表

[root@serv01 databackup]# mysqldump game hero country -uroot -p123456 > game_hero_country.sql[root@serv01 databackup]# ll game_hero_country.sql -rw-r--r―. 1 root root 3955 Sep 10 19:11 game_hero_country.sql

第二步,模拟数据丢失

mysql> use game;Database changedmysql> show tables;+----------------+| Tables_in_game |+----------------+| country || fight  || hero  |+----------------+3 rows in set (0.00 sec)mysql> drop table hero;Query OK, 0 rows affected (0.00 sec)mysql> drop table country;Query OK, 0 rows affected (0.00 sec) 

第三步,查看数据是否正常

[root@serv01 databackup]# mysql -uroot -p123456 < game_hero_country.sql ERROR 1046 (3D000) at line 22: No database selected[root@serv01 databackup]# mysql -uroot -p123456 --database game < game_hero_country.sql [root@serv01 databackup]# mysql -uroot -p123456 -e "select * from game.country"+-----+---------+----------+| cno | cname | location |+-----+---------+----------+| 10 | caowei | luoyang || 20 | shuhan | chengdou || 30 | sunwu | nanjing || 40 | houhan | luoyang || 50 | beisong | kaifeng || 60 | 魏国 | 洛阳 |+-----+---------+----------+

通过这两天关于MySQL热备和冷备的学习,大家是不是对MySQL备份与恢复的了解更加深入了,不管是冷备还是热备其目的都是一致的保证数据的完整性和一致性,切记,备份重于一切!!!

相信今天所学的知识在之后的学习工作中对大家有所帮助。


  • 上一条:
    MySQL备份与恢复之热拷贝(4)
    下一条:
    MySQL备份与恢复之真实环境使用冷备(2)
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 分库分表的目的、优缺点及具体实现方式介绍(0个评论)
    • DevDB - 在 VS 代码中直接访问数据库(0个评论)
    • 在ubuntu系统中实现mysql数据存储目录迁移流程步骤(0个评论)
    • 在mysql中使用存储过程批量新增测试数据流程步骤(0个评论)
    • php+mysql数据库批量根据条件快速更新、连表更新sql实现(0个评论)
    • 近期文章
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2017-06
    • 2017-08
    • 2017-09
    • 2017-10
    • 2017-11
    • 2018-01
    • 2018-05
    • 2018-10
    • 2018-11
    • 2020-02
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-08
    • 2020-09
    • 2021-02
    • 2021-04
    • 2021-07
    • 2021-08
    • 2021-11
    • 2021-12
    • 2022-02
    • 2022-03
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-07
    • 2023-08
    • 2023-10
    • 2023-11
    • 2023-12
    • 2024-01
    • 2024-03
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客