search
HomeDatabaseMysql TutorialMySQL数据库InnoDB引擎数据表的恢复示例

保持数据的完整性和一致性(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.

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What Are the Limitations of Using Views in MySQL?What Are the Limitations of Using Views in MySQL?May 14, 2025 am 12:10 AM

MySQLviewshavelimitations:1)Theydon'tsupportallSQLoperations,restrictingdatamanipulationthroughviewswithjoinsorsubqueries.2)Theycanimpactperformance,especiallywithcomplexqueriesorlargedatasets.3)Viewsdon'tstoredata,potentiallyleadingtooutdatedinforma

Securing Your MySQL Database: Adding Users and Granting PrivilegesSecuring Your MySQL Database: Adding Users and Granting PrivilegesMay 14, 2025 am 12:09 AM

ProperusermanagementinMySQLiscrucialforenhancingsecurityandensuringefficientdatabaseoperation.1)UseCREATEUSERtoaddusers,specifyingconnectionsourcewith@'localhost'or@'%'.2)GrantspecificprivilegeswithGRANT,usingleastprivilegeprincipletominimizerisks.3)

What Factors Influence the Number of Triggers I Can Use in MySQL?What Factors Influence the Number of Triggers I Can Use in MySQL?May 14, 2025 am 12:08 AM

MySQLdoesn'timposeahardlimitontriggers,butpracticalfactorsdeterminetheireffectiveuse:1)Serverconfigurationimpactstriggermanagement;2)Complextriggersincreasesystemload;3)Largertablesslowtriggerperformance;4)Highconcurrencycancausetriggercontention;5)M

MySQL: Is it safe to store BLOB?MySQL: Is it safe to store BLOB?May 14, 2025 am 12:07 AM

Yes,it'ssafetostoreBLOBdatainMySQL,butconsiderthesefactors:1)StorageSpace:BLOBscanconsumesignificantspace,potentiallyincreasingcostsandslowingperformance.2)Performance:LargerrowsizesduetoBLOBsmayslowdownqueries.3)BackupandRecovery:Theseprocessescanbe

MySQL: Adding a user through a PHP web interfaceMySQL: Adding a user through a PHP web interfaceMay 14, 2025 am 12:04 AM

Adding MySQL users through the PHP web interface can use MySQLi extensions. The steps are as follows: 1. Connect to the MySQL database and use the MySQLi extension. 2. Create a user, use the CREATEUSER statement, and use the PASSWORD() function to encrypt the password. 3. Prevent SQL injection and use the mysqli_real_escape_string() function to process user input. 4. Assign permissions to new users and use the GRANT statement.

MySQL: BLOB and other no-sql storage, what are the differences?MySQL: BLOB and other no-sql storage, what are the differences?May 13, 2025 am 12:14 AM

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

MySQL Add User: Syntax, Options, and Security Best PracticesMySQL Add User: Syntax, Options, and Security Best PracticesMay 13, 2025 am 12:12 AM

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

MySQL: How to avoid String Data Types common mistakes?MySQL: How to avoid String Data Types common mistakes?May 13, 2025 am 12:09 AM

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools