用mysqldump和mysqlbinlog的MySQL数据恢复实验(一)

2014-11-24 18:31:14 · 作者: · 浏览: 3

实验文档:过程:建表——备份——模拟宕机(删表)——还原备份——恢复到当前数据。


创建数据库,并用drop语句模拟数据库宕机,通过mysqldump和musqlbinlog来还原和恢复。


mysql> usechannel;


Database changed


mysql> showtables;


+-------------------------+


| Tables_in_channel |


+-------------------------+


| official_channel |


| official_channel_widget |


| personal_channel |


| personal_channel_widget |


| tags |


+-------------------------+


5 rows in set (0.00 sec)


mysql> createtable chanel_following (id int primary key ,uid int not null);


Query OK, 0 rows affected (1.18 sec)


mysql> showtables;


+-------------------------+


| Tables_in_channel |


+-------------------------+


| chanel_following |


| official_channel |


| official_channel_widget |


| personal_channel |


| personal_channel_widget |


| tags |


+-------------------------+


6 rows in set (0.00 sec)


mysql> showcolumns from chanel_following;


+-------+---------+------+-----+---------+-------+


| Field | Type | Null | Key | Default | Extra |


+-------+---------+------+-----+---------+-------+


| id | int(11) | NO | PRI | NULL | |


| uid | int(11) | NO | | NULL | |


+-------+---------+------+-----+---------+-------+


2 rows in set (0.01 sec)


(为还原实验做准备):为输入方便,另开一窗口。


[root@channelme~]# mysqldump -uroot -p -B channel --table chanel_following >chanelfollowing.sql


Enter password:


[root@channelme ~]# 备份成功。


注意:


mysqldump、mysqladmin、mysqlbinlog 等工具是在linux命令行下输入。


如果没有特别写明备份路径,则默认在当前路径下,而不是mysql数据目录下。


查看备份内容:


[root@channelme~]# cat chanelfollowing.sql


-- MySQL dump 10.11


--


-- Host: localhost Database: channel


--------------------------------------------------------


-- Server version 5.5.13-log



/*!40101 SET@OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;


……


/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES,SQL_NOTES=0 */;


--


-- Table structure for table`chanel_following`


--


DROP TABLE IF EXISTS `chanel_following`;


SET @saved_cs_client = @@character_set_client;


SET character_set_client = utf8;


CREATE TABLE `chanel_following` (


`id` int(11) NOT NULL,


`uid` int(11) NOT NULL,


PRIMARY KEY (`id`)


) ENGINE=InnoDB DEFAULT CHARSET=utf8;


SET character_set_client =@saved_cs_client;


--


-- Dumping data for table`chanel_following`


--


LOCK TABLES `chanel_following` WRITE;


/*!40000 ALTER TABLE `chanel_following`DISABLE KEYS */;


/*!40000 ALTER TABLE `chanel_following`ENABLE KEYS */;


UNLOCK TABLES;


/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;


……


/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;


-- Dump completed on 2011-10-10 3:07:39


[root@channelme ~]#


通过备份文档只针对表chanel_following,说明备份正确。


mysql> insertinto chanel_following values(102,100000),(3,69686869),(2132723743,21327237432);


Query OK, 3 rows affected, 1 warning (0.01sec)


Records: 3 Duplicates: 0 Warnings: 1


查看插入是否正确:


mysql> select *from chanel_following;


+------------+------------+


| id | uid |


+------------+------------+


| 3 | 69686869 |


| 102 | 100000 |


| 2132723743 | 2147483647 |


+------------+------------+


rows in set (0.00 sec)


此处我顺便做了个关于int的实验。如果细心,就会发现,我插入的第三个数据与显示的不一样。这是因为int最大能显示为2147483647,我存的超出了它的最大值,就截断了,也并不是少一位为2132723743。因为uid属性我并没设置为非负unsigned,所以不是4294967295。


mysql> droptable chanel_following;


Query OK, 0 rows affected (0.02 sec)


mysql> select *from chanel_following;


Empty set (0.00 sec)


删除成功。


在1.2步,我们把表备份在了chanelfollo