search
HomeDatabaseMysql TutorialMySQL数据库表修复 MyISAM_MySQL

一:MySQL中MyISAM表损坏原因总结:

1、 服务器突然断电导致数据文件损坏;强制关机,没有先关闭mysql 服务;mysqld 进程在写表时被杀掉。
2、 磁盘损坏。
3、 服务器死机。
4、 mysql 本身的bug 。

二:MySQL中MyISAM表损坏的症状总结:

1 、查询数据时报出错误:Incorrect key file for table: '...'. Try to repair it
2 、查询不能在表中找到行或返回不完全的数据。
3 、Error: Table '...' is marked as crashed and should be repaired 。
4 、打开表失败: Can't open file: ‘×××.MYI' (errno: 145) 。

三:MySQL中MyISAM表损坏的预防:

1 、定期检查MyISAM 表。可以使用myisamchk,也可以使用mysqlcheck,还可以使用check table。
2 、在做过大量的更新或删除操作后,使用mysqlcheck或者optimize table来优化表,这样既减少了文件碎片,又减少了表损坏的概率。 3 、关闭服务器前,先关闭mysqld (正常关闭服务,不要使用kill -9 来杀进程)。
4 、使用ups 电源,避免出现突然断电的情况。
5 、使用最新的稳定发布版mysql ,减少mysql 本身的bug 导致表损坏。
6 、对磁盘做raid ,减少磁盘出错并提高性能。
7 、数据库服务器最好只跑mysqld 和必要的其他服务,不要跑其他业务服务,这样减少死机导致表损坏的可能。

四:MySQL中MyISAM表损坏的修复总结:

1 、如果mysqld已经宕掉,且无法启动,那么可以通过mysiamchk工具来进行修复。此工具在mysqld服务没有启动时才可以使用。该工具可以检查并分析修复MyISAM表。
2 、如果mysqld仍在运行,或者可以重新启动,那么可以通过mysqlcheck工具来进行修复。或者直接通过mysql的内置修复SQL语句来修复:CHECK TABLE,REPAIR TABLE ,ANALYSE TABLE,OPTIMIZE TABLE。这两种方法可以同样达到对表的修复作用。 以上两种方式各有应用场景。

MySQL数据表损坏的巧妙修复 修复mysql表之前首先要知道是哪个表出了问题

可以用root登录后在information_schema里查找有问题的表名

select TABLE_SCHEMA as DB_DBNAME,TABLE_NAME from INFORMATION_SCHEMA.tables where engine is null;

可以通过事件查看器里是否有

应用程序日志--类型为"错误"--事件来源为"Mysql"的错误日志,大概内容为

1 xxxxx: Table '.xxxx' is marked as crashed and should be repaired

或者直接在phpmyadmin中查看有哪些表出了问题比如显示“使用中”

如果查到问题表后,可以使用一下命令来修复mysql数据库中的MYISAM表

1、check, repair

例如abc的test表有问题

复制代码 代码如下:
# mysql -u root -p 'abc';   //登录mysql控制台,并进入abc数据库
mysql> check table test;  //如果有问题Msg_type里会有warning或error。没有问题的话出现的结果Status是OK
mysql> repair table test;  // 修复test表(多个表名用“逗号”隔开)
mysql> check table test; // 再次检查确认是否修复成功

2. myisamchk, isamchk

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

myisamchk tablename.MYI

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

myisamchk -of tablename.MYI

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

mysqladmin -uroot -p 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服务器必须没有启动!

3、检测修复所有数据库(表)

复制代码 代码如下:
MySQLcheck -A -o -r -p
mysqlcheck --all-databases -r

Statement
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
How does MySQL handle concurrency compared to other RDBMS?How does MySQL handle concurrency compared to other RDBMS?Apr 29, 2025 am 12:44 AM

MySQLhandlesconcurrencyusingamixofrow-levelandtable-levellocking,primarilythroughInnoDB'srow-levellocking.ComparedtootherRDBMS,MySQL'sapproachisefficientformanyusecasesbutmayfacechallengeswithdeadlocksandlacksadvancedfeatureslikePostgreSQL'sSerializa

How does MySQL handle transactions compared to other relational databases?How does MySQL handle transactions compared to other relational databases?Apr 29, 2025 am 12:37 AM

MySQLhandlestransactionseffectivelyusingtheInnoDBengine,supportingACIDpropertiessimilartoPostgreSQLandOracle.1)MySQLusesREPEATABLEREADasthedefaultisolationlevel,whichcanbeadjustedtoREADCOMMITTEDforhigh-trafficscenarios.2)Itoptimizesperformancewithabu

What are the data types available in MySQL?What are the data types available in MySQL?Apr 29, 2025 am 12:28 AM

MySQL data types are divided into numerical, date and time, string, binary and spatial types. Selecting the correct type can optimize database performance and data storage.

What are some best practices for writing efficient SQL queries in MySQL?What are some best practices for writing efficient SQL queries in MySQL?Apr 29, 2025 am 12:24 AM

Best practices include: 1) Understanding the data structure and MySQL processing methods, 2) Appropriate indexing, 3) Avoid SELECT*, 4) Using appropriate JOIN types, 5) Use subqueries with caution, 6) Analyzing queries with EXPLAIN, 7) Consider the impact of queries on server resources, 8) Maintain the database regularly. These practices can make MySQL queries not only fast, but also maintainability, scalability and resource efficiency.

How does MySQL differ from PostgreSQL?How does MySQL differ from PostgreSQL?Apr 29, 2025 am 12:23 AM

MySQLisbetterforspeedandsimplicity,suitableforwebapplications;PostgreSQLexcelsincomplexdatascenarioswithrobustfeatures.MySQLisidealforquickprojectsandread-heavytasks,whilePostgreSQLispreferredforapplicationsrequiringstrictdataintegrityandadvancedSQLf

How does MySQL handle data replication?How does MySQL handle data replication?Apr 28, 2025 am 12:25 AM

MySQL processes data replication through three modes: asynchronous, semi-synchronous and group replication. 1) Asynchronous replication performance is high but data may be lost. 2) Semi-synchronous replication improves data security but increases latency. 3) Group replication supports multi-master replication and failover, suitable for high availability requirements.

How can you use the EXPLAIN statement to analyze query performance?How can you use the EXPLAIN statement to analyze query performance?Apr 28, 2025 am 12:24 AM

The EXPLAIN statement can be used to analyze and improve SQL query performance. 1. Execute the EXPLAIN statement to view the query plan. 2. Analyze the output results, pay attention to access type, index usage and JOIN order. 3. Create or adjust indexes based on the analysis results, optimize JOIN operations, and avoid full table scanning to improve query efficiency.

How do you back up and restore a MySQL database?How do you back up and restore a MySQL database?Apr 28, 2025 am 12:23 AM

Using mysqldump for logical backup and MySQLEnterpriseBackup for hot backup are effective ways to back up MySQL databases. 1. Use mysqldump to back up the database: mysqldump-uroot-pmydatabase>mydatabase_backup.sql. 2. Use MySQLEnterpriseBackup for hot backup: mysqlbackup--user=root-password=password--backup-dir=/path/to/backupbackup. When recovering, use the corresponding life

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.