首页 > 数据库 > MySQL > 正文

MySQL备份与恢复之冷备份(1)

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

这篇文章主要介绍了MySQL备份与恢复之冷备,冷备一般需要定制计划,比如什么时候做备份,每次对哪些数据进行备份等等,对冷备感兴趣的小伙伴们可以参考一下

用一句话概括冷备,就是把数据库服务,比如MySQL,Oracle停下来,然后使用拷贝、打包或者压缩命令对数据目录进行备份。如果数据出现异常,则可以通过备份数据恢复。冷备一般需要定制计划,比如什么时候做备份,每次对哪些数据进行备份等等。但是由于这样的备份占用过多的空间,对大数据量的环境下不一定适合,故生产环境很少使用。

冷备示意图

MySQL备份与恢复之冷备份(1)

冷备实验

第一步,创建测试数据库,插入测试数据

 

 
  1. mysql> use larrydb; 
  2. Database changed 
  3. mysql> show tables; 
  4. +-------------------+ 
  5. | Tables_in_larrydb | 
  6. +-------------------+ 
  7. | access | 
  8. +-------------------+ 
  9. 1 row in set (0.00 sec) 
  10.  
  11. mysql> drop table access; 
  12. Query OK, 0 rows affected (0.00 sec) 
  13.  
  14. mysql> clear 
  15. mysql> show tables; 
  16. Empty set (0.00 sec) 
  17.  
  18. mysql>  
  19. mysql> create table class
  20. -> cid int
  21. -> cname varchar(30)); 
  22. Query OK, 0 rows affected (0.01 sec) 
  23.  
  24. mysql> show create table class /G; 
  25. *************************** 1. row *************************** 
  26. Table: class 
  27. Create Table: CREATE TABLE `class` ( 
  28. `cid` int(11) DEFAULT NULL, 
  29. `cname` varchar(30) DEFAULT NULL 
  30. ) ENGINE=InnoDB DEFAULT CHARSET=latin1 
  31. 1 row in set (0.00 sec) 
  32.  
  33. ERROR:  
  34. No query specified 
  35.  
  36. mysql> create table stu( 
  37. -> sid int
  38. -> sname varchar(30), 
  39. -> cid int) engine=myisam; 
  40. Query OK, 0 rows affected (0.00 sec) 
  41.  
  42. mysql> show create table stu /G; 
  43. *************************** 1. row *************************** 
  44. Table: stu 
  45. Create Table: CREATE TABLE `stu` ( 
  46. `sid` int(11) DEFAULT NULL, 
  47. `sname` varchar(30) DEFAULT NULL, 
  48. `cid` int(11) DEFAULT NULL 
  49. ) ENGINE=MyISAM DEFAULT CHARSET=utf8 
  50. 1 row in set (0.00 sec) 
  51.  
  52. ERROR:  
  53. No query specified 
  54.  
  55. mysql> insert into class values(1,'linux'),(2,'oracle'); 
  56. Query OK, 2 rows affected (0.00 sec) 
  57. Records: 2 Duplicates: 0 Warnings: 0 
  58.  
  59. mysql> desc class
  60. +-------+-------------+------+-----+---------+-------+ 
  61. | Field | Type | Null | Key | Default | Extra | 
  62. +-------+-------------+------+-----+---------+-------+ 
  63. | cid | int(11) | YES | | NULL | | 
  64. | cname | varchar(30) | YES | | NULL | | 
  65. +-------+-------------+------+-----+---------+-------+ 
  66. 2 rows in set (0.00 sec) 
  67.  
  68. mysql> desc stu; 
  69. +-------+-------------+------+-----+---------+-------+ 
  70. | Field | Type | Null | Key | Default | Extra | 
  71. +-------+-------------+------+-----+---------+-------+ 
  72. | sid | int(11) | YES | | NULL | | 
  73. | sname | varchar(30) | YES | | NULL | | 
  74. | cid | int(11) | YES | | NULL | | 
  75. +-------+-------------+------+-----+---------+-------+ 
  76. 3 rows in set (0.00 sec) 
  77.  
  78. mysql> insert into stu values(1,'larry01',1),(2,'larry02',2); 
  79. Query OK, 2 rows affected (0.00 sec) 
  80. Records: 2 Duplicates: 0 Warnings: 0 
  81.  
  82. mysql> select * from stu; 
  83. +------+---------+------+ 
  84. | sid | sname | cid | 
  85. +------+---------+------+ 
  86. | 1 | larry01 | 1 | 
  87. | 2 | larry02 | 2 | 
  88. +------+---------+------+ 

第二步,停掉MySQL

 

 
  1. [root@serv01 ~]# /etc/init.d/mysqld stop 
  2. Shutting down MySQL... SUCCESS!  

第三步,创建备份目录,并修改拥有者和所属组

 

 
  1. [root@serv01 ~]# mkdir /databackup 
  2. [root@serv01 ~]# chown mysql.mysql /databackup/ -R 
  3. [root@serv01 ~]# ll /databackup/ -d 
  4. drwxr-xr-x. 2 mysql mysql 4096 Sep 10 17:46 /databackup/ 
  5. [root@serv01 ~]# cd /databackup/ 

第四步,冷备(使用tar命令)

 

  1. [root@serv01 databackup]# tar -cvPzf mysql01.tar.gz  

第五步,测试冷备的数据是否正常,我们删除掉data下的所有数据

 

 
  1. [root@serv01 databackup]# rm -rf /usr/local/mysql/data/* 

第六步,删除所有数据后数据库不能启动

 

 
  1. [root@serv01 databackup]# /etc/init.d/mysqld start 
  2. Starting MySQL.. ERROR! The server quit without updating PID file (/usr/local/mysql/data/serv01.host.com.pid). 

第七步,恢复数据(使用tar命令)

 

 
  1. [root@serv01 databackup]# tar -xvPf mysql01.tar.gz  

第八步,启动MySQL,然后登录MySQL,查看数据是否丢失,如果数据正常代表冷备成功

 

 
  1. [root@serv01 databackup]# /etc/init.d/mysqld start 
  2. Starting MySQL.. SUCCESS!  
  3.  
  4. [root@serv01 ~]# mysql 
  5. Welcome to the MySQL monitor. Commands end with ; or /g. 
  6. Your MySQL connection id is 1 
  7. Server version: 5.5.29-log Source distribution 
  8.  
  9. Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved. 
  10.  
  11. Oracle is a registered trademark of Oracle Corporation and/or its 
  12. affiliates. Other names may be trademarks of their respective 
  13. owners. 
  14.  
  15. Type 'help;' or '/h' for help. Type '/c' to clear the current input statement. 
  16.  
  17. mysql> use larrydb; 
  18. Database changed 
  19. mysql> select * from class
  20. +------+--------+ 
  21. | cid | cname | 
  22. +------+--------+ 
  23. | 1 | linux | 
  24. | 2 | oracle | 
  25. +------+--------+ 
  26. 2 rows in set (0.00 sec) 
  27.  
  28. mysql> select * from stu; 
  29. +------+---------+------+ 
  30. | sid | sname | cid | 
  31. +------+---------+------+ 
  32. | 1 | larry01 | 1 | 
  33. | 2 | larry02 | 2 | 
  34. +------+---------+------+ 
  35. 2 rows in set (0.00 sec) 

以上就是实现MySQL冷备的全部过程,大家对冷备有没有了大概的了解,希望这篇文章可以对大家的学习有所帮助。

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