MySQL数据库崩溃修复案例,再也⽆需删库跑路!
前⾔
互联⽹⾏业是个⾼危⾏业,动不动就删库跑路!⼏天前⼀朋友在测试服务器上执⾏⼀条错误的命令,导致MySQL数据库崩溃,纠结了好⼏天也没解决问题。深⼊研究MySQL源
码!从根源上出了MySQL崩溃原因。
问题描述
研究MySQL源代码,调试并压测MySQL源代码时,MySQL崩溃!问题是崩溃!⽽且还损坏了InnoDB⽂件!还好是在调试环境下发⽣的,赶紧看看如何解决这个问题,经过⼀
系列的查阅资料、验证、对⽐、MySQL源码调试跟踪、修复损坏的InnoDB⽂件、总结等流程,整理成此⽂,如果以后真的发⽣在线上的⽣产坏境,也不⽤担⼼是不是要跑路的
问题,可以分分钟搞定MySQL的崩溃问题!查看错误⽇志,如下:
-----------------------------------------
161108 23:36:45 mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/var2020-05-01 23:36:46 0
[Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).2020-05-
01 23:36:46 5497
[Note] Plugin 'FEDERATED' is disabled.2020-05-01 23:36:46
7f11c48e1720 InnoDB: Warning: Using innodb_additional_mem_pool_size is DEPRECATED. This option may be
removed in future releases, together with the option innodb_use_sys_malloc and with the InnoDB's
internal memory allocator.2020-05-01 23:36:46 5497
[Note] InnoDB: Using atomics to ref count buffer pool pages2020-05-01 23:36:46 5497
[Note] InnoDB: The InnoDB memory heap is disabled2020-05-01 23:36:46 5497
[Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins2020-05-01 23:36:46 5497
[Note] InnoDB: Memory barrier is not used2020-05-01 23:36:46 5497
[Note] InnoDB: Compressed tables use zlib 1.2.32020-05-01 23:36:46 5497
[Note] InnoDB: Using CPU crc32 instructions2020-05-01 23:36:46 5497
[Note] InnoDB: Initializing buffer pool, size = 16.0M2020-05-01 23:36:46 5497
[Note] InnoDB: Completed initialization of buffer poolInnoDB: Database page corruption on disk or a failedInnoDB:
file read of page 5.InnoDB: You may have to recover from a backup.2020-05-01 23:36:46 7f11c48e1720
InnoDB: Page dump in ascii and hex (16384 bytes): len 16384; hex
7478d078000000050000000000000000000000000f271f4d000700000000000000000000000000000000001b4000000000000
000000200f20000000000000006000000000000002d000000000000002e000000000000002f0000000000000030000000000
省略很多类似代码)InnoDB: End of page dump2020-05-01 23:36:46 7f11c48e1720 InnoDB: uncompressed page,
stored checksum in field1 1954074744, calculated checksums for field1: crc32 993334256, innodb
2046145943, none 3735928559, stored checksum in field2 1139795846, calculated checksums for field2:
crc32 993334256, innodb 1606613742, none 3735928559, page LSN 0 254222157, low 4 bytes of LSN at page end 254221236, page number (if stored to page already) 5,
space id (if created with >= MySQL-4.1.1
and stored already) 0InnoDB: Page may be a transaction system pageInnoDB: Database page corruption on disk or a failedInnoDB: file read of page 5.InnoDB: You may have
to recover from a backup.InnoDB: It
is also possible that your operatingInnoDB: system has corrupted its own file cacheInnoDB: and
rebooting your computer removes theInnoDB: error.InnoDB: If the corrupt page is an index pageInnoDB:
you can also try to fix the corruptionInnoDB: by dumping, dropping, and reimportingInnoDB: the
corrupt table. You can use CHECKInnoDB: TABLE to scan your table for corruption.InnoDB: See also
sql/doc/refman/5.6/en/forcing-innodb-recovery.htmlInnoDB: about forcing
recovery.InnoDB: Ending processing because of a corrupt database page.2020-05-01 23:36:46
7f11c48e1720 InnoDB: Assertion failure in thread 139714288817952 in line 4201InnoDB: We intentionally generate a memory trap.InnoDB: Submit a detailed
bug report to
sql.InnoDB: If you get repeated assertion failures or crashes, evenInnoDB:
immediately after the mysqld startup, there may beInnoDB: corruption in the InnoDB tablespace. Please refer toInnoDB: sql/doc/refman/5.6/en/forcing-innodb-
recovery.htmlInnoDB: about
forcing recovery.03:36:46 UTC - mysqld got signal 6 ;This could be because you hit a bug. It is also possible that this binaryor one of the libraries it was linked against is corrupt,
improperly
built,or misconfigured. This error can also be caused by malfunctioning hardware.We will try our best
to scrape up some info that will hopefully helpdiagnose the problem, but since we have already
crashed,something is definitely wrong and this may
fail.key_buffer_size=16777216read_buffer_size=262144max_used_connections=0max_threads=1000thread_coun
t=0connection_count=0It is possible that mysqld could use up tokey_buffer_size + (read_buffer_size +
sort_buffer_size)*max_threads = 798063 K bytes of memoryHope that's ok; if not, decrease some
variables in the equation.Thread pointer: 0x0Attempting backtrace. You can use the following
information to find outwhere mysqld died. If you see no messages after this, something wentterribly
[0x8e64b5]/usr/local/mysql/bin/mysqld(handle_fatal_signal+0x41b)
[0x652fbb]/lib64/libpthread.so.0(+0xf7e0)[0x7f11c44c77e0]/lib64/libc.so.6(gsignal+0x35)
[0x7f11c315d625]/lib64/libc.so.6(abort+0x175)
[0x7f11c315ee05]/usr/local/mysql/bin/mysqld[0xa585c5]/usr/local/mysql/bin/mysqld[0xa6c7b4]/usr/local/
mysql/bin/mysqld[0xa6cbc7]/usr/local/mysql/bin/mysqld[0xa5bce2]/usr/local/mysql/bin/mysqld[0xa1e2ba]/usr/local/mysql/bin/mysqld[0xa0bf60]/usr/local/mysql/bin/mysqld[0x95a427]/usr/local/mysql/bin/ [0x58f788]/usr/local/mysql/bin/mysqld[0x6e4a36]/usr/local/mysql/bin/mysqld(_Z11plugin_initPiPPci+0xb3e)
[0x6e826e]/usr/local/mysql/bin/mysqld[0x582d85]/usr/local/mysql/bin/mysqld(_Z11mysqld_mainiPPc+0x4d8)
[0x587d18]/lib64/libc.so.6(__libc_start_main+0xfd)
[0x7f11c3149d5d]/usr/local/mysql/bin/mysqld[0x57a019]The manual page at
sql/doc/mysql/en/crashing.html containsinformation that should help you find out
what is causing the crash.161108 23:36:46 mysqld_safe mysqld from pid file
/usr/local/mysql/var/VM_241_49_centos.pid
ended
------------------------------------------------------------------------------
问题分析
从⽇志中可以看出是InnoDB引擎出了问题。⽇志⾥提⽰到sql/doc/refman/5.6/en/forcing-innodb-recovery.html查看强制恢复的⽅法。在MySQL配置⽂件myf
⾥到[mysqld]字段下,添加innodb_force_recovery=1:
[mysqld]
innodb_force_recovery = 1
如果innodb_force_recovery = 1不⽣效,则可尝试2—6⼏个数字
重启MySQL,重启成功。然后使⽤mysqldump或pma导出数据,执⾏修复操作等。修复完成后,把该参数注释掉,还原默认值0。
配置⽂件的参数:innodb_force_recovery
innodb_force_recovery影响整个InnoDB存储引擎的恢复状况。默认为0,表⽰当需要恢复时执⾏所有的恢复操作(即校验数据页/purge undo/insert buffer merge/rolling
back&forward),当不能进⾏有效的恢复操作时,MySQL有可能⽆法启动,并记录错误⽇志;
innodb_force_recovery可以设置为1-6,⼤的数字包含前⾯所有数字的影响。当设置参数值⼤于0后,可以对表进⾏select、create、drop操作,但insert、update、delete这类操作是不允许的。
(SRV_FORCE_IGNORE_CORRUPT):忽略检查到的corrupt页。
(SRV_FORCE_NO_BACKGROUND):阻⽌主线程的运⾏,如主线程需要执⾏full purge操作,会导致crash。
(SRV_FORCE_NO_TRX_UNDO):不执⾏事务回滚操作。
(SRV_FORCE_NO_IBUF_MERGE):不执⾏插⼊缓冲的合并操作。
(SRV_FORCE_NO_UNDO_LOG_SCAN):不查看重做⽇志,InnoDB存储引擎会将未提交的事务视为已提交。
(SRV_FORCE_NO_LOG_REDO):不执⾏前滚的操作。
解决⽅案:
1)
建⽴⼀张新表:
create table demo_bak #和原表结构⼀样,只是把INNODB改成了MYISAM。
把数据导进去
insert into demo_bak select * from demo;
删除掉原表:
drop table demo;
注释掉innodb_force_recovery之后,重启。
重命名:
rename table demo_bak to demo;
最后改回存储引擎:
alter table demo engine = innodb
2)
使⽤mysqldump将表格导出,然后再导回到InnoDB表中。这两种⽅法的结果是相同的。
备份导出(包括结构和数据):
mysqldump -uroot -p123 test > test.sql
还原⽅法1:
use test;source test.sql
还原⽅法2(系统命令⾏):
mysql -uroot -p123 test < test.sql;
注意:CHECK TABLE命令在InnoDB数据库中基本上是没有⽤的。
3)
1、配置myf
配置innodb_force_recovery = 1或2—6⼏个数字,重启MySQL
2、导出数据脚本
mysqldump -uroot -p123 test > test.sql
导出SQL脚本。或者⽤Navicat将所有数据库/表导⼊到其他服务器的数据库中。
注意:数据⼀定要备份成功。然后删除原数据库中的数据。
mysql需要安装documentation3、删除ib_logfile0、ib_logfile1、ibdata1
备份MySQL数据⽬录下的ib_logfile0、ib_logfile1、ibdata1三个⽂件,然后将这三个⽂件删除
4、配置myf
将myf中innodb_force_recovery = 1或2—6⼏个数字这⾏配置删除或者配置为innodb_force_recovery = 0,重启MySQL服务
5、将数据导⼊MySQL数据库
mysql -uroot -p123 test < test.sql; 或⽤Navicat将备份的数据导⼊到数据库中。
注意:
ib_logfile0、ib_logfile1、ibdata1三个⽂件⼀定要先备份后删除;
⼀定要确认原数据导出成功
当数据导出成功后,删除原数据库中的数据时,如果提⽰不能删除,可在命令⾏进⼊MySQL的数据⽬录,⼿动删除相关数据库的⽂件夹或者数据库⽂件夹下的数据表⽂件,前提是数据⼀定导出或备份成功。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。