mysql备份和恢复数据库_备份和恢复你的MySQL数据库

如果你作为DBA不想因为丢失MySQL中的数据而引咎辞职,那么多多少少你要掌握如何备份和恢复你的MySQL数据库。而且,此方法还可以用来将MySQL中的数据迁移到新的服务器上。

1、通过命令行进行备份(使用mysqldump)

备份的语法如下:

$ mysqldump --opt -u [uname] -p[pass] [dbname] > [backupfile.sql]

[uname] 用户名

[pass] 用户密码

[dbname] 需要备份的数据库名

[backupfile.sql] 备份文件名

[--opt] 一些选项,详情可查看MySQL官方文档

例如我想备份整个test1库

我可以使用如下命令:

mysqldump -u root -proot test1 > test1.sql

备份完成后查看备份的文件:

[root@ace ~]# cat test1.sql

-- MySQL dump 10.13 Distrib 5.6.30, for Linux (x86_64)

--

-- Host: localhost Database: test1

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

-- Server version5.6.30

/*!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 `dept`

--

DROP TABLE IF EXISTS `dept`;

/*!40101 SET @saved_cs_client = @@character_set_client */;

/*!40101 SET character_set_client = utf8 */;

CREATE TABLE `dept` (

`deptno` int(5) DEFAULT NULL,

`deptname` varchar(10) DEFAULT NULL

) ENGINE=InnoDB DEFAULT CHARSET=latin1;

/*!40101 SET character_set_client = @saved_cs_client */;

--

-- Dumping data for table `dept`

--

LOCK TABLES `dept` WRITE;

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

INSERT INTO `dept` VALUES (1,'haha'),(2,'heihei'),(3,'hehe');

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

UNLOCK TABLES;

--

-- 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` (

`sal` decimal(10,3) DEFAULT NULL,

`ename1` varchar(10) DEFAULT NULL,

`birth` date DEFAULT NULL,

`hiredate` date DEFAULT NULL,

`deptno` int(2) DEFAULT NULL

) ENGINE=InnoDB DEFAULT CHARSET=latin1;

/*!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 (NULL,'ztp',NULL,NULL,NULL);

/*!40000 ALTER TABLE `emp` 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 2016-05-26 22:24:15

发现备份出来的直接就是文本格式的文件。

但是如果我只想备份test1中的某几张表怎么办呢?

可以使用如下命令:

mysqldump -u root -proot --databases test1 --tables emp > test1_emp.sql

然后我们可以查看备份文件,可以发现其中仅包含emp这一张表。

如果你想备份所有的数据库可用如下命令:

mysqldump -u root -p --all-databases > alldb.sql

还有一些常用的备份选项如下:

--add-drop-table

--no-data

--add-locks

2、对数据进行压缩备份

备份命令如下:

mysqldump -u [uname] -p[pass] [dbname] | gzip -9 > [backupfile.sql.gz]

我们先用普通方式备份test1中的dept表:

mysqldump -uroot -p --databases test1 --tables dept

[root@ace ~]# mysqldump -uroot -p --databases test1 --tables dept > test1_emp.sql

Enter password:

[root@ace ~]# ls -lth *.sql

-rw-r--r-- 1 root root 18M May 26 22:51 test1_emp.sql

-rw-r--r-- 1 root root 1.1M May 26 22:30 alldb.sql

-rw-r--r-- 1 root root 2.5K May 26 22:24 test1.sql

我们可以看到文件大小问18MB

接下来我们用压缩的方式进行备份

[root@ace ~]# mysqldump -uroot -p --databases test1 --tables dept | gzip -9 > test1_emp.sql.gz

Enter password:

[root@ace ~]# ls -lth *sql*

-rw-r--r-- 1 root root  54K May 26 22:53 test1_emp.sql.gz

-rw-r--r-- 1 root root  18M May 26 22:51 test1_emp.sql

-rw-r--r-- 1 root root 1.1M May 26 22:30 alldb.sql

-rw-r--r-- 1 root root 2.5K May 26 22:24 test1.sql

我们可以看到压缩后占用的空间大大减小

3、恢复数据

恢复普通备份:

mysql -u [uname] -p[pass] [db_to_restore] < [backupfile.sql]

恢复压缩备份:

gunzip < [backupfile.sql.gz] | mysql -u [uname] -p[pass] [dbname]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值