#执行全备
[iyunv@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.
[iyunv@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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)
# 最后恢复test库
#恢复库的时候报错,找不到test库。
[iyunv@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库
[iyunv@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库创建好了,再进行恢复。
#恢复成功没有报错
[iyunv@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
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)
#现在还是来恢复test库,其它库不恢复
#在操作系统执行下面的命令
[iyunv@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)
#首先登陆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)
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