Home > Database > Mysql Tutorial > body text

如何修复损坏的MySQL数据表

WBOY
Release: 2016-06-07 17:10:22
Original
1218 people have browsed it

于断电或非正常关机而导致MySQL数据库出现错误是非常常见的问题。有两种方法,一种方法使用mysql的check table和repair table 的

于断电或非正常关机而导致MySQL数据库出现错误是非常常见的问题。有两种方法,一种方法使用mysql的check table和repair table 的sql语句,另一种方法是使用MySQL提供的多个myisamchk, isamchk数据检测恢复工具。前者使用起来比较简便。推荐使用。

1. check table 和 repair table
登陆mysql 终端:
mysql -uxxxxx -p dbname
> check table tabTest;
如果出现的结果说Status是OK,则不用修复,如果有Error,可以用:
> repair table tabTest;
进行修复,修复之后可以在用check table命令来进行检查。在新版本的phpMyAdmin里面也可以使用check/repair的功能。

2. myisamchk, isamchk
其中myisamchk适用于MYISAM类型的数据表,而isamchk适用于ISAM类型的数据表。这两条命令的主要参数相同,一般新的系统都使用MYISAM作为缺省的数据表类型,这里以myisamchk为例子进行说明。当发现某个数据表出现问题时可以使用:

myisamchk tablename.MYI

进行检测,如果需要修复的话,可以使用:

myisamchk -of tablename.MYI

关于myisamchk的详细参数说明,可以参见它的使用帮助。需要注意的时在进行修改时必须确保MySQL服务器没有访问这个数据表,保险的情况下是最好在进行检测时把MySQL服务器Shutdown掉。

-----------------------------

另外可以把下面的命令放在你的rc.local里面启动MySQL服务器前:

[ -x /tmp/mysql.sock ] && /pathtochk/myisamchk -of /DATA_DIR/*/*.MYI

其中的/tmp/mysql.sock是MySQL监听的Sock文件位置,对于使用RPM安装的用户应该是/var/lib/mysql/mysql.sock,对于使用源码安装则是/tmp/mysql.sock可以根据自己的实际情况进行变更,而pathtochk则是myisamchk所在的位置,DATA_DIR是你的MySQL数据库存放的位置。

需要注意的时,,如果你打算把这条命令放在你的rc.local里面,必须确认在执行这条指令时MySQL服务器必须没有启动!

检测修复所有数据库(表)
mysqlcheck -A -o -r -p
---------------------------------------------------------------------------------


示例:
mysql> check table tabFTPAccountInstances;

linux

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!