首页 > 代码库 > MYSQLDUMP全备只恢复单库或是单表
MYSQLDUMP全备只恢复单库或是单表
一、发现问题
平时使用mysqldump备份时有人喜欢用-A –B参数进行全备,这样备份的时候会简单一点,但是恢复的时候如果直接恢复会把所有库都会恢复,这不是我们想要的结果。
二、分析问题
恢复单库或是单表我们可以通过shell命令从全库备份的SQL文件中截取出我们想要的部分。另外针对单库的恢复MySQL也有一个参数来解决这个问题.
三、解决问题
1、通过MySQL自带的参数恢复单库。
# 全备的数据库 mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | WL_TJ56_DICT | | mysql | | performance_schema | | test | | test01 | | test02 | +--------------------+ #执行全备 [root@TestServer01 ~]# mysqldump -uroot -pterjoy2016 -S /data/mysql/mysql_3306/mysql.sock -A -B --events > /opt/fullbackup.sql Warning: Using a password on the command line interface can be insecure. [root@TestServer01 ~]# ll /opt/fullbackup.sql -rw-r--r--. 1 root root 651037 Dec 20 00:44 /opt/fullbackup.sql #drop数据库 mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | WL_TJ56_DICT | | mysql | | performance_schema | | test | | test01 | | test02 | +--------------------+ 7 rows in set (0.00 sec) mysql> drop database test; Query OK, 2 rows affected (0.07 sec) mysql> drop database test01; Query OK, 1 row affected (0.02 sec) mysql> drop database test02; Query OK, 1 row affected (0.02 sec) mysql> drop database WL_TJ56_DICT; Query OK, 1 row affected (0.00 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | +--------------------+ 3 rows in set (0.00 sec) # 最后恢复test库 #恢复库的时候报错,找不到test库。 [root@TestServer01 ~]# mysql -uroot -pterjoy2016 -S /data/mysql/mysql_3306/mysql.sock --one-database test < /opt/fullbackup.sql Warning: Using a password on the command line interface can be insecure. ERROR 1049 (42000): Unknown database ‘test‘ #这时可以通过全备的SQL文件,找到创建库的语句,创建test库 [root@TestServer01 ~]# grep -i "^create database" /opt/fullbackup.sql CREATE DATABASE /*!32312 IF NOT EXISTS*/ `WL_TJ56_DICT` /*!40100 DEFAULT CHARACTER SET latin1 */; CREATE DATABASE /*!32312 IF NOT EXISTS*/ `mysql` /*!40100 DEFAULT CHARACTER SET utf8 */; CREATE DATABASE /*!32312 IF NOT EXISTS*/ `test` /*!40100 DEFAULT CHARACTER SET utf8 */; CREATE DATABASE /*!32312 IF NOT EXISTS*/ `test01` /*!40100 DEFAULT CHARACTER SET utf8 */; CREATE DATABASE /*!32312 IF NOT EXISTS*/ `test02` /*!40100 DEFAULT CHARACTER SET utf8 */; mysql> CREATE DATABASE /*!32312 IF NOT EXISTS*/ `test` /*!40100 DEFAULT CHARACTER SET utf8 */; Query OK, 1 row affected (0.00 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | +--------------------+ 4 rows in set (0.00 sec) #test库创建好了,再进行恢复。 #恢复成功没有报错 [root@TestServer01 ~]# mysql -uroot -pterjoy2016 -S /data/mysql/mysql_3306/mysql.sock --one-database test < /opt/fullbackup.sql #最后来查看有没有恢复出来数据 #奇怪? 这里怎么其它库也出来了,不是只恢复test库吗? mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | WL_TJ56_DICT | | mysql | | performance_schema | | test | | test01 | +--------------------+ 6 rows in set (0.01 sec) #赶紧查看test有没有数据,这里可以查看test的数据 mysql> use test; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> select * from t; +----+---------+ | id | name | +----+---------+ | 1 | xm | | 2 | xmj | | 3 | xuwu | | 4 | chuzan | | 5 | chuzan2 | | 6 | chuzan3 | +----+---------+ 6 rows in set (0.00 sec) #其它库呢? 这里没有恢复出数据来。在这里我也纠结好久,我明明只恢复test库怎么其它库也恢复出来了呢? 只是其它库没有数据而已。 mysql> use test01 Database changed mysql> show tables; Empty set (0.00 sec) mysql> 至此,单库通过参数恢复到此为止。
2、通过shell命令截取要恢复库的表结构及数据来进行恢复
#首先我drop掉用户的数据库,全备的数据我还是使用之前的备份。 #现在只有mysql自带的库了 mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | +--------------------+ 3 rows in set (0.00 sec) #现在还是来恢复test库,其它库不恢复 #在操作系统执行下面的命令 [root@TestServer01 ~]# cat /opt/fullbackup.sql | > sed -n -e ‘/^CREATE DATABASE.*`test`/,/^CREATE DATABASE/ p‘ | > sed -e ‘$d‘ | > mysql -uroot -pterjoy2016 -S /data/mysql/mysql_3306/mysql.sock #现在来查看test库是否恢复成功 mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | +--------------------+ 4 rows in set (0.00 sec) mysql> use test Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> show tables; +----------------+ | Tables_in_test | +----------------+ | Area | | t | +----------------+ 2 rows in set (0.00 sec) mysql> select * from t; +----+---------+ | id | name | +----+---------+ | 1 | xm | | 2 | xmj | | 3 | xuwu | | 4 | chuzan | | 5 | chuzan2 | | 6 | chuzan3 | +----+---------+ 6 rows in set (0.00 sec) mysql> #从上面结果来看,恢复是成功的。
3、通过shell命令截取要恢复表的表结构及数据来进行恢复
#首先登陆test数据库,删除t表 mysql> use test Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> show tables; +----------------+ | Tables_in_test | +----------------+ | Area | | t | +----------------+ 2 rows in set (0.00 sec) mysql> select * from t; +----+---------+ | id | name | +----+---------+ | 1 | xm | | 2 | xmj | | 3 | xuwu | | 4 | chuzan | | 5 | chuzan2 | | 6 | chuzan3 | +----+---------+ 6 rows in set (0.00 sec) mysql> drop table t; Query OK, 0 rows affected (0.01 sec) mysql> show tables; +----------------+ | Tables_in_test | +----------------+ | Area | +----------------+ 1 row in set (0.00 sec) # 在操作系统使用命令截取出表的结构及数据的SQL。 [root@TestServer01 ~]# cat /opt/fullbackup.sql | sed -n -e ‘/^CREATE DATABASE.*`test`/,/^CREATE DATABASE/ p‘ | sed -e ‘$d‘ | sed -n ‘/-- Table structure for table `t`/,/UNLOCK TABLES;/p‘ > create_t.sql #查看生成的SQL语句。 [root@TestServer01 ~]# cat create_t.sql -- Table structure for table `t` -- DROP TABLE IF EXISTS `t`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `t` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(50) NOT NULL DEFAULT ‘‘, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Dumping data for table `t` -- LOCK TABLES `t` WRITE; /*!40000 ALTER TABLE `t` DISABLE KEYS */; INSERT INTO `t` VALUES (1,‘xm‘),(2,‘xmj‘),(3,‘xuwu‘),(4,‘chuzan‘),(5,‘chuzan2‘),(6,‘chuzan3‘); /*!40000 ALTER TABLE `t` ENABLE KEYS */; UNLOCK TABLES; #重新登录并切换到test库,使用source 命令执行之前生成的SQL语句。 mysql> use test Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A mysql> source create_t.sql Query OK, 0 rows affected, 1 warning (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.09 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected, 1 warning (0.00 sec) Query OK, 6 rows affected (0.00 sec) Records: 6 Duplicates: 0 Warnings: 0 Query OK, 0 rows affected, 1 warning (0.00 sec) Query OK, 0 rows affected (0.00 sec) #查看数据t表恢复成功 mysql> select * from t; +----+---------+ | id | name | +----+---------+ | 1 | xm | | 2 | xmj | | 3 | xuwu | | 4 | chuzan | | 5 | chuzan2 | | 6 | chuzan3 | +----+---------+ 6 rows in set (0.00 sec)
至此单表的恢复成功了。
本文出自 “徐铭江的博客” 博客,请务必保留此出处http://xumingjiang.blog.51cto.com/703960/1884622
MYSQLDUMP全备只恢复单库或是单表
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。