首页 > 数据库 > MySQL > 正文

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

2024-07-24 13:07:49
字体:
来源:转载
供稿:网友

热备使用mysqldump命令进行备份,此工具是MySQL内置的备份和恢复工具,功能强大,它可以对整个库进行备份,可以对多个库进行备份,可以对单张表或者某几张表进行备份,需要了解的朋友可以参考下

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

热备可以对多个库进行备份,可以对单张表或者某几张表进行备份。但是无法同时备份多个库多个表,只有分开备份。下面我们看下热备的示意图,并进行热备模拟。

示意图

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

热备模拟

1、对单个库进行备份

第一步,移除LVM快照。(如果没有创建,忽略此步)

 

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

第二步,设置MySQL的密码

 

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

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

 

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

第四步,导出单个数据库

 

 
  1. [root@serv01 data]# cd /databackup/ 
  2.  
  3. #本质是导出为SQL 
  4. [root@serv01 databackup]# mysqldump -uroot -p123456 --database larrydb 
  5. -- MySQL dump 10.13 Distrib 5.5.29, for Linux (x86_64) 
  6. -- 
  7. -- Host: localhost Database: larrydb 
  8. -- ------------------------------------------------------ 
  9. -- Server version 5.5.29-log 
  10.  
  11. /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */
  12. /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */
  13. /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */
  14. /*!40101 SET NAMES utf8 */
  15. /*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */
  16. /*!40103 SET TIME_ZONE='+00:00' */
  17. /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */
  18. /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */
  19. /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */
  20. /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */
  21.  
  22. -- 
  23. -- Current Database: `larrydb` 
  24. -- 
  25.  
  26. CREATE DATABASE /*!32312 IF NOT EXISTS*/ `larrydb` /*!40100 DEFAULT CHARACTER SET latin1 */
  27.  
  28. USE `larrydb`; 
  29.  
  30. -- 
  31. -- Table structure for table `class
  32. -- 
  33.  
  34. DROP TABLE IF EXISTS `class`; 
  35. /*!40101 SET @saved_cs_client = @@character_set_client */
  36. /*!40101 SET character_set_client = utf8 */
  37. CREATE TABLE `class` ( 
  38. `cid` int(11) DEFAULT NULL, 
  39. `cname` varchar(30) DEFAULT NULL 
  40. ) ENGINE=InnoDB DEFAULT CHARSET=latin1; 
  41. /*!40101 SET character_set_client = @saved_cs_client */
  42.  
  43. -- 
  44. -- Dumping data for table `class
  45. -- 
  46.  
  47. LOCK TABLES `class` WRITE; 
  48. /*!40000 ALTER TABLE `class` DISABLE KEYS */
  49. INSERT INTO `class` VALUES (1,'linux'),(2,'oracle'); 
  50. /*!40000 ALTER TABLE `class` ENABLE KEYS */
  51. UNLOCK TABLES; 
  52.  
  53. -- 
  54. -- Table structure for table `stu` 
  55. -- 
  56.  
  57. DROP TABLE IF EXISTS `stu`; 
  58. /*!40101 SET @saved_cs_client = @@character_set_client */
  59. /*!40101 SET character_set_client = utf8 */
  60. CREATE TABLE `stu` ( 
  61. `sid` int(11) DEFAULT NULL, 
  62. `sname` varchar(30) DEFAULT NULL, 
  63. `cid` int(11) DEFAULT NULL 
  64. ) ENGINE=MyISAM DEFAULT CHARSET=latin1; 
  65. /*!40101 SET character_set_client = @saved_cs_client */
  66.  
  67. -- 
  68. -- Dumping data for table `stu` 
  69. -- 
  70.  
  71. LOCK TABLES `stu` WRITE; 
  72. /*!40000 ALTER TABLE `stu` DISABLE KEYS */
  73. INSERT INTO `stu` VALUES (1,'larry01',1),(2,'larry02',2); 
  74. /*!40000 ALTER TABLE `stu` ENABLE KEYS */
  75. UNLOCK TABLES; 
  76. /*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */
  77.  
  78. /*!40101 SET SQL_MODE=@OLD_SQL_MODE */
  79. /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */
  80. /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */
  81. /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */
  82. /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */
  83. /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */
  84. /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */
  85.  
  86. Dump completed on 2013-09-10 18:56:06 
  87.  
  88. #将输出结果保存到文件中 
  89. [root@serv01 databackup]# mysqldump -uroot -p123456 --database larrydb > larrydb.sql 

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

 

 
  1. [root@serv01 data]# mysql -uroot -p123456 
  2. Welcome to the MySQL monitor. Commands end with ; or /g. 
  3. Your MySQL connection id is 4 
  4. Server version: 5.5.29-log Source distribution 
  5.  
  6. Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved. 
  7.  
  8. Oracle is a registered trademark of Oracle Corporation and/or its 
  9. affiliates. Other names may be trademarks of their respective 
  10. owners. 
  11.  
  12. Type 'help;' or '/h' for help. Type '/c' to clear the current input statement. 
  13.  
  14. mysql> show databases; 
  15. +--------------------+ 
  16. | Database | 
  17. +--------------------+ 
  18. | information_schema | 
  19. | crm | 
  20. | game | 
  21. | hello | 
  22. | larrydb | 
  23. | mnt | 
  24. | mysql | 
  25. | performance_schema | 
  26. | test | 
  27. +--------------------+ 
  28. 9 rows in set (0.00 sec) 
  29.  
  30. mysql> drop database larrydb; 
  31. Query OK, 2 rows affected (0.01 sec) 
  32.  
  33. mysql> show databases; 
  34. +--------------------+ 
  35. | Database | 
  36. +--------------------+ 
  37. | information_schema | 
  38. | crm | 
  39. | game | 
  40. | hello | 
  41. | mnt | 
  42. | mysql | 
  43. | performance_schema | 
  44. | test | 
  45. +--------------------+ 
  46. 8 rows in set (0.00 sec) 
  47.  
  48. mysql> exit 
  49. Bye 

第六步,导入数据

 

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

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

 

 
  1. [root@serv01 data]# mysql -uroot -p123456 
  2. Welcome to the MySQL monitor. Commands end with ; or /g. 
  3. Your MySQL connection id is 6 
  4. Server version: 5.5.29-log Source distribution 
  5.  
  6. Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved. 
  7.  
  8. Oracle is a registered trademark of Oracle Corporation and/or its 
  9. affiliates. Other names may be trademarks of their respective 
  10. owners. 
  11.  
  12. Type 'help;' or '/h' for help. Type '/c' to clear the current input statement. 
  13.  
  14. mysql> show database; 
  15. 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 1 
  16. mysql> show databases; 
  17. +--------------------+ 
  18. | Database | 
  19. +--------------------+ 
  20. | information_schema | 
  21. | crm | 
  22. | game | 
  23. | hello | 
  24. | larrydb | 
  25. | mnt | 
  26. | mysql | 
  27. | performance_schema | 
  28. | test | 
  29. +--------------------+ 
  30. 9 rows in set (0.00 sec) 
  31.  
  32. mysql> use larrydb; 
  33. Database changed 
  34. mysql> select * from class
  35. +------+--------+ 
  36. | cid | cname | 
  37. +------+--------+ 
  38. | 1 | linux | 
  39. | 2 | oracle | 
  40. +------+--------+ 
  41. 2 rows in set (0.00 sec) 
  42.  
  43. mysql> select * from stu; 
  44. +------+---------+------+ 
  45. | sid | sname | cid | 
  46. +------+---------+------+ 
  47. | 1 | larry01 | 1 | 
  48. | 2 | larry02 | 2 | 
  49. +------+---------+------+ 
  50. 2 rows in set (0.00 sec) 

对多个库进行备份

第一步,查看有哪些数据库

 

 
  1. mysql> show databases; 
  2. +--------------------+ 
  3. | Database | 
  4. +--------------------+ 
  5. | information_schema | 
  6. | crm | 
  7. | game | 
  8. | hello | 
  9. | larrydb | 
  10. | mnt | 
  11. | mysql | 
  12. | performance_schema | 
  13. | test | 
  14. +--------------------+ 
  15. 9 rows in set (0.00 sec) 
  16. mysql> use game; 
  17. Database changed 
  18. mysql> show tables; 
  19. +----------------+ 
  20. | Tables_in_game | 
  21. +----------------+ 
  22. | country | 
  23. | fight | 
  24. | hero | 
  25. +----------------+ 
  26. 3 rows in set (0.00 sec) 
  27.  
  28. mysql> select * from country; 
  29. +-----+---------+----------+ 
  30. | cno | cname | location | 
  31. +-----+---------+----------+ 
  32. | 10 | caowei | luoyang | 
  33. | 20 | shuhan | chengdou | 
  34. | 30 | sunwu | nanjing | 
  35. | 40 | houhan | luoyang | 
  36. | 50 | beisong | kaifeng | 
  37. | 60 | 魏国 | 洛阳 | 
  38. +-----+---------+----------+ 
  39. 6 rows in set (0.00 sec) 

第二步,备份多个库

 

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

第三步,模拟数据丢失。

 

 
  1. mysql> drop database game; 
  2. Query OK, 3 rows affected (0.01 sec) 
  3.  
  4. mysql> drop database larrydb; 
  5. Query OK, 2 rows affected (0.00 sec) 
  6. mysql> use crm; 
  7. Database changed 
  8. mysql> show tables; 
  9. +---------------+ 
  10. | Tables_in_crm | 
  11. +---------------+ 
  12. | test | 
  13. +---------------+ 
  14. 1 row in set (0.00 sec) 
  15.  
  16. mysql> select * from test; 
  17. Empty set (0.00 sec) 
  18.  
  19. mysql> drop database crm; 
  20. Query OK, 1 row affected (0.00 sec) 

第四步,恢复数据

 

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

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

 

 
  1. [root@serv01 data]# mysql -uroot -p123456 
  2. Welcome to the MySQL monitor. Commands end with ; or /g. 
  3. Your MySQL connection id is 9 
  4. Server version: 5.5.29-log Source distribution 
  5.  
  6. Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved. 
  7.  
  8. Oracle is a registered trademark of Oracle Corporation and/or its 
  9. affiliates. Other names may be trademarks of their respective 
  10. owners. 
  11.  
  12. Type 'help;' or '/h' for help. Type '/c' to clear the current input statement. 
  13.  
  14. mysql> show databases; 
  15. +--------------------+ 
  16. | Database | 
  17. +--------------------+ 
  18. | information_schema | 
  19. | game | 
  20. | hello | 
  21. | larrydb | 
  22. | mnt | 
  23. | mysql | 
  24. | performance_schema | 
  25. | test | 
  26. +--------------------+ 
  27. 8 rows in set (0.00 sec) 
  28.  
  29. mysql> use game; 
  30. Database changed 
  31. mysql> select * from country; 
  32. +-----+---------+----------+ 
  33. | cno | cname | location | 
  34. +-----+---------+----------+ 
  35. | 10 | caowei | luoyang | 
  36. | 20 | shuhan | chengdou | 
  37. | 30 | sunwu | nanjing | 
  38. | 40 | houhan | luoyang | 
  39. | 50 | beisong | kaifeng | 
  40. | 60 | 魏国 | 洛阳 | 
  41. +-----+---------+----------+ 
  42. 6 rows in set (0.00 sec) 
  43.  
  44. mysql> use larrydb; 
  45. Database changed 
  46. mysql> select * from class
  47. +------+--------+ 
  48. | cid | cname | 
  49. +------+--------+ 
  50. | 1 | linux | 
  51. | 2 | oracle | 
  52. +------+--------+ 
  53. 2 rows in set (0.00 sec) 

备份所有的库

 

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

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

 

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

第二步,模拟数据丢失

 

 
  1. mysql> use game; 
  2. Database changed 
  3. mysql> show tables; 
  4. +----------------+ 
  5. | Tables_in_game | 
  6. +----------------+ 
  7. | country | 
  8. | fight | 
  9. | hero | 
  10. +----------------+ 
  11. 3 rows in set (0.00 sec) 
  12.  
  13. mysql> drop table hero; 
  14. Query OK, 0 rows affected (0.00 sec) 
  15.  
  16. mysql> drop table country; 
  17. Query OK, 0 rows affected (0.00 sec) 

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

 

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

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

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

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表