因断电导致的innodb错误导致MySQL服务器无法启动解决

事情是这样的,拿着公司配的MacBook Pro,想找个闲置的显示器外接缓解下颈椎压力,于是拿了个雷电转接器,刚插上,整个系统就完全失去了响应,触摸板和键盘不管怎么动就是没动静,只能强制关机,但是重启后就出现问题了——MySQL启动不了了。

为了方便,我用的是MAMP Pro,MySQL也是其中自带的,重启之后,想打开PHP+Apache+MySQL,其余两个都没问题,就MySQL启动不了,这是查看日志找到的相关信息:

2018-07-14 09:16:05 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-07-14 09:16:05 0 [Note] --secure-file-priv is set to NULL. Operations related to importing and exporting data are disabled
2018-07-14 09:16:05 0 [Note] /Applications/MAMP/Library/bin/mysqld (mysqld 5.6.35) starting as process 5909 ...
2018-07-14 09:16:05 5909 [Note] Plugin 'FEDERATED' is disabled.
2018-07-14 09:16:05 5909 [Note] InnoDB: Using atomics to ref count buffer pool pages
2018-07-14 09:16:05 5909 [Note] InnoDB: The InnoDB memory heap is disabled
2018-07-14 09:16:05 5909 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2018-07-14 09:16:05 5909 [Note] InnoDB: Memory barrier is not used
2018-07-14 09:16:05 5909 [Note] InnoDB: Compressed tables use zlib 1.2.8
2018-07-14 09:16:05 5909 [Note] InnoDB: Using CPU crc32 instructions
2018-07-14 09:16:05 5909 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2018-07-14 09:16:05 5909 [Note] InnoDB: Completed initialization of buffer pool
2018-07-14 09:16:05 5909 [Note] InnoDB: Highest supported file format is Barracuda.
2018-07-14 09:16:05 5909 [Note] InnoDB: The log sequence numbers 4703124 and 4703124 in ibdata files do not match the log sequence number 7556703 in the ib_logfiles!
2018-07-14 09:16:05 5909 [Note] InnoDB: Database was not shutdown normally!
2018-07-14 09:16:05 5909 [Note] InnoDB: Starting crash recovery.
2018-07-14 09:16:05 5909 [Note] InnoDB: Reading tablespace information from the .ibd files...
2018-07-14 09:16:05 5909 [ERROR] InnoDB: Attempted to open a previously opened tablespace. Previous tablespace mysql/innodb_index_stats uses space ID: 2 at filepath: ./mysql/innodb_index_stats.ibd. Cannot open tablespace myapi/users which uses space ID: 2 at filepath: ./myapi/users.ibd
2018-07-14 09:16:05 7fff91104380  InnoDB: Operating system error number 2 in a file operation.
InnoDB: The error means the system cannot find the path specified.
InnoDB: If you are installing InnoDB, remember that you must create
InnoDB: directories yourself, InnoDB does not create them.
InnoDB: Error: could not open single-table tablespace file ./myapi/users.ibd
InnoDB: We do not continue the crash recovery, because the table may become
InnoDB: corrupt if we cannot apply the log records in the InnoDB log to it.
InnoDB: To fix the problem and start mysqld:
InnoDB: 1) If there is a permission problem in the file and mysqld cannot
InnoDB: open the file, you should modify the permissions.
InnoDB: 2) If the table is not needed, or you can restore it from a backup,
InnoDB: then you can remove the .ibd file, and InnoDB will do a normal
InnoDB: crash recovery and ignore that table.
InnoDB: 3) If the file system or the disk is broken, and you cannot remove
InnoDB: the .ibd file, you can set innodb_force_recovery > 0 in my.cnf
InnoDB: and force InnoDB to continue crash recovery here.
180714 09:16:05 mysqld_safe mysqld from pid file /Applications/MAMP/tmp/mysql/mysql.pid ended

拿着报错信息搜了下,貌似是说某个表表空间id号(1)抢占了mysql/innodb_table_stats表空间的id号(1),导致不能打开mysql/innodb_table_stats表空间,从而数据库不能打开,详细解释可以看这里:via

解决办法也简单,只需要更改my.cnf的一项配置就行,这里重点说下又一个坑,网上有地方说,MAMP的配置文件可以在/Applications/MAMP/conf下新建一个my.cnf,但是我试了之后发现,却没有生效,最后找到的解决办法就是,打开MAMP界面,然后一次点击:File-> Edit Template-> MySQL(my.cnf)。

然后按照如图的设置,增加或者直接修改,将innodb_force_recovery设置成大于0的数:
mamp-my-cnf-file-modify

如果没意外的话,重启MySQL,应该就能解决了。