Programming, tutorials, mechatronics, operating systems, and other tech stuff

Fixing mysql crash caused by InnoDB

2 comments

The Problem

I ran a moodle application on ubuntu inside virtual machine (virtualbox). It has been working fine for a long time until one day, it decided to stop working in the middle of an exam participated by -+ 40 students. It shows that the database is somehow crashed.


Diagnosis

I tried to open the mysql using mysql client, and i'm unable to.

ERROR 2002: Can't connect to local MySQL server through socket '/tmp/mysql.sock'
I check the state of mysql server and turn out it have forcibly stopped, so i try to run it again using:

/etc/init.d/mysql start
and get

start: Job failed to start

First, because i ran the system in a vm, i thought it was either the disk space or memory, so i ran df command then found out it currently use only 5% of the total disk spaces. And then i ran htop and it only used <500MB from the total of 8GB allocated to the vm so i ruled out both possibility.

Ok, so i know there are something definitely wrong with the mysql server. So, just in case, i made a backup to the mysql data directory into my home dir (and you should do to, backup is very important afterall)

sudo cp -rp /var/lib/mysql ~/mysql/
The -p option here is to keep the permission, ownership and timestamp intact.

I checked the mysql logs:

cat /var/log/mysql.log
cat /var/log/mysql.err
both shows nothing, so i checked the error log

cat /var/log/mysql.err
And checking some latest lines, i got

mysqld got signal 11 ;
This could be because you hit a bug. It is also possible that this binary
or one of the libraries it was linked against is corrupt, improperly built,
or misconfigured. This error can also be caused by malfunctioning hardware.
Attempting to collect some information that could help diagnose the problem.
As this is a crash and something is definitely wrong, the information
collection process might fail.
......
And bunch of log text which i didn't particularly understood, stating that mysql server is crashing.

Solution

After scouring the web for solution, i came across a possibility that my innodb database is corrupted. So as a trial and error, i remove these 3 files:

/var/lib/mysql/ibdata1
/var/lib/mysql/ib_logfile0
/var/lib/mysql/ib_logfile1
Then try to run the mysql server again using /etc/init.d/mysql start and it runs. With this, i am sure that it's the innodb fault.
Before continuing, stopped the mysql server again, then copy those 3 files from the backup i've made, then start the mysql server again. As expected, it failed to start.

After scouring the web again, i've found a solution to run mysql server in innodb force recovery mode. I open /etc/mysql/my.cnf using nano, then add these lines:

innodb_force_recovery = 2
innodb_purge_threads = 0
As described here, i tried with number 1 first, then continue upward until the server could run. Found out that my magic number is 2. Which means preventing master and purge threads from running.
Taken from the link above:
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. 
After the mysql server runs, i connect to the mysql using

mysql -u root -p
to make sure that my databases and tables are still there. And they are.
In this state, you won't be able to insert or update anything in the database. You can only read and drop things. So the idea here is to dump the corrupted table to a file somewhere, drop the table from database, and then import the dumped file back into database.

I run mysqlcheck to see which tables is corrupted:
mysqlcheck -u root -p --all-databases
and it says all my tables is OK.
in my case, i have to pipe the command to "more" command because i have a lot of tables.
mysqlcheck -u root -p --all-databases | more
This confuse me as i don't know which table i should dump. So i dump my database into a file as a backup. Luckily, my database is not too big to dump.

mysqldump -u root -p [pass] [db name] > [db name].sql
and it produce a single -+ 600MB sql file for a database in my case (it still pretty big for me).

If in your case you found out the corrupted table when you do mysqlcheck, you should only dump that corrupted table to avoid bloating the dump file. Especially when your database is big.

mysqldump -u root -p [pass] [db name] [table name] > [db name].[table name].sql

Then i drop the whole database, create a new database with same name, and import the dumped file back into that database.

Be careful, in your case, you might only need to drop the corrupted table, not the whole database.

And after the import proccess is finished. My moodle application can run normally.

2 comments :

  1. El Cortez - Casino Restaurants | Mapyro
    › restaurants 안산 출장안마 군포 출장안마 › restaurants 광양 출장샵 El Cortez, El Cortez, located in 서귀포 출장마사지 El Cortez, features 392 slot machines, over 1500 table games, poker tables, an expansive roster of restaurants, a casino and 아산 출장마사지

    ReplyDelete
  2. Are you looking to elevate your travel with Asiana Airlines? Need help on How to cancel and refund at Asiana Airlines Flight? For assistance, contact Asiana Airlines at OTA+1 800 970 3794 (no wait).

    How to cancel and refund at Asiana Airlines Flight?

    ReplyDelete