如何使用mysqldump对mysql进行备份和恢复
这篇文章给大家分享的是有关如何使用mysqldump对mysql进行备份和恢复的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
mysqldump是mysql的逻辑备份恢复工具,可以只备份某个表,某个库或者整个数据库。为了保证数据的一致性,备份的时候会加写锁,所以一定要在系统闲的时候用。
语法:
[root@D2-LZY245 ~]# mysqldump
Usage: mysqldump [OPTIONS] database [tables]
OR mysqldump [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...]
OR mysqldump [OPTIONS] --all-databases [OPTIONS]
For more options, use mysqldump -help
备份test1库:
[root@D2-LZY245 ~]# mysqldump -uroot -p123456 test1 > test1_bk.sql
查看生成的备份文件:
[root@D2-LZY245 ~]# mysqldump -uroot -p123456 test1 > test1_bk.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@D2-LZY245 ~]# cat test1_bk.sql
-- MySQL dump 10.13 Distrib 5.7.15, for linux-glibc2.5 (x86_64)
--
-- Host: localhost Database: test1
-- ------------------------------------------------------
-- Server version 5.7.15
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
--
-- Table structure for table `emp`
--
DROP TABLE IF EXISTS `emp`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `emp` (
`employee_id` int(10) DEFAULT NULL,
`first_name` varchar(50) DEFAULT NULL,
`last_name` varchar(50) DEFAULT NULL,
`salary` decimal(10,2) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `emp`
--
LOCK TABLES `emp` WRITE;
/*!40000 ALTER TABLE `emp` DISABLE KEYS */;
INSERT INTO `emp` VALUES (100,'Steven','King',24000.00),(101,'Neena','Kochhar',17000.00),(102,'Lex','De Haan',17000.00),(103,'Alexander','Hunold',9000.00),(104,'Bruce','Ernst',6000.00),(105,'David','Austin',4800.00),(106,'Valli','Pataballa',4800.00),(107,'Diana','Lorentz',4200.00),(108,'Nancy','Greenberg',12008.00),(109,'Daniel','Faviet',9000.00),(110,'John','Chen',8200.00);
/*!40000 ALTER TABLE `emp` ENABLE KEYS */;
UNLOCK TABLES;
--
-- Table structure for table `emp1`
--
DROP TABLE IF EXISTS `emp1`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `emp1` (
`employee_id` int(10) DEFAULT NULL,
`first_name` varchar(50) DEFAULT NULL,
`last_name` varchar(50) DEFAULT NULL,
`salary` decimal(10,2) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `emp1`
--
LOCK TABLES `emp1` WRITE;
/*!40000 ALTER TABLE `emp1` DISABLE KEYS */;
INSERT INTO `emp1` VALUES (100,'Steven','King',24000.00),(101,'Neena','Kochhar',17000.00),(102,'Lex','De Haan',17000.00),(103,'Alexander','Hunold',9000.00),(104,'Bruce','Ernst',6000.00),(105,'David','Austin',4800.00),(106,'Valli','Pataballa',4800.00),(107,'Diana','Lorentz',4200.00),(108,'Nancy','Greenberg',12008.00),(109,'Daniel','Faviet',9000.00),(110,'John','Chen',8200.00);
/*!40000 ALTER TABLE `emp1` ENABLE KEYS */;
UNLOCK TABLES;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
-- Dump completed on 2017-05-26 18:32:51
备份文件实际上就是SQL脚本,先是设置参数,如果表存在则删除表,然后创建表,锁表,执行插入语句,解锁。恢复的时候执行这个SQL脚本就好了。执行脚本有2种方式,一种是重定向,一种是使用source。
重定向方式:
mysql> create database test2;
Query OK, 1 row affected (0.00 sec)
#创建一个数据库test2,恢复到test2。
[root@D2-LZY245 ~]# mysql -uroot -p123456 test2 < test1_bk.sql
查看确认:
mysql> use test2
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_test2 |
+-----------------+
| emp |
| emp1 |
+-----------------+
2 rows in set (0.00 sec)
source方式:
mysql> create database test3;
Query OK, 1 row affected (0.00 sec)
mysql> use test3;
Database changed
mysql> source /root/test1_bk.sql
mysql> show tables;
+-----------------+
| Tables_in_test3 |
+-----------------+
| emp |
| emp1 |
+-----------------+
2 rows in set (0.00 sec)
这两种方式是有区别的,使用重定向方式,如果有错误会直接退出,不会执行后续的语句,而使用source方式则会跳过错误的地方,继续执行后续的语句。
感谢各位的阅读!关于"如何使用mysqldump对mysql进行备份和恢复"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!