保持数据的完整性和一致性(integrity and consistency)是数据库在商务应用中的核心内容,mysql数据库使用innodb引擎来实现事务
保持数据的完整性和一致性(integrity and consistency)是数据库在商务应用中的核心内容,mysql数据库使用innodb引擎来实现事务处理(transaction),因此针对使用 innodb 类型引擎的数据表就需要有有更多检查和限制。而相应地,这也就更容易出现因为数据一致性和完整性而导致无法正常读取表中部分数据甚至全部记录的问题,因此在实际应用之中,您有可能需要比较多地面对如何恢复 innodb 数据表的问题。
本文笔记一次 innodb 数据表恢复的过程。
另外,值得一提的是,从mysql 5.5.5 版本开始,预设引擎已经改为innodb了。
starting from mysql 5.5.5, the default storage engine for new tables is innodb.
(1) get backup files(取得相关备份文件):
data file: /var/lib/mysql/ibdata1
logs file: /var/lib/mysql/ib_logfile0
logs file: /var/lib/mysql/ib_logfile1
database: bizness
table name: transaction
table file: /var/lib/mysql/bizness/transaction.frm
(2) setup temporary mysql service for recover(设置临时数据库以供恢复操作)
create database and innodb table with same name engine(not matter the field name):
mysql> create database bizness;
mysql> create table transaction(a int) engine=innodb;
stop the temporary mysql service, and copy all the backup files to replace the
current databases files.(ibdata1,ib_logfile0,ib_logfile1,transaction.frm)
# cp -p ibdata1 /var/lib/mysql/
# cp -p ib_logfile0 /var/lib/mysql/
# cp -p ib_logfile1 /var/lib/mysql/
# cp -p transaction.frm /var/lib/mysql/bizness/
# chown mysql:mysql /var/lib/mysql/ibdata
# chown mysql:mysql /var/lib/mysql/ib_logfile0
# chown mysql:mysql /var/lib/mysql/ib_logfile1
# chown mysql:mysql /var/lib/mysql/transaction.frm
(3) check the old logfiles size:
# /bin/ls -l -b /var/lib/mysql/ib_logfile*;
------------------------------------------------------------------------------
-rw-rw---- 1 mysql mysql 5242880 9月 2 13:29 ib_logfile0
-rw-rw---- 1 mysql mysql 5242880 7月 30 13:46 ib_logfile1
------------------------------------------------------------------------------
note: the size is 5242880 bytes.
(4) now start up mysql in rescue mode
# /usr/libexec/mysqld --innodb-log-file-size=5242880 --innodb-force-recovery=6
------------------------------------------------------------------------------
innodb: initializing buffer pool, size = 8.0m
innodb: completed initialization of buffer pool
innodb: the user has set srv_force_no_log_redo on
innodb: skipping log redo
innodb: started; log sequence number 0 0
innodb: !!! innodb_force_recovery is set to 6 !!!
[note] event scheduler: loaded 0 events
[note] /usr/libexec/mysqld: ready for connections.
version: '5.1.61' socket: '/var/lib/mysql/mysql.sock' port: 3306 source distribution
------------------------------------------------------------------------------
and then dump the database to sql file with mysqldump command:
# mysqldump -u root -p bizness > bizness.sql
或者:
# mysqldump -u root -p bizness transaction > bizness.transaction.sql
如果成功导出了数据文本,那么就可以它用来进行恢复了,根据您所遇到的实际情况,,
您或许有可能需要现删除已经崩溃的数据表,然后再导入恢复出来的sql文本。例如:
# mysql -u root -p bizness 或者:
# mysql -u root -p bizness
note: mysql importing do not need to indicate the table name .
如有需要,可查看帮助说明:
# /usr/libexec/mysqld --verbose --help
--innodb-log-file-size=#
size of each log file in a log group.
--innodb-force-recovery=#
helps to save your data in case the disk image of the database becomes corrupt.
0 by default (normal startup without forced recovery)
官方网站的参考文档:
1 (srv_force_ignore_corrupt)
即使发现了损坏页也继续让服务运行,这个选项对于备份或者转存当前数据尤为有用。
lets the server run even if it detects a corrupt page. tries to make select * from tbl_name
jump over corrupt index records and pages, which helps in dumping tables.
2 (srv_force_no_background)
防止主线程和任何清除线程运行,如果清除操作会导致服务器挂掉,此选项有助于防止它。
prevents the master thread and any purge threads from running. if a crash
would occur during the purge operation, this recovery value prevents it.
3 (srv_force_no_trx_undo)
恢复后不回滚事务。
does not run transaction rollbacks after crash recovery.
4 (srv_force_no_ibuf_merge)
如果插入到缓冲区的合并操作会导致系统崩溃,那么插入将不会被执行。
prevents insert buffer merge operations. if they would cause a crash,
does not do them. does not calculate table statistics.
5 (srv_force_no_undo_log_scan)
启动数据库时,忽略撤消日志,innodb将未完成事务当作已经完成。
does not look at undo logs when starting the database:
innodb treats even incomplete transactions as committed.
6 (srv_force_no_log_redo)
启动数据库时,忽略与恢复相关的前滚日志。
does not do the redo log roll-forward in connection with recovery.