mysql 灾难备份_MYSQL 教程:§14, 备份和灾难恢复

§14.1备份和恢复数据库

备份的方法:

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

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

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

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

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

*使用mysqldump备份恢复数据库。

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

版本查看:

mysqldump -V

mysqldumpVer 10.9 Distrib 4.1.20,

for redhat-linux-gnu (x86_64)

/usr/local/mysql/bin/mysqldump

-V

mysqldumpVer 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,-dor--no-data

在mysql cluster中/usr/local/mysql/bin/mysqldump

lsdb --no-tablespaces> meil.sql

可以不备份table space

*使用mysqlhotcopy备份恢复数据库。

mysqlhotcopy -u username -p database_name backup_location

*手工备份恢复数据库。

lock tables

employee read,

department read,

client read,

assignment read,

employeeSkills

read;

flush tables;

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

unlock tables;

*使用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。

*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只能用于MyISAM。Isamchk只能用于ISAM。myisamchk or isamchk不能用于在用的表。使用这些工具的时候最好关闭server,当然也可以使用lock。CHECK, REPAIR, and mysqlcheck则表在用时都可以使用。

*使用CHECK and REPAIR检查和修复表

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

*使用myisamchk检查和修复表

*myisamchk tableThe 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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值