Recovering corrupted database tables: Difference between revisions
(Review: opravy formátování (hierarchie nadpisů), přidány kategorie, aktualizován AI Summary) |
(Rewrite: consolidated from 617 to 237 lines, added quick reference table, streamlined procedures, improved structure) |
||
| Line 3: | Line 3: | ||
[[Category:Troubleshooting]] | [[Category:Troubleshooting]] | ||
Guide for recovering corrupted database tables in MariaDB/MySQL. Methods are ordered from least to most invasive. | |||
== Quick Reference == | |||
= | {| class="wikitable" | ||
! Symptom !! Likely Cause !! Recommended Method | |||
|- | |||
| <code>Failed to read from .par file</code> || Metadata corruption || [[#Method 2: Transportable Tablespaces (InnoDB)|Method 2: Transportable Tablespaces]] | |||
|- | |||
| REPAIR TABLE fails || InnoDB metadata damage || [[#Method 2: Transportable Tablespaces (InnoDB)|Method 2: Transportable Tablespaces]] | |||
|- | |||
| MyISAM table errors || MyISAM corruption || [[#Method 1: REPAIR TABLE (MyISAM)|Method 1: REPAIR TABLE]] | |||
|- | |||
| <code>mysql.innodb_table_stats</code> errors || Disk space exhaustion || [[#Method 4: System Table Corruption|Method 4: System Tables]] | |||
|- | |||
| .ibd file corrupted/missing || Severe data corruption || [[#Method 3: Drop and Recreate (Destructive)|Method 3: Drop/Recreate]] | |||
|} | |||
{{Warning|1=Always backup your database before attempting recovery. Run <code>mysqldump -u root -p voipmonitor > backup.sql</code> first.}} | |||
== Method 1: REPAIR TABLE (MyISAM) == | |||
For MyISAM tables only (not common in VoIPmonitor): | |||
<syntaxhighlight lang="sql"> | <syntaxhighlight lang="sql"> | ||
| Line 28: | Line 31: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
{{Note|1=Most VoIPmonitor tables use InnoDB. Use Method 2 for InnoDB tables.}} | |||
== Method 2: Transportable Tablespaces | == Method 2: Transportable Tablespaces (InnoDB) == | ||
'''Recommended''' for InnoDB when <code>.par</code> file is corrupted but <code>.ibd</code> data file is intact. Preserves all data. | |||
<kroki lang="mermaid"> | <kroki lang="mermaid"> | ||
%%{init: {'flowchart': {'nodeSpacing': 10, 'rankSpacing': 25}}}%% | %%{init: {'flowchart': {'nodeSpacing': 10, 'rankSpacing': 25}}}%% | ||
flowchart LR | flowchart LR | ||
A["1. Stop voipmonitor"] --> B["2. Create new DB"] | |||
B --> C["3. Start/stop voipmonitor<br/>(creates schema)"] | |||
C --> D["4. DISCARD tablespace"] | |||
D --> E["5. Copy .ibd files"] | |||
E --> F["6. IMPORT tablespace"] | |||
F --> G["7. Verify & switch"] | |||
</kroki> | </kroki> | ||
=== | === Step-by-Step Procedure === | ||
'''1. Stop service and create recovery database:''' | |||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
systemctl stop voipmonitor | systemctl stop voipmonitor | ||
mysql -u root -p -e "CREATE DATABASE voipmonitorBackup;" | |||
CREATE DATABASE voipmonitorBackup; | |||
</syntaxhighlight> | </syntaxhighlight> | ||
'''2. Configure VoIPmonitor to use new database temporarily:''' | |||
<syntaxhighlight lang="bash"> | |||
# Edit /etc/voipmonitor.conf | |||
<syntaxhighlight lang=" | |||
mysqldb = voipmonitorBackup | mysqldb = voipmonitorBackup | ||
</syntaxhighlight> | </syntaxhighlight> | ||
'''3. Let VoIPmonitor create fresh schema:''' | |||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
systemctl start voipmonitor | systemctl start voipmonitor | ||
sleep 60 # Wait for schema creation | |||
Wait for | |||
systemctl stop voipmonitor | systemctl stop voipmonitor | ||
</syntaxhighlight> | </syntaxhighlight> | ||
'''4. Discard tablespaces in new database:''' | |||
<syntaxhighlight lang="sql"> | <syntaxhighlight lang="sql"> | ||
USE voipmonitorBackup; | USE voipmonitorBackup; | ||
-- Discard main table | -- Discard main table and partitions | ||
ALTER TABLE cdr DISCARD TABLESPACE; | ALTER TABLE cdr DISCARD TABLESPACE; | ||
-- | -- For partitioned tables, discard each partition: | ||
ALTER TABLE cdr DISCARD TABLESPACE FOR PARTITION p20250101; | ALTER TABLE cdr DISCARD TABLESPACE FOR PARTITION p20250101; | ||
-- Repeat for all partitions (check with SHOW CREATE TABLE cdr\G) | |||
-- Repeat for all partitions ( | |||
</syntaxhighlight> | </syntaxhighlight> | ||
= | {{Tip|1=Generate DISCARD commands automatically: | ||
<code>SELECT CONCAT('ALTER TABLE cdr DISCARD TABLESPACE FOR PARTITION ', PARTITION_NAME, ';') FROM INFORMATION_SCHEMA.PARTITIONS WHERE TABLE_SCHEMA='voipmonitorBackup' AND TABLE_NAME='cdr' AND PARTITION_NAME IS NOT NULL;</code>}} | |||
'''5. Copy .ibd files from old database:''' | |||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
# | # Copy data files | ||
cp /var/lib/mysql/voipmonitor/cdr.ibd /var/lib/mysql/voipmonitorBackup/ | cp /var/lib/mysql/voipmonitor/cdr.ibd /var/lib/mysql/voipmonitorBackup/ | ||
cp /var/lib/mysql/voipmonitor/cdr#p#*.ibd /var/lib/mysql/voipmonitorBackup/ | cp /var/lib/mysql/voipmonitor/cdr#p#*.ibd /var/lib/mysql/voipmonitorBackup/ | ||
# Set | # Set ownership | ||
chown mysql:mysql /var/lib/mysql/voipmonitorBackup/cdr*.ibd | chown mysql:mysql /var/lib/mysql/voipmonitorBackup/cdr*.ibd | ||
</syntaxhighlight> | </syntaxhighlight> | ||
'''6. Import tablespaces:''' | |||
<syntaxhighlight lang="sql"> | <syntaxhighlight lang="sql"> | ||
USE voipmonitorBackup; | USE voipmonitorBackup; | ||
ALTER TABLE cdr IMPORT TABLESPACE; | ALTER TABLE cdr IMPORT TABLESPACE; | ||
ALTER TABLE cdr IMPORT TABLESPACE FOR PARTITION p20250101; | ALTER TABLE cdr IMPORT TABLESPACE FOR PARTITION p20250101; | ||
-- Repeat for all partitions | |||
-- | |||
</syntaxhighlight> | </syntaxhighlight> | ||
'''7. Verify and finalize:''' | |||
<syntaxhighlight lang="sql"> | <syntaxhighlight lang="sql"> | ||
SELECT COUNT(*) FROM cdr; | SELECT COUNT(*) FROM cdr; | ||
SELECT MIN(calldate), MAX(calldate) FROM cdr; | SELECT MIN(calldate), MAX(calldate) FROM cdr; | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Update <code>/etc/voipmonitor.conf</code> to use <code>voipmonitorBackup</code> permanently, then start service: | |||
< | |||
</ | |||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
systemctl start voipmonitor | systemctl start voipmonitor | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== Method 3: Drop and Recreate (Destructive) == | |||
== Method 3: Drop and Recreate | |||
{{Warning|1=Use ONLY when .ibd files are corrupted and you have a backup or data loss is acceptable.}} | |||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
systemctl stop voipmonitor | systemctl stop voipmonitor | ||
</syntaxhighlight> | </syntaxhighlight> | ||
<syntaxhighlight lang="sql"> | <syntaxhighlight lang="sql"> | ||
USE voipmonitor; | USE voipmonitor; | ||
DROP TABLE IF EXISTS cdr; | DROP TABLE IF EXISTS cdr; | ||
-- Get fresh schema from GUI: Tools > System Status > Check MySQL Schema | |||
CREATE TABLE `cdr` ( ... ); | |||
</syntaxhighlight> | </syntaxhighlight> | ||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
| Line 288: | Line 134: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== | == Method 4: System Table Corruption == | ||
Applies when MySQL logs errors about <code>mysql.innodb_table_stats</code>, <code>mysql.column_stats</code>, or similar system tables. Usually caused by disk space exhaustion. | |||
=== Diagnosis === | === Diagnosis === | ||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
# Check disk space | # Check disk space (common root cause) | ||
df -h /var/lib/mysql | df -h /var/lib/mysql | ||
# | # Check MySQL error log | ||
tail -100 /var/log/mysql/error.log | |||
</syntaxhighlight> | </syntaxhighlight> | ||
=== Quick Workaround === | |||
= | |||
Add to <code>/etc/mysql/my.cnf</code> under <code>[mysqld]</code>: | |||
<syntaxhighlight lang="ini"> | <syntaxhighlight lang="ini"> | ||
innodb_stats_persistent = 0 | innodb_stats_persistent = 0 | ||
</syntaxhighlight> | </syntaxhighlight> | ||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
systemctl restart mysql | |||
</syntaxhighlight> | </syntaxhighlight> | ||
=== Permanent Fix === | |||
== | |||
'''Option A: Run mysql_upgrade''' (after OS/MySQL version upgrades): | |||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
mysql_upgrade -u root -p | |||
systemctl restart mysql | |||
</syntaxhighlight> | </syntaxhighlight> | ||
'''Option B: Full database reinitialize''' (if Option A fails): | |||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
# | # 1. Backup everything | ||
mysqldump -u root -p --all-databases > full_backup.sql | |||
# | # 2. Stop and reinitialize | ||
mysql - | systemctl stop mysql | ||
mv /var/lib/mysql /var/lib/mysql-corrupted | |||
mysql_install_db --user=mysql # or: mysqld --initialize --user=mysql | |||
# | # 3. Start and restore | ||
mysql | systemctl start mysql | ||
mysql -u root -p | mysql -u root -p < full_backup.sql | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== | == Troubleshooting == | ||
=== | {| class="wikitable" | ||
! Error !! Solution | |||
|- | |||
| Schema mismatch during IMPORT || Check <code>journalctl -u voipmonitor</code> for ALTER TABLE commands and run them manually | |||
|- | |||
| Cannot DISCARD TABLESPACE || Verify InnoDB engine: <code>SELECT ENGINE FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME='cdr';</code><br/>Try: <code>SET FOREIGN_KEY_CHECKS=0;</code> before DISCARD | |||
|- | |||
| DROP TABLE hangs || Stop MariaDB, delete files manually from <code>/var/lib/mysql/voipmonitor/</code>, restart | |||
|} | |||
== Prevention == | |||
* '''Regular backups:''' <code>mysqldump -u root -p voipmonitor | gzip > backup_$(date +%F).sql.gz</code> | |||
* '''Monitor disk space:''' Alert when MySQL partition > 80% full | |||
* '''Clean shutdown:''' Always use <code>systemctl stop</code> before maintenance | |||
* '''UPS:''' Prevent sudden power loss | |||
* '''Replication:''' See [[Redundant_database|Redundant Database]] | |||
=== Automated Backup Example === | |||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
# | #!/bin/bash | ||
# | # /usr/local/bin/backup_voipmonitor_db.sh | ||
BACKUP_DIR="/var/backups/voipmonitor" | |||
mkdir -p $BACKUP_DIR | |||
mysqldump -u root -p"$MYSQL_PASSWORD" voipmonitor | gzip > "$BACKUP_DIR/voipmonitor_$(date +%F).sql.gz" | |||
find $BACKUP_DIR -name "*.sql.gz" -mtime +7 -delete | |||
</syntaxhighlight> | </syntaxhighlight> | ||
== | Cron: <code>0 2 * * * /usr/local/bin/backup_voipmonitor_db.sh</code> | ||
== See Also == | |||
* [[Database_structure|Database Structure Reference]] | * [[Database_structure|Database Structure Reference]] | ||
* [[ | * [[Database_troubleshooting|Database Troubleshooting]] | ||
* [[Redundant_database|Redundant Database]] | * [[Redundant_database|Redundant Database]] | ||
* [[Emergency_procedures|Emergency Procedures]] | * [[Emergency_procedures|Emergency Procedures]] | ||
== AI Summary for RAG == | == AI Summary for RAG == | ||
'''Summary:''' Database table corruption recovery guide for VoIPmonitor. Four methods | '''Summary:''' Database table corruption recovery guide for VoIPmonitor MySQL/MariaDB. Four methods by invasiveness: (1) REPAIR TABLE for MyISAM only; (2) Transportable Tablespaces (RECOMMENDED for InnoDB .par corruption) - create fresh database, let VoIPmonitor generate schema, DISCARD tablespace, copy .ibd files, IMPORT tablespace; (3) Drop/recreate (destructive, last resort); (4) System table corruption (mysql.innodb_table_stats) - usually caused by disk space exhaustion, workaround with innodb_stats_persistent=0 or run mysql_upgrade. Key troubleshooting: schema mismatch requires manual ALTER TABLE from syslog; DISCARD fails check InnoDB engine and FOREIGN_KEY_CHECKS. Prevention: regular mysqldump, disk monitoring, proper shutdown, UPS, replication. | ||
'''Keywords:''' database corruption | '''Keywords:''' database corruption, repair table, .par file, .ibd file, transportable tablespaces, discard tablespace, import tablespace, InnoDB recovery, MariaDB corruption, MySQL corruption, data recovery, cdr table, mysqldump, innodb_table_stats, innodb_stats_persistent, system tables, disk space, mysql_upgrade | ||
'''Key Questions:''' | '''Key Questions:''' | ||
* How do I fix | * How do I fix "Failed to read from .par file" error? | ||
* How do I recover data from corrupted InnoDB table using | * How do I recover data from corrupted InnoDB table using transportable tablespaces? | ||
* How do I fix mysql.innodb_table_stats missing or corrupted? | * How do I fix mysql.innodb_table_stats missing or corrupted? | ||
* What causes MySQL system table corruption? | |||
* What | * When should I use mysql_upgrade vs transportable tablespaces? | ||
* When | |||
* How do I prevent database corruption in VoIPmonitor? | * How do I prevent database corruption in VoIPmonitor? | ||
* What backup strategy should I use for VoIPmonitor database? | |||
Latest revision as of 16:48, 8 January 2026
Guide for recovering corrupted database tables in MariaDB/MySQL. Methods are ordered from least to most invasive.
Quick Reference
| Symptom | Likely Cause | Recommended Method |
|---|---|---|
Failed to read from .par file |
Metadata corruption | Method 2: Transportable Tablespaces |
| REPAIR TABLE fails | InnoDB metadata damage | Method 2: Transportable Tablespaces |
| MyISAM table errors | MyISAM corruption | Method 1: REPAIR TABLE |
mysql.innodb_table_stats errors |
Disk space exhaustion | Method 4: System Tables |
| .ibd file corrupted/missing | Severe data corruption | Method 3: Drop/Recreate |
⚠️ Warning: Always backup your database before attempting recovery. Run mysqldump -u root -p voipmonitor > backup.sql first.
Method 1: REPAIR TABLE (MyISAM)
For MyISAM tables only (not common in VoIPmonitor):
REPAIR TABLE cdr;
ℹ️ Note: Most VoIPmonitor tables use InnoDB. Use Method 2 for InnoDB tables.
Method 2: Transportable Tablespaces (InnoDB)
Recommended for InnoDB when .par file is corrupted but .ibd data file is intact. Preserves all data.
Step-by-Step Procedure
1. Stop service and create recovery database:
systemctl stop voipmonitor
mysql -u root -p -e "CREATE DATABASE voipmonitorBackup;"
2. Configure VoIPmonitor to use new database temporarily:
# Edit /etc/voipmonitor.conf
mysqldb = voipmonitorBackup
3. Let VoIPmonitor create fresh schema:
systemctl start voipmonitor
sleep 60 # Wait for schema creation
systemctl stop voipmonitor
4. Discard tablespaces in new database:
USE voipmonitorBackup;
-- Discard main table and partitions
ALTER TABLE cdr DISCARD TABLESPACE;
-- For partitioned tables, discard each partition:
ALTER TABLE cdr DISCARD TABLESPACE FOR PARTITION p20250101;
-- Repeat for all partitions (check with SHOW CREATE TABLE cdr\G)
💡 Tip: Generate DISCARD commands automatically:
SELECT CONCAT('ALTER TABLE cdr DISCARD TABLESPACE FOR PARTITION ', PARTITION_NAME, ';') FROM INFORMATION_SCHEMA.PARTITIONS WHERE TABLE_SCHEMA='voipmonitorBackup' AND TABLE_NAME='cdr' AND PARTITION_NAME IS NOT NULL;
5. Copy .ibd files from old database:
# Copy data files
cp /var/lib/mysql/voipmonitor/cdr.ibd /var/lib/mysql/voipmonitorBackup/
cp /var/lib/mysql/voipmonitor/cdr#p#*.ibd /var/lib/mysql/voipmonitorBackup/
# Set ownership
chown mysql:mysql /var/lib/mysql/voipmonitorBackup/cdr*.ibd
6. Import tablespaces:
USE voipmonitorBackup;
ALTER TABLE cdr IMPORT TABLESPACE;
ALTER TABLE cdr IMPORT TABLESPACE FOR PARTITION p20250101;
-- Repeat for all partitions
7. Verify and finalize:
SELECT COUNT(*) FROM cdr;
SELECT MIN(calldate), MAX(calldate) FROM cdr;
Update /etc/voipmonitor.conf to use voipmonitorBackup permanently, then start service:
systemctl start voipmonitor
Method 3: Drop and Recreate (Destructive)
⚠️ Warning: Use ONLY when .ibd files are corrupted and you have a backup or data loss is acceptable.
systemctl stop voipmonitor
USE voipmonitor;
DROP TABLE IF EXISTS cdr;
-- Get fresh schema from GUI: Tools > System Status > Check MySQL Schema
CREATE TABLE `cdr` ( ... );
systemctl start voipmonitor
Method 4: System Table Corruption
Applies when MySQL logs errors about mysql.innodb_table_stats, mysql.column_stats, or similar system tables. Usually caused by disk space exhaustion.
Diagnosis
# Check disk space (common root cause)
df -h /var/lib/mysql
# Check MySQL error log
tail -100 /var/log/mysql/error.log
Quick Workaround
Add to /etc/mysql/my.cnf under [mysqld]:
innodb_stats_persistent = 0
systemctl restart mysql
Permanent Fix
Option A: Run mysql_upgrade (after OS/MySQL version upgrades):
mysql_upgrade -u root -p
systemctl restart mysql
Option B: Full database reinitialize (if Option A fails):
# 1. Backup everything
mysqldump -u root -p --all-databases > full_backup.sql
# 2. Stop and reinitialize
systemctl stop mysql
mv /var/lib/mysql /var/lib/mysql-corrupted
mysql_install_db --user=mysql # or: mysqld --initialize --user=mysql
# 3. Start and restore
systemctl start mysql
mysql -u root -p < full_backup.sql
Troubleshooting
| Error | Solution |
|---|---|
| Schema mismatch during IMPORT | Check journalctl -u voipmonitor for ALTER TABLE commands and run them manually
|
| Cannot DISCARD TABLESPACE | Verify InnoDB engine: SELECT ENGINE FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME='cdr';Try: SET FOREIGN_KEY_CHECKS=0; before DISCARD
|
| DROP TABLE hangs | Stop MariaDB, delete files manually from /var/lib/mysql/voipmonitor/, restart
|
Prevention
- Regular backups:
mysqldump -u root -p voipmonitor | gzip > backup_$(date +%F).sql.gz - Monitor disk space: Alert when MySQL partition > 80% full
- Clean shutdown: Always use
systemctl stopbefore maintenance - UPS: Prevent sudden power loss
- Replication: See Redundant Database
Automated Backup Example
#!/bin/bash
# /usr/local/bin/backup_voipmonitor_db.sh
BACKUP_DIR="/var/backups/voipmonitor"
mkdir -p $BACKUP_DIR
mysqldump -u root -p"$MYSQL_PASSWORD" voipmonitor | gzip > "$BACKUP_DIR/voipmonitor_$(date +%F).sql.gz"
find $BACKUP_DIR -name "*.sql.gz" -mtime +7 -delete
Cron: 0 2 * * * /usr/local/bin/backup_voipmonitor_db.sh
See Also
AI Summary for RAG
Summary: Database table corruption recovery guide for VoIPmonitor MySQL/MariaDB. Four methods by invasiveness: (1) REPAIR TABLE for MyISAM only; (2) Transportable Tablespaces (RECOMMENDED for InnoDB .par corruption) - create fresh database, let VoIPmonitor generate schema, DISCARD tablespace, copy .ibd files, IMPORT tablespace; (3) Drop/recreate (destructive, last resort); (4) System table corruption (mysql.innodb_table_stats) - usually caused by disk space exhaustion, workaround with innodb_stats_persistent=0 or run mysql_upgrade. Key troubleshooting: schema mismatch requires manual ALTER TABLE from syslog; DISCARD fails check InnoDB engine and FOREIGN_KEY_CHECKS. Prevention: regular mysqldump, disk monitoring, proper shutdown, UPS, replication.
Keywords: database corruption, repair table, .par file, .ibd file, transportable tablespaces, discard tablespace, import tablespace, InnoDB recovery, MariaDB corruption, MySQL corruption, data recovery, cdr table, mysqldump, innodb_table_stats, innodb_stats_persistent, system tables, disk space, mysql_upgrade
Key Questions:
- How do I fix "Failed to read from .par file" error?
- How do I recover data from corrupted InnoDB table using transportable tablespaces?
- How do I fix mysql.innodb_table_stats missing or corrupted?
- What causes MySQL system table corruption?
- When should I use mysql_upgrade vs transportable tablespaces?
- How do I prevent database corruption in VoIPmonitor?
- What backup strategy should I use for VoIPmonitor database?