前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >MySQL备份恢复第一篇(r5笔记第5天)

MySQL备份恢复第一篇(r5笔记第5天)

作者头像
jeanron100
发布2018-03-15 17:09:46
9570
发布2018-03-15 17:09:46
举报

今天学习了下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.

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2015-04-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 杨建荣的学习笔记 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云数据库 SQL Server
腾讯云数据库 SQL Server (TencentDB for SQL Server)是业界最常用的商用数据库之一,对基于 Windows 架构的应用程序具有完美的支持。TencentDB for SQL Server 拥有微软正版授权,可持续为用户提供最新的功能,避免未授权使用软件的风险。具有即开即用、稳定可靠、安全运行、弹性扩缩等特点。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档