2016年10月11日 星期二

MySQL 4 Error : MySQL can’t open file MYI or MYD (errno: 145)

https://braingrabber.wordpress.com/2012/12/07/mysql-cant-open-file-myi-or-myd-errno-145/


MySQL can’t open file MYI or MYD (errno: 145)


Some of our monitoring applications write their data redundantly to a local MySQL database, plain text on a network share and to a MSSQL database. This is an out of the box implementation and maintained externally. Sometimes we get stuck with MySQL buggering the whole application because there is something wrong with one of the data files.
Almost all cases we get confronted with error “Can’t open file: ‘file.MYI or file.MYD’. (errno: 145)”and the table is not visible in the MySQL browser. I’m afraid the users reboot the machine now and then when they think it’s not responding. Since this machine is continuously writing sensor data it might cause data corruption. Since the descript of error 145 is “MySQL error code 145 = Table was marked as crashed and should be repaired” it’s time to repair the table.
How to troubleshoot this problem in a few quick steps:
  1. Stop the mysqld.
  2. Copy the data files MYI (indices), MYD (data file) and frm (table definition) of the files that are giving the error in the log.
  3. Open up a command-line and browse to the MySQL bin directory.
  4. First try a quick (-q) recovery (-r) by using the following command: myisamchk –r –q “location of the MYI/MYD file”, i.e. myisamchk –r –q c:\mysql\data\db.MYI
  5. Start the mysqld and check the logs if the error disappeared.
  6. If the error still exists, stop the mysqld again.
  7. Get your command-line back up and use the following command: myisamchk –r “location of the MYI/MYD file”, i.e. myisamchk –r c:\mysql\data\db.MYI
  8. Start the daemon again and check the logs. Most of the times this will do the job to get the table up and running again.
If it doesn’t does the job for you, check out the following links:


Method 1.

Step 1. Log into mysql as administrator

mysql -uroot -p

Step 2. Select the database containing the corrupted table, i.e., ‘use {database name};’

mysql> use {database name};

Step 3. Show tables your corrupted table should be listed

mysql> show tables;

Step 4. Repair table

mysql> repair table {bad_table_name};


Method 2.

Step 1. Login to your MySQL server using Secure Shell (SSH).

Step 2. Repairing MyISAM mySQL Tables/Databases.

cd /var/lib/mysql/DBNAME

myisamchk -r -f tables.MYI

Please note that MyISAM is a default storage engine of MySQL, unless you change it. Mostly MyISAM is set as default engine to check and repair all the tables in the mysql database.



Some other errors that may be caused by table corruption include:
  • Can't find file table.MYI.
  • Duplicate unique key or constraint on write or update.
  • Got error ERR from table handler. (ERR being a number).
  • Index file is crashed / Wrong file format.
  • No more room in index file.
  • No more room in record file.
  • Old database file.
  • Record file is crashed.
  • Record was already deleted (or record file crashed).
  • table.frm is locked against change.
  • Table is crashed and last repair failed.
  • Tables was marked as crashed and should be repaired.
  • Unexpected end of file.



WebjxCom提示:这个错误是典型的mysql表受损造成的,解决的办法就是修复表,这个问题也是mysql经常容易出现的,mysql表和索引损坏的几率很大,但是修复也很方便,减少此类问题发生的办法就是尽量减少服务器无故断电,关闭服务器时,最后先手动关闭mysql数据库。
这个错误是典型的mysql表受损造成的,解决的办法就是修复表,这个问题也是mysql经常容易出现的,mysql表和索引损坏的几率很大,但是修复也很方便,减少此类问题发生的办法就是尽量减少服务器无故断电,关闭服务器时,最后先手动关闭mysql数据库。
下面看看这个问题:

Can’t open file: ‘×××.MYI’ (errno: 145),这个错误一般就是你的数据库表文件损坏造成的,造成的原因大概是你把数据库文件挪来挪去的结果,当然不排除其他原因啊。
具体数据库出现错误的提示代码是:
 Invalid SQL: ……
MySQL 错误!: 1016 (Can’t open file: ‘×××.MYI’ (errno: 145))
解决这个问题有多种方案,如果你的数据库可以远程链接或者你可以连接到你的远程服务器桌面,并且你的服务器系统刚好是Windows的,那么你可以:
运行->cmd
然后:mysql –h 主机地址 –u 用户名 –p
然后输入密码,连接数据库;
Use 数据库名称;
然后 输入:reapair table 出错的表名称,回车,这样修复一下就可以了,参考图片如下:
image002
第二种方案,假设你只能运行PHP代码,那么也很简单,写一个php文件,链接上数据库,把这段代码:
Reapair table `表名`,
像发送 “select * ……”一样发送给mysql,也是可以的;
第三种方案,可简单了,打开你的myphpadmin,选中Can’t open file:后面同名的表,有个下拉菜单“选中项”,选择“修复”如图:
image004
就ok了。
如果你的数据库主机就你一个人用,并且你还能控制的话,建议你用Mysql自带的修复工具myisamchk.exe进行修复,操作如下:
修复前将mysql服务停止。
如果是Win主机,打开命令行方式,然后进入到mysql的/bin目录。
执行myisamchk -r  数据库所在路径\*.MYI
如果是类Unix主机,直接使用myisamchk -r  数据库目录\*.MYI
其它参考:
Mysql有的时候因为掉电或者其他原因导致数据库损坏,错信息如下:
MySql: Can't open file: 'sdb_sessions.MYI'. (errno: 145)
For more information, see Help and Support Center at
我们可以使用mysql自带的mysqlcheck命令来快速修复所有的数据库或者特定的数据库;例如
检查优化并修复所有的数据库用:
# mysqlcheck -A -o -r -p
Enter password:
guestbook.simpgb_avatars OK
guestbook.simpgb_bad_words OK
guestbook.simpgb_banlist OK
guestbook.simpgb_data OK
......
......
......
mysqlcheck语法/html/Program/Mssql/200711/130.html
修复指定的数据库用
# mysqlcheck -A -o -r Database_NAME -p
IXDBA.NET社区论坛
另外如果只是对某个表进行修复可以用: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数据库存放的位置。