InnoDB on MySQL server was corrupted. And it could be solved by removing database files and reload SQL data.
Steps to recover MySQL with corrupted InnoDB files
- Stop MySQL server
- Backup InnoDB files (i.e. ib*), I suggests backup whole /var/lib/mysql directory.
- Adding innodb_force_recovery = 4 to [mysqld] section at MySQL server config file (eg. /etc/mysql/mysql.conf.d/mysqld.cnf in Ubuntu 16.04)
- Start MySQL server.
- Export all data: # mysqldump -A > mysqldump-A-`date +%s`.sql
- Finding databases stored in InnoDB with SQL: SELECT table_schema FROM INFORMATION_SCHEMA.TABLES WHERE engine = ‘innodb’ group by table_schema;
- Drop all databases stored in InnoDB. If any database in InnoDB could be dropped (eg. lost connection to server), you need to remove all files at /var/lib/mysql directory according to next section.
- Stop MySQL server.
- Remove InnoDB files (i.e. ib*).
- Remove innodb_force_recovery line from MySQL server config file.
- Start MySQL database for normal operation, new InnoDB files should be created. Let’s check whether it is in normal operation.
- Restore databases: # mysql -p < mysqldump-A-nnnnnn.sql
Removing ALL database files to recover MySQL
- Ensure that you have all backup data and backup copy of database files from /var/lib/mysql.
- Backup all database: # mysqldump -A > mysqldump-A-`date +%s`.sql
- Stop MySQL server.
- Remove all files in /var/lib/mysql
- Re-initialize inital database files for MySQL, new unknown random password will be generated for root user account in MySQL, therefore I prepared a
- Create /tmp/chmysqlpass SQL file:
- ALTER USER ‘root’@’localhost’ IDENTIFIED BY ‘sammy.hk’;
flush privileges;
- ALTER USER ‘root’@’localhost’ IDENTIFIED BY ‘sammy.hk’;
- Re-initialize inital database files: # mysqld –initialize
- Stop running MySQL server manually using: killall -9 mysqld
- Use initial SQL file to change password at startup# mysqld –init-file=/tmp/chmysqlpass
- Try to use mysql client to login with new password (example password: sammy.hk)
- Stop running MySQL server manually using: killall -9 mysqld
- Create /tmp/chmysqlpass SQL file:
- Start MySQL database for normal operation.
- Restore databases: # mysql -p < mysqldump-A-nnnnnn.sql
Reference
- https://chepri.com/blog/mysql-innodb-corruption-and-recovery/
- https://dev.mysql.com/doc/refman/5.7/en/forcing-innodb-recovery.html