我在虚拟机上模拟mysql服务器突然断电的场景,在mysql正常运行的情况下直接重启服务器,结果mysql数据库起不来了,详细情况如下:
环境:
操作系统 系统内核版本 mysql 版本
CentOS 5.4 Linux 2.6.18-164.el5 x86_64 5.6.14
1. 重启服务器后(模拟断电),启动mysql报错
[root@langkeziju2 bin]# ./mysqld_safe --user=mysql &
131104 10:02:26 mysqld_safe Logging to '/data0/mysql-3307/data/langkeziju2.err'.
131104 10:02:26 mysqld_safe Starting mysqld daemon with databases from /data0/mysql-3307/data
131104 10:02:28 mysqld_safe mysqld from pid file /data0/mysql-3307/data/langkeziju2.pid ended
2.在错误日志中查看究竟
[root@langkeziju2 bin]# vi /data0/mysql-3307/data/langkeziju2.err
2013-11-04 10:02:27 5816 [Note] InnoDB: The InnoDB memory heap is disabled
2013-11-04 10:02:27 5816 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2013-11-04 10:02:27 5816 [Note] InnoDB: Compressed tables use zlib 1.2.3
2013-11-04 10:02:27 5816 [Note] InnoDB: Using Linux native AIO
2013-11-04 10:02:27 5816 [Note] InnoDB: Using CPU crc32 instructions
2013-11-04 10:02:27 5816 [Note] InnoDB: Initializing buffer pool, size = 100.0M
2013-11-04 10:02:27 5816 [Note] InnoDB: Completed initialization of buffer pool
2013-11-04 10:02:28 5816 [Note] InnoDB: Highest supported file format is Barracuda.
2013-11-04 10:02:28 5816 [Note] InnoDB: The log sequence numbers 1600657 and 1600657 in ibdata files do not match the log sequence number 1676012 in the ib_logfiles!
2013-11-04 10:02:28 5816 [Note] InnoDB: Database was not shutdown normally!
2013-11-04 10:02:28 5816 [Note] InnoDB: Starting crash recovery.
2013-11-04 10:02:28 5816 [Note] InnoDB: Reading tablespace information from the .ibd files...
2013-11-04 10:02:28 5816 [ERROR] InnoDB: Attempted to open a previously opened tablespace. Previous tablespace hr/employee uses space ID: 1 at filepath: ./hr/employee.ibd. Cannot open tablespace mysql/innodb_table_stats which uses space ID: 1 at filepath: ./mysql/innodb_table_stats.ibd
2013-11-04 10:02:28 2aaf5d032520 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 ./mysql/innodb_table_stats.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.
131104 10:02:28 mysqld_safe mysqld from pid file /data0/mysql-3307/data/langkeziju2.pid ended
hr/employee 表空间id号(1)抢占了mysql/innodb_table_stats表空间的id号(1),导致不能打开mysql/innodb_table_stats表空间,从而数据库不能打开
3. 根据错误日志中的提示将innodb_force_recovery参数设置成>0的数值加入配置文件my.cnf中
[root@langkeziju2 bin]# vi /data0/mysql-3307/my.cnf
在[mysqld]组中加入:
innodb_force_recovery=6
*************************************
innodb_force_recovery参数解释:
innodb_force_recovery影响整个InnoDB存储引擎的恢复状况,默认值为0,表示当需要恢复时执行所有的恢复操作。
当不能进行有效的恢复操作时,mysql有可能无法启动,并记录下错误日志。
innodb_force_recovery可以设置为1-6,大的数字包含前面所有数字的影响。
当设置参数值大于0后,可以对表进行select,create,drop操作,但insert,update或者delete这类操作是不允许的。
1(SRV_FORCE_IGNORE_CORRUPT):忽略检查到的corrupt页
2(SRV_FORCE_NO_BACKGROUND):阻止主线程的运行,如主线程需要执行full purge操作,会导致crash
3(SRV_FORCE_NO_TRX_UNDO):不执行事务回滚操作。
4(SRV_FORCE_NO_IBUF_MERGE):不执行插入缓冲的合并操作。
5(SRV_FORCE_NO_UNDO_LOG_SCAN):不查看重做日志,InnoDB存储引擎会将未提交的事务视为已提交。
6(SRV_FORCE_NO_LOG_REDO):不执行前滚的操作。
4.mysql能正常启动了,我对hr库做了备份,然后将其删掉
[root@langkeziju2 bin]# ./mysqld_safe --user=mysql &
[1] 6440
[root@langkeziju2 bin]# 131104 10:04:06 mysqld_safe Logging to '/data0/mysql-3307/data/langkeziju2.err'.
131104 10:04:06 mysqld_safe Starting mysqld daemon with databases from /data0/mysql-3307/data
[root@langkeziju2 data_3307]# mysqldump -uroot -p hr > /bak/hr.sql
Enter password:
[root@langkeziju2 data_3307]# ls
hr.sql
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| hr |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)
mysql> drop database hr;
Query OK, 3 rows affected (0.65 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
5. 还原hr库,报:ERROR 1030 (HY000) at line 41: Got error -1 from storage engine
[root@langkeziju2 data_3307]# mysql -uroot -p hr < hr.sql
Enter password:
ERROR 1049 (42000): Unknown database 'hr.sql'
还原hr库,hr库必须存在,因我已经将hr库删除,现在创建一个空hr库
mysql> create database hr;
Query OK, 1 row affected (0.00 sec)
再次尝试还原hr库
[root@langkeziju2 data_3307]# mysql -uroot -p hr < hr.sql
Enter password:
ERROR 1030 (HY000) at line 41: Got error -1 from storage engine
报这个错误是因为上面在my.cnf中添加了innodb_force_recovery=6的缘故
6. 将my.cnf中的innodb_force_recovery参数设置成0或者将其删除
[root@langkeziju2 ~]# vi /data0/mysql-3307/my.cnf
[mysqld]
innodb_force_recovery=0
7.重启mysql
[root@langkeziju2 ~]# mysqladmin -uroot -p shutdown
[root@langkeziju2 ~]# mysqld_safe --user=mysql &
8. 成功还原hr库
[root@langkeziju2 data_3307]# mysql3307 hr < hr.sql
Enter password:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| hr |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.12 sec)