在没有额外磁盘空间和表损坏时修复 Myisam 表

Gop*_*ath 5 mysql myisam mysql-5.1 temporary-tables

我有一个表,其中 MyIsam 作为存储引擎包含数百万行。有一个清除任务,需要删除 35 天的旧数据。我按以下方式测量了行:

mysql> select count(*) from table_date;

+----------+
| count(*) |
+----------+
| 53217368 |
+----------+

2. Here are the min and max value for the  table1:

mysql> select min(table_date),max(table_date) from table1;
+---------------------+---------------------+
| min(table_date)     | max(table_date)     |
+---------------------+---------------------+
| 2011-08-09 04:05:01 | 2012-01-13 04:04:16 |
+---------------------+---------------------+
row in set (0.01 sec)

3. Date and time 35 days ago from the current date & time will be:

mysql> select now(),now() - interval 35 day;
+---------------------+-------------------------+
| now()               | now() - interval 35 day |
+---------------------+-------------------------+
| 2012-01-13 21:41:36 | 2011-12-09 21:41:36     |
+---------------------+-------------------------+

4. Number of rows which contains dataentry_date less than the above date is:

mysql> select count(*) from table1 where table_date < '2011-12-09 21:41:36';
+----------+
| count(*) |
+----------+
| 30729315 |
+----------+
1 row in set (3 min 3.34 sec)

Now purged the 3 million rows using :

mysql> delete from table1 where table_date < (now() - interval 35 day);

After running  the  delete query has been terminated with the below error message
and mysql has been restarted.

Tried to Repair using :

mysql> check table table_date;
+----------------------------+-------+----------+-----------------------------------------------------------------------------------+
| Table                      | Op    | Msg_type | Msg_text                                                                          |
+----------------------------+-------+----------+-----------------------------------------------------------------------------------+
| tabe1.table_date           | check | warning  | 7 clients are using or haven't closed the table properly                          |
| tabe1.table_date           | check | error    | Can't read key from filepos: 2048                                                 |
| tabe1.table_date           | check | Error    | Incorrect key file for table './table1/table_date.MYI'; try to repair it |
| tabe1.table_date           | check | error    | Corrupt                                                                           |
+----------------------------+-------+----------+-----------------------------------------------------------------------------------+
4 rows in set (0.01 sec)

No luck & then tried with repair table :

mysql> repair table table_date;
+----------------------------+--------+----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table                      | Op     | Msg_type | Msg_text                                                                                                                                                                                 |
+----------------------------+--------+----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| tabe1.table_date           | repair | error    | 28 when writing to datafile                                                                                                                                                              |
| tabe1.table_date           | repair | Error    | Disk is full writing './table/tabl1.TMD' (Errcode: 28). Waiting for someone to free space... (Expect up to 60 secs delay for server to continue after freeing disk space)                |
| tabe1.table_date           | repair | Error    | Retry in 60 secs. Message reprinted in 600 secs                                                                                                                                          |
| tabe1.table_date           | repair | Error    | Retry in 60 secs. Message reprinted in 600 secs                                                                                                                                          |
| tabe1.table_date           | repair | Error    | Error writing file './table/table_data.TMD' (Errcode: 28)                                                                                                                                |
| tabe1.table_date           | repair | status   | Operation failed                                                                                                                                                                         |
+----------------------------+--------+----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
6 rows in set (43 min 5.53 sec)
Run Code Online (Sandbox Code Playgroud)

又没有运气了。

Error Log :
--------------------
Version: '5.1.45-community' socket: '/var/lib/mysql/mysql.sock' port: 3306
MySQL Community Server (GPL)
120120 4:38:05 [ERROR] /usr/sbin/mysqld: Incorrect key file for
table './tabl1/table_data.MYI'; try to repair it
120120 4:39:14 [ERROR] /usr/sbin/mysqld: Incorrect key file for
table './table1/table_date.MYI'; try to repair it
120120 5:05:13 [ERROR] /usr/sbin/mysqld: Disk is full
writing './table1/table_date.TMD' (Errcode: 28). Waiting for someone to free
space... (Expect up to 60 secs delay for server to continue after freeing disk
space)
120120 5:05:13 [ERROR] /usr/sbin/mysqld: Retry in 60 secs. Message reprinted
in 600 secs
120120 5:15:13 [ERROR] /usr/sbin/mysqld: Retry in 60 secs. Messagemysql>

I tried with Myisamchk -r & -o  also. But no luck.

Here is my present table status :

mysql> show table status like 'table_date'\G
        *************************** 1. row ***************************
                   Name: table_date
                 Engine: NULL
                Version: NULL
             Row_format: NULL
                   Rows: NULL
         Avg_row_length: NULL
            Data_length: NULL
        Max_data_length: NULL
           Index_length: NULL
              Data_free: NULL
         Auto_increment: NULL
            Create_time: NULL
            Update_time: NULL
             Check_time: NULL
              Collation: NULL
               Checksum: NULL
         Create_options: NULL
                Comment: Table './table1/table_date'
                         is marked as crashed and last (automatic?)
      1 row in set (0.01 sec)
Run Code Online (Sandbox Code Playgroud)

一季度。当我使用 myisamchk 时,创建了 .TMD。它是一个我可以理解的中间数据文件。如果我可以删除它,它会起作用。?

Q2。该表占用17GB空间。并在执行删除后崩溃。尝试修复磁盘空间问题时。使该表恢复到工作状态的任何棘手解决方案。解决方案将不胜感激。

Aar*_*own 7

如果您无法释放或添加任何磁盘空间,您可以将 table_data.* 文件(MYD、MYI、frm 等)复制到另一台有大量可用磁盘的机器上,使用 myisamchk 在那里运行修复,然后将文件复制回原机。如果服务器需要保持运行,请FLUSH TABLES WITH READ LOCK table_data在将数据复制到另一台服务器之前和UNLOCK TABLES将数据复制回来之后执行。您需要在此期间保持 FLUSH TABLES ... 会话运行。

所以,它看起来像这样

服务器 1:

# keep this session running for the duration of the repair!
mysql> FLUSH TABLES WITH READ LOCK table_data;
/var/lib/mysql# rsync -aP table_date.* server2:/somedir/
Run Code Online (Sandbox Code Playgroud)

服务器2:

# make a backup
/somedir# tar -czvf table_data_backup.tgz table_date.*
# run the repair
/somedir# myisamchk -r table_data
# copy the files back
/somedir# rsync -aP table_date.* server1:/var/lib/mysql/
Run Code Online (Sandbox Code Playgroud)

服务器 1:

# make sure the permissions are correct on /var/lib/mysql/table_date.*
mysql> UNLOCK TABLES;
Run Code Online (Sandbox Code Playgroud)

请记住,不要关闭 FLUSH TABLES WITH READ LOCK 会话

您可能希望为以下参数提供 myisamchk 额外内存以加快速度:--key_buffer_size --sort_buffer_size --read_buffer_size --write_buffer_size

接下来,将您的表转换为 InnoDB。现在几乎没有充分的理由使用 MyISAM。