Chinaunix首页 | 论坛 | 博客
  • 博客访问: 19741597
  • 博文数量: 679
  • 博客积分: 10495
  • 博客等级: 上将
  • 技术积分: 9308
  • 用 户 组: 普通用户
  • 注册时间: 2006-07-18 10:51
文章分类

全部博文(679)

文章存档

2012年(5)

2011年(38)

2010年(86)

2009年(145)

2008年(170)

2007年(165)

2006年(89)

分类: Mysql/postgreSQL

2008-02-26 17:40:23

备份和恢复数据库

备份的方法:

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

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

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

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

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

 

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

     使用mysqlhotcopy备份恢复数据库。

 

        mysqlhotcopy -u username -p database_name backup_location

       由于使用到perlwindows下一般要安装

 

     手工备份恢复数据库。

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

 

     使用CHECK and REPAIR检查和修复表

 

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

     使用myisamchk检查和修复表

     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.

 

阅读(7464) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2008-02-26 17:41:22

习题和答案: 1: If you want to back up your database, it is necessary to take down the server lock and flush the tables both a) and b) none of the above 2: You should lock tables manually before executing a manual backup mysqldump mysqlhotcopy none of the above 3: Which table types can you check with CHECK TABLE? InnoDB and MyISAM MyISAM only MyISAM and BDB InnoDB and BDB 4: Which table types can you repair with REPAIR TABLE? InnoDB and MyISAM MyI