一、發現問題
平時使用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 -p123456 -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 -p123456 -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 -p123456 -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 -p123456 -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>
#從上面結果來看,恢複是成功的。
#首先登陸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)