MySQL备份恢复第一篇(r5笔记第5天)
今天学习了下MySQL的备份恢复内容,也算是对之前的 数据导入导出的一个细化内容。备份恢复的内容其实还是蛮复杂的,一般网站上提到的备份恢复也基本都是逻辑备份恢复的内容。对于更为高效的备份mysqlbackup和恢复的内容提到的很少,mysqlbackup是需要Licence,需要单独收费的,这也算是向oracle产品线的一个靠拢了。
首先我们还是走走老路,来看看最基本的逻辑备份恢复吧。我们模拟了3万多条的数据。然后尝试恢复回来。
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 count(*)from new_test;
+----------+
| count(*) |
+----------+
| 32046 |
+----------+
1 row in set (0.02 sec)
mysql> select current_timestamp(); --在删除数据之前,我们先来看看时间戳。
+---------------------+
| current_timestamp() |
+---------------------+
| 2015-04-13 14:52:27 |
+---------------------+
1 row in set (0.00 sec)
然后使用经典工具mysqldump来导出 test的数据。
[mysql@oel1 ~]$ mysqldump -u test --databases test > exp_test.log
然后我们直接运行exp_test.log文件
[mysql@oel1 ~]$ mysql -u test < exp_test.log
[mysql@oel1 ~]$
mysql> use information_schema;
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>
mysql> select table_schema,table_name,table_type,engine,create_time from tables where table_schema='test';
+--------------+-------------+------------+--------+---------------------+
| table_schema | table_name | table_type | engine | create_time |
+--------------+-------------+------------+--------+---------------------+
| test | innodb_test | BASE TABLE | InnoDB | 2015-04-13 14:54:09 |
| test | myisam_test | BASE TABLE | MyISAM | 2015-04-13 14:54:09 |
| test | new_test | BASE TABLE | InnoDB | 2015-04-13 14:54:09 |
+--------------+-------------+------------+--------+---------------------+
3 rows in set (0.01 sec)
记录时间戳的目的就是可以看到表new_test的创建时间是在恢复之后,也就是说在恢复的时候也是删除了表,然后重建导入数据。
然后我们再次尝试通过source运行脚本来恢复数据,效果也是一样的。
mysql> source exp_test.log
...
mysql> select count(*)from new_test; --可以看到数据都原原本本的回来了。
+----------+
| count(*) |
+----------+
| 32046 |
+----------+
1 row in set (0.03 sec)
mysql> select current_timestamp();
+---------------------+
| current_timestamp() |
+---------------------+
| 2015-04-13 14:56:32 |
+---------------------+
1 row in set (0.00 sec)
还可以通过如下的方式来导出表结构和表数据,然后分别处理。下面两种方式都是等价的。
mysqldump -u test -T /u02/mysql/dump test
mysqldump -u test --tab=/u02/mysql/dump test
运行后生成的文件结构如下:.txt是数据内容,.sql是表结构语句。
[mysql@oel1 dump]$ ll
total 948
-rw-r--r-- 1 mysql dba 1349 Apr 13 16:02 innodb_test.sql
-rw-rw-rw- 1 mysql dba 2 Apr 13 16:02 innodb_test.txt
-rw-r--r-- 1 mysql dba 1349 Apr 13 16:02 myisam_test.sql
-rw-rw-rw- 1 mysql dba 2 Apr 13 16:02 myisam_test.txt
-rw-r--r-- 1 mysql dba 1380 Apr 13 16:02 new_test.sql
-rw-rw-rw- 1 mysql dba 943641 Apr 13 16:02 new_test.txt
导入数据可以使用下面的两种方式来实现。
[mysql@oel1 dump]$ mysqlimport -u test test /u02/mysql/dump/new_test.txt
test.new_test: Records: 32046 Deleted: 0 Skipped: 0 Warnings: 0
mysql> load data infile '/u02/mysql/dump/new_test.txt' into table new_test;
Query OK, 32046 rows affected (0.23 sec)
Records: 32046 Deleted: 0 Skipped: 0 Warnings: 0
mysql> select count(*)from new_test; --再次导入数据,数据量就翻倍了。
+----------+
| count(*) |
+----------+
| 64092 |
+----------+
1 row in set (0.05 sec)
一些细节的补充。
下面两种方式还是存在着较大的差距。我们来看看差别到底都在哪里。标黄部分就是差别所在。
mysqldump -u test test > b.sql
mysqldump -u test --databases test > a.sql
> sdiff a.sql b.sql
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
-- Current Database: `test` <
-- <
CREATE DATABASE /*!32312 IF NOT EXISTS*/ `test` /*!40100 DEFA <
USE `test`; <
-- <
-- Table structure for table `innodb_test` -- Table structure for table `innodb_test`
在实际工作中,基本上我们不会导个数据重建个数据,所以第二种方式还是推荐的,不建议使用--databases选项。
mysqlimport导入数据,不能导入ddl语句
[mysql@oel1 dump]$ mysqlimport -u test test '/u02/mysql/dump/innodb_test.sql'
mysqlimport: Error: 1146, Table 'test.innodb_test' doesn't exist, when using table: innodb_test
-----导出存储程序
对于存储程序来说,可以使用下面的选项来选择性的导出,也可以忽略。
--events: Dump Event Scheduler events
--routines: Dump stored procedures and functions
--triggers: Dump triggers for tables
--skip-events, --skip-routines, or --skip-triggers.
只导出表结构
mysqldump -u test --no-data test > dump-defs.sql
只导出表中数据
mysqldump -u test --no-create-info test > dump-data.sql
导出数据中包含存储程序
mysqldump -u test --no-data --routines --events test > dump-defs.sql
以上基本就是逻辑备份的内容了,说起高效的备份,mysqlbackup性能提升还是很大的。不过企业版本的加强版本,还是需要付费的。下载的时候其实也是很方便的。
通过这个链接 http://www.mysql.com/downloads/ 选择enterprise backup对应的安装包即可
至于说安装,其实就是个插件一样,完全不需要安装,解压的事情,解压完配置一下环境变量就好了。
[mysql@oel1 ~]$ ll *.zip
-rw-r--r-- 1 mysql dba 4141359 Apr 13 17:37 V59683-01.zip
[mysql@oel1 ~]$ unzip V59683-01.zip
Archive: V59683-01.zip
extracting: meb-3.11.1-linux-glibc2.5-x86-32bit.tar.gz
extracting: meb-3.11.1-linux-glibc2.5-x86-32bit.tar.gz.asc
extracting: meb-3.11.1-linux-glibc2.5-x86-32bit.tar.gz.md5
extracting: README.txt
[mysql@oel1 ~]$ gunzip meb-3.11.1-linux-glibc2.5-x86-32bit.tar.gz
[mysql@oel1 ~]$ tar -xvf *.tar
meb-3.11.1-linux-glibc2.5-x86-32bit/
meb-3.11.1-linux-glibc2.5-x86-32bit/mvl.css
meb-3.11.1-linux-glibc2.5-x86-32bit/bin/
meb-3.11.1-linux-glibc2.5-x86-32bit/bin/mysqlbackup
meb-3.11.1-linux-glibc2.5-x86-32bit/README.txt
meb-3.11.1-linux-glibc2.5-x86-32bit/LICENSE.mysql
meb-3.11.1-linux-glibc2.5-x86-32bit/manual.html
[mysql@oel1 ~]$
解压好以后,直接配置.bash_profile把mysqlbackup配置到里面就大功告成了。
[mysql@oel1 mysql]$ mysqlbackup --version
MySQL Enterprise Backup version 3.11.1 Linux-2.6.18-274.el5-i686 [2014/11/04]
Copyright (c) 2003, 2014, Oracle and/or its affiliates. All Rights Reserved.
Run mysqlbackup --help for help information.
- 数据库安全·内容版本控制,撰改留痕
- 数据库安全·为数据安全而分库
- 数据库安全·时间一致性
- 帝国cms栏目别名如何调用?
- 数据库安全·保护表字段
- 【实践】伪造名人的脸—做一个小示例了解生成式对抗网络
- Spring 常见问题与解决方法
- 如何通过css控制内容显示顺序 第二行的内容优先显示
- 杨廷琨 - 用SQL解析神奇的扑克牌魔术
- Spring boot with Spring security
- Spring RestFul and RestTemplate
- Spring boot with Velocity template
- Oracle 11g R2 RAC 高可用连接特性 – SCAN 详解
- Spring boot · 链接池配置
- MySQL 教程
- MySQL 安装
- MySQL 管理与配置
- MySQL PHP 语法
- MySQL 连接
- MySQL 创建数据库
- MySQL 删除数据库
- MySQL 选择数据库
- MySQL 数据类型
- MySQL 创建数据表
- MySQL 删除数据表
- MySQL 插入数据
- MySQL 查询数据
- MySQL where 子句
- MySQL UPDATE 查询
- MySQL DELETE 语句
- MySQL LIKE 子句
- mysql order by
- Mysql Join的使用
- MySQL NULL 值处理
- MySQL 正则表达式
- MySQL 事务
- MySQL ALTER命令
- MySQL 索引
- MySQL 临时表
- MySQL 复制表
- 查看MySQL 元数据
- MySQL 序列 AUTO_INCREMENT
- MySQL 处理重复数据
- MySQL 及 SQL 注入
- MySQL 导出数据
- MySQL 导入数据
- MYSQL 函数大全
- MySQL Group By 实例讲解
- MySQL Max()函数实例讲解
- mysql count函数实例
- MYSQL UNION和UNION ALL实例
- MySQL IN 用法
- MySQL between and 实例讲解
- Java byte数组操纵方式代码实例解析
- php桥接模式应用案例分析
- PHP设计模式之中介者模式(Mediator Pattern)入门与应用案例详解
- laravel 出现command not found问题的解决方案
- PHP怎么搭建百度Ueditor富文本编辑器
- 使用composer命令加载vendor中的第三方类库 的方法
- PHP批斗大会之缺失的异常详解
- Laravel 6.2 中添加了可调用容器对象的方法
- php实现微信企业转账功能
- 在 Laravel 6 中缓存数据库查询结果的方法
- PHP操作XML中XPath的应用示例
- Laravel手动返回错误码示例
- laravel添加前台跳转成功页面示例
- PHP设计模式之装饰器(装饰者)模式(Decorator)入门与应用详解
- thinkPHP利用ajax异步上传图片并显示、删除的示例