当前位置: 首页 >> 应用软件 >> 数据库 >> MYSQL 数据备份和灾难恢复
 

MYSQL 数据备份和灾难恢复

作者:      来源:zz     发表时间:2008-02-27     浏览次数:      字号:    

§14.1           备份和恢复数据库

备份的方法:

*使用mysqldump,创建dump file  改文件包含了重建数据库的SQL语句。

* mysqlhotcopy:是个perl脚本,有1000多行,学习perl的时候可以研究一下。直接拷贝指定数据库的目录。

*自己备份数据库。实际是手工执行mysqlhotcopy的操作。你必须关闭数据库或者flush and lock all tablesmysqldump and mysqlhotcopy 会自动 flush and lock的。

*使用BACKUP TABLE and RESTORE TABLE备份或恢复指定表或一系列表。

    备份多少会影响数据库的使用,使用复制可以避免这点。

 

<!--[if !supportLists]-->     <!--[endif]-->使用mysqldump备份恢复数据库。

在系统自带mysql安装的情况下,可能会有多个mysqldump存在,确认版本如下:

版本查看:

mysqldump -V

mysqldump  Ver 10.9 Distrib 4.1.20, for redhat-linux-gnu (x86_64)

 

/usr/local/mysql/bin/mysqldump -V

mysqldump  Ver 10.13 Distrib 5.1.22-ndb-6.3.4-telco, for redhat-linux-gnu (x86_64)

 

备份举例:mysqldump --opt –u username –p password employee > backup.sql

由于俺们的数据没有密码:mysqldump --opt employee > backup.sql

恢复的方法:mysql –u username –p < backup.sql

 

--opt(meaning optimized)包含的选先如下:

–-quick 不经内存,直接写入文件。

--add-drop-table

--add-locks

--extended-insert

--lock-tables

它主要考虑了恢复时的优化,备份时可能会更慢

其他的有用参数如下:--databases, --all-databases, --all-databases, -d or --no-data

 

mysql cluster/usr/local/mysql/bin/mysqldump lsdb --no-tablespaces  > meil.sql

可以不备份table space

<!--[if !supportLists]-->     <!--[endif]-->使用mysqlhotcopy备份恢复数据库。

 

        mysqlhotcopy -u username -p database_name backup_location

       由于使用到perlwindows下一般要安装www.activestate.com/Products/ActivePerl

 

<!--[if !supportLists]-->     <!--[endif]-->手工备份恢复数据库。

lock tables

employee read,

department read,

client read,

assignment read,

employeeSkills read;

flush tables;

以上的代替语句:flush tables with read lock;不要离开会话,否则加锁可能会失效。

 

unlock tables;

<!--[if !supportLists]-->     <!--[endif]-->使用BACKUP TABLE and RESTORE TABLE备份恢复数据库。

 

对于MyISAM表:backup table t1 to 'path/to/backup';windows下面要这样使用:backup table t1 to 'path/to/backup';

备份时会自动read locked。不过如果备份多个表,还是需要先使用LOCK TABLES。恢复:restore table t1 from 'c:/tmp'; RESTORE只在MyISAM中有用。如果表存在,要先使用DROP TABLE

<!--[if !supportLists]-->     <!--[endif]-->Binary Log

mysqlbinlog logfile > updates.sql

建议恢复之前先阅读,以免和现有的数据有冲突。

 

§14.2           检查和修复表

检查表的3种方法:CHECK TABLE, using myisamchk (or isamchk), and using mysqlcheck

修复表使用:REPAIR TABLE or again with myisamchk (or isamchk) or mysqlcheck.

       

不过myisamchk and mysqlcheck只能用于MyISAMIsamchk只能用于ISAMmyisamchk or isamchk不能用于在用的表。使用这些工具的时候最好关闭server,当然也可以使用lockCHECK, REPAIR, and mysqlcheck则表在用时都可以使用。

 

<!--[if !supportLists]-->     <!--[endif]-->使用CHECK and REPAIR检查和修复表

 

比如:check table department;修复:repair table t1;

<!--[if !supportLists]-->     <!--[endif]-->使用myisamchk检查和修复表

<!--[if !supportLists]-->     <!--[endif]-->myisamchk table   The table should be the path to a .MYI file that represents a MyISAM table.

其他暂略。

 

§14.3           小结

Backup

mysqldump creates a dump file of SQL statements.

 

mysqlhotcopy copies the data files to a backup location.

 

BACKUP TABLE copies the data file for a table to a backup location.

 

You can manually back up by locking and flushing the tables and then copying the files.

 

Restoration

Reload dump files from mysqldump.

 

Copy back data files from mysqlhotcopy or a manual backup.

 

Restore from BACKUP TABLE with RESTORE TABLE.

 

Re-execute operations since the backup from the binary log.

 

Checking and Repairing Tables

Check tables with CHECK TABLE, myisamchk, isamchk, or mysqlcheck.

 

Repair tables with REPAIR TABLE, myisamchk, isamchk, or mysqlcheck.

 

Don't use myisamchk while the server is being used.

责任编辑 webmaster

 
 
 
 
 
评论更多>>
 
 
 
发表
 
姓名: QQ:
性别: MSN:
E-mail: 主页:
评分: 1 2 3 4 5
评论内容:
验证码:
  
  • 请遵守《互联网电子公告服务管理规定》及中华人民共和国其他各项有关法律法规。
  • 严禁发表危害国家安全、损害国家利益、破坏民族团结、破坏国家宗教政策、破坏社会稳定、侮辱、诽谤、教唆、淫秽等内容的评论 。
  • 用户需对自己在使用本站服务过程中的行为承担法律责任(直接或间接导致的)。
  • 本站管理员有权保留或删除评论内容。
  • 评论内容只代表网友个人观点,与本网站立场无关。
  •