Skip to content

Repair broken tables in MySQL

Muchas veces las tablas se las bases de datos MySQL se rompen, this is a way to check their status and repair:

  1. Should replace "db-data_1" with the actual name of the database and "aTable" by the real name of the table.
  2. ALWAYS: hacer un backup de la tabla antes de proceder con la reparación, por las dudas que se cague aún más.

# Connecting to MySQL from the console:

mysql -u root -p

# We move to the BD table having broken

mysql> use base-de-datos_1;

# We check the status of the table:

mysql> check table laTabla;
+------------------------------+-------+----------+------------------------------------------------------------------------------------+
| Table                        | On    | Msg_type | Msg_text                                                                           |
+------------------------------+-------+----------+------------------------------------------------------------------------------------+
| base-de-datos_1.laTabla | check | error    | Table './base-de-datos_1/laTabla' is marked as crashed and should be repaired |
+------------------------------+-------+----------+------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

# The repair

mysql> repair table laTabla;

# Attention: si esto no arregla la tabla rota, lo que se debe hacer es lo siguiente:
# Salimos del shell mysql con “quit;” y volvemos al shell Linux como root, allí tipeamos lo siguiente:

[root@hostname.server.com]# myisamchk -r /var/lib/mysql/base-de-datos_1/laTabla.MYI
- recovering (with sort) MyISAM-table '/var/lib/mysql/base-de-datos_1/laTabla.MYI'
Data records: 43071
- Fixing index 1
- Fixing index 2
- Fixing index 3
- Fixing index 4
[root@hostname.server.com]

# And if this fails, going to go with otra variant al use "-safe-recover", this option takes longer, but it is recommended if the above fails:

myisamchk -r --safe-recover /var/lib/mysql/base-de-datos_1/laTabla.MYI