Home > Database > Mysql Tutorial > mysql restore accidentally deleted data

mysql restore accidentally deleted data

WBOY
Release: 2023-05-20 12:14:38
Original
7215 people have browsed it

Data is one of the most important assets in modern society. Many businesses and individuals rely on databases to save and manage their valuable data. However, no matter how careful you are, accidentally deleting data will still happen. If you are a user of MySQL database, you may be confused and worried about whether accidentally deleted data can be restored. But if you know the correct recovery methods, you can recover your MySQL data as quickly as possible.

This article will introduce some common MySQL data recovery techniques to help you recover deleted data in unexpected circumstances.

  1. Understand the basic knowledge of MySQL data recovery

Before using MySQL to recover data, you need to understand some basic terms and concepts.

In MySQL, each table has a related file. This file holds the table's data and metadata. When you execute the DELETE or DROP command, MySQL deletes the related files. However, not all data is permanently deleted immediately. MySQL will mark deleted data as free space so that it can be overwritten in the future. Before being overwritten, the data can still be recovered.

Another thing to note is that if your MySQL database is running, do not restart MySQL. Restarting the MySQL instance causes MySQL to open all table files for new data. If this happens, you will lose the chance to recover the deleted data.

  1. Using MySQL's rollback function

MySQL provides a rollback function that can undo submitted INSERT, UPDATE, and DELETE statements. Rollback is one of the safest and easiest recovery methods. If you are just in a debugging and testing environment, or you just need to recover recently deleted data, you can try using the rollback feature.

Please note that rollback only applies to transactional tables, that is, tables whose storage engine is InnoDB.

To use the MySQL rollback function, please perform the following steps:

  • Set the table as a transactional table

To use the rollback function, first The table needs to be set up as transactional. To set up a transactional table, use the following command:

ALTER TABLE table_name ENGINE = InnoDB;
Copy after login
  • Start a transaction

Before executing any INSERT, UPDATE, or DELETE statement, you need to start a transaction. To start a transaction, use the following command:

START TRANSACTION;
Copy after login
  • Execute a change statement

Execute an INSERT, UPDATE, or DELETE statement. If you want to roll back all changes then execute the following command:

ROLLBACK;
Copy after login
  • Commit changes

If you have completed all changes and want to save the changes to the database, Please execute the following command:

COMMIT;
Copy after login
  1. Use MySQL's binlog to achieve data recovery

If you cannot use the rollback function, you can try to use MySQL's binlog to achieve data recovery. Binlog is the binary log used by MySQL to record database change operations.

When executing an INSERT, UPDATE, or DELETE statement, MySQL will record the changes to the binlog. These change logs are recorded in chronological order and stored in binary form. To use binlog to achieve data recovery, you need to know the table name of the deleted data, the deletion time and the binlog file name and location.

To use binlog to achieve data recovery, please perform the following steps:

  • Find the binlog file

To find the binlog file with the deletion time, please use The following command:

SHOW BINARY LOGS;
Copy after login

This command will display the names and locations of all binlog files. You need to find the binlog file containing the accidentally deleted statement among these files.

  • Restore data

To restore data using the binlog file, use the following command:

mysqlbinlog [binlog_file_name] | mysql -u [user_name] -p [database_name]
Copy after login

This command will generate SQL for all changes in the binlog file statements and then apply those statements to the specified database. You can create a new database before executing this command to avoid overwriting any existing data.

  1. Using MySQL's data recovery tools

If you cannot achieve data recovery using rollback or binlog, MySQL also provides other data recovery tools. These tools can scan the free space for deleted data and try to recover them into new files or databases.

  • Using the MyISAMchk tool for MySQL

If you are using the MyISAM engine, you can use the MyISAMchk tool to check and repair damaged tables. MyISAMchk can find blocks of deleted data and restore them to new files. To use MyISAMchk, perform the following steps:

  1. Stop the MySQL service
  2. Copy the table files to the temporary directory
cp [table_file_name] [path/to/new_table_file]
Copy after login
  1. Scan using MyISAMchk Table file
myisamchk --recover [path/to/new_table_file]
Copy after login
  1. Copy the file to the original location
cp [path/to/new_table_file] [original_file_path]
Copy after login
  1. Restart the MySQL service
  • Use third-party data recovery tools

If you cannot use MySQL's own tools to recover data, you can try to use some third-party data recovery tools. Typical examples are TestDisk and PhotoRec. These tools can scan the disk for deleted data and files and attempt to recover them into new files or databases.

It should be noted that using third-party tools to recover data may result in data loss or system crash. It's a good idea to back up all your data before using these tools.

Summarize

MySQL data recovery technology is a complex process that requires special attention and caution. No matter which method you use, make sure you back up all your data before executing any commands. Rollback is the safest data recovery method, but only applies to transactional tables. If you are unable to use rollback, you can try using MySQL's binlog for advanced data recovery. If none of these methods work, you may consider using MySQL's data recovery tool or a third-party data recovery tool.

The above is the detailed content of mysql restore accidentally deleted data. For more information, please follow other related articles on the PHP Chinese website!

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