错误的MySQL更新查询后恢复?

时间:2022-06-01 20:01:24

I made a wrong update query in my table.

我在表格中做了一个错误的更新查询。

I forgot to make an id field in the WHERE clause.

我忘了在WHERE子句中创建一个id字段。

So that updated all my rows.

所以更新了我的所有行。

How to recover that?

如何恢复?

I didn't have a backup....

我没有备份....

4 个解决方案

#1


10  

Sorry man, but the chances of restoring an overwritten MySQL database are usually close to zero. Different from deleting a file, overwriting a record actually and physically overwrites the existing data in most cases.

对不起,但恢复被覆盖的MySQL数据库的几率通常接近于零。与删除文件不同,在大多数情况下,覆盖记录实际上会物理覆盖现有数据。

To be prepared if anything comes up here, you should stop your MySQL server, and make a copy of the physical directory containing the database so nothing can get overwritten further: A simple copy+paste of the data folder to a different location should do.

要做好准备,如果有任何问题,你应该停止你的MySQL服务器,并制作一个包含数据库的物理目录的副本,这样就不会有任何进一步的覆盖:将数据文件夹的简单复制+粘贴到另一个位置应该这样做。

But don't get your hopes up - I think there's nothing that can be done really.

但是不要抱有希望 - 我认为没有什么可以做到的。

You may want to set up a frequent database backup for the future. There are many solutions around; one of the simplest, most reliable and easiest to automate (using at or cron in Linux, or the task scheduler in Windows) is MySQL's own mysqldump.

您可能希望为将来设置频繁的数据库备份。有很多解决方案;最简单,最可靠和最容易自动化的一个(在Linux中使用at或cron,或在Windows中使用任务调度程序)是MySQL自己的mysqldump。

#2


13  

There are two lessons to be learned here:

这里有两个教训:

  1. Backup data
  2. Perform UPDATE/DELETE statements within a transaction, so you can use ROLLBACK if things don't go as planned
  3. 在事务中执行UPDATE / DELETE语句,因此如果事情没有按计划进行,则可以使用ROLLBACK

Being aware of the transaction (autocommit, explicit and implicit) handling for your database can save you from having to restore data from a backup.

了解数据库的事务(自动提交,显式和隐式)处理可以使您不必从备份还原数据。

Transactions control data manipulation statement(s) to ensure they are atomic. Being "atomic" means the transaction either occurs, or it does not. The only way to signal the completion of the transaction to database is by using either a COMMIT or ROLLBACK statement (per ANSI-92, which sadly did not include syntax for creating/beginning a transaction so it is vendor specific). COMMIT applies the changes (if any) made within the transaction. ROLLBACK disregards whatever actions took place within the transaction - highly desirable when an UPDATE/DELETE statement does something unintended.

事务控制数据操作语句以确保它们是原子的。 “原子”意味着交易要么发生,要么不发生。向数据库发出事务完成信号的唯一方法是使用COMMIT或ROLLBACK语句(根据ANSI-92,遗憾的是不包括创建/开始事务的语法,因此它是特定于供应商的)。 COMMIT应用在事务中进行的更改(如果有)。 ROLLBACK忽略了事务中发生的任何动作 - 当UPDATE / DELETE语句无意中发生某些事情时非常需要。

Typically individual DML (Insert, Update, Delete) statements are performed in an autocommit transaction - they are committed as soon as the statement successfully completes. Which means there's no opportunity to roll back the database to the state prior to the statement having been run in cases like yours. When something goes wrong, the only restoration option available is to reconstruct the data from a backup (providing one exists). In MySQL, autocommit is on by default for InnoDB - MyISAM doesn't support transactions. It can be disabled by using:

通常,单个DML(插入,更新,删除)语句在自动提交事务中执行 - 它们在语句成功完成后立即提交。这意味着在像您这样的情况下运行语句之前,没有机会将数据库回滚到状态。当出现问题时,唯一可用的恢复选项是从备份重建数据(提供一个存在)。在MySQL中,InnoDB默认启用自动提交 - MyISAM不支持事务。可以使用以下方法禁用它:

SET autocommit = 0

An explicit transaction is when statement(s) are wrapped within an explicitly defined transaction code block - for MySQL, that's START TRANSACTION. It also requires an explicitly made COMMIT or ROLLBACK statement at the end of the transaction. Nested transactions is beyond the scope of this topic.

显式事务是将语句包装在显式定义的事务代码块中 - 对于MySQL,即START TRANSACTION。它还需要在事务结束时显式生成COMMIT或ROLLBACK语句。嵌套事务超出了本主题的范围。

Implicit transactions are slightly different from explicit ones. Implicit transactions do not require explicity defining a transaction. However, like explicit transactions they require a COMMIT or ROLLBACK statement to be supplied.

隐式事务与显式事务略有不同。隐式事务不需要明确定义事务。但是,与显式事务一样,它们需要提供COMMIT或ROLLBACK语句。

Conclusion

Explicit transactions are the most ideal solution - they require a statement, COMMIT or ROLLBACK, to finalize the transaction, and what is happening is clearly stated for others to read should there be a need. Implicit transactions are OK if working with the database interactively, but COMMIT statements should only be specified once results have been tested & thoroughly determined to be valid.

显式事务是最理想的解决方案 - 它们需要语句,COMMIT或ROLLBACK来完成事务,并且如果有需要,其他人可以清楚地说明正在发生的事情。如果以交互方式使用数据库,则隐式事务是可以的,但只有在测试结果并彻底确定有效后才应指定COMMIT语句。

That means you should use:

这意味着你应该使用:

SET autocommit = 0;START TRANSACTION;  UPDATE ...;

...and only use COMMIT; when the results are correct.

...并且只使用COMMIT;当结果是正确的。

That said, UPDATE and DELETE statements typically only return the number of rows affected, not specific details. Convert such statements into SELECT statements & review the results to ensure correctness prior to attempting the UPDATE/DELETE statement.

也就是说,UPDATE和DELETE语句通常只返回受影响的行数,而不是特定的详细信息。在尝试UPDATE / DELETE语句之前,将这些语句转换为SELECT语句并查看结果以确保正确性。

Addendum

DDL (Data Definition Language) statements are automatically committed - they do not require a COMMIT statement. IE: Table, index, stored procedure, database, and view creation or alteration statements.

DDL(数据定义语言)语句是自动提交的 - 它们不需要COMMIT语句。 IE:表,索引,存储过程,数据库和视图创建或更改语句。

#3


3  

Sorry to say that, but there is no way to restore the old field values without a backup.

很抱歉这样说,但没有备份就无法恢复旧的字段值。

Don't shoot the messenger...

不要射击信使......

#4


0  

Do you have binlogs enabled? You can recover by accessing the binlogs.

你有binlogs吗?您可以通过访问binlog来恢复。

#1


10  

Sorry man, but the chances of restoring an overwritten MySQL database are usually close to zero. Different from deleting a file, overwriting a record actually and physically overwrites the existing data in most cases.

对不起,但恢复被覆盖的MySQL数据库的几率通常接近于零。与删除文件不同,在大多数情况下,覆盖记录实际上会物理覆盖现有数据。

To be prepared if anything comes up here, you should stop your MySQL server, and make a copy of the physical directory containing the database so nothing can get overwritten further: A simple copy+paste of the data folder to a different location should do.

要做好准备,如果有任何问题,你应该停止你的MySQL服务器,并制作一个包含数据库的物理目录的副本,这样就不会有任何进一步的覆盖:将数据文件夹的简单复制+粘贴到另一个位置应该这样做。

But don't get your hopes up - I think there's nothing that can be done really.

但是不要抱有希望 - 我认为没有什么可以做到的。

You may want to set up a frequent database backup for the future. There are many solutions around; one of the simplest, most reliable and easiest to automate (using at or cron in Linux, or the task scheduler in Windows) is MySQL's own mysqldump.

您可能希望为将来设置频繁的数据库备份。有很多解决方案;最简单,最可靠和最容易自动化的一个(在Linux中使用at或cron,或在Windows中使用任务调度程序)是MySQL自己的mysqldump。

#2


13  

There are two lessons to be learned here:

这里有两个教训:

  1. Backup data
  2. Perform UPDATE/DELETE statements within a transaction, so you can use ROLLBACK if things don't go as planned
  3. 在事务中执行UPDATE / DELETE语句,因此如果事情没有按计划进行,则可以使用ROLLBACK

Being aware of the transaction (autocommit, explicit and implicit) handling for your database can save you from having to restore data from a backup.

了解数据库的事务(自动提交,显式和隐式)处理可以使您不必从备份还原数据。

Transactions control data manipulation statement(s) to ensure they are atomic. Being "atomic" means the transaction either occurs, or it does not. The only way to signal the completion of the transaction to database is by using either a COMMIT or ROLLBACK statement (per ANSI-92, which sadly did not include syntax for creating/beginning a transaction so it is vendor specific). COMMIT applies the changes (if any) made within the transaction. ROLLBACK disregards whatever actions took place within the transaction - highly desirable when an UPDATE/DELETE statement does something unintended.

事务控制数据操作语句以确保它们是原子的。 “原子”意味着交易要么发生,要么不发生。向数据库发出事务完成信号的唯一方法是使用COMMIT或ROLLBACK语句(根据ANSI-92,遗憾的是不包括创建/开始事务的语法,因此它是特定于供应商的)。 COMMIT应用在事务中进行的更改(如果有)。 ROLLBACK忽略了事务中发生的任何动作 - 当UPDATE / DELETE语句无意中发生某些事情时非常需要。

Typically individual DML (Insert, Update, Delete) statements are performed in an autocommit transaction - they are committed as soon as the statement successfully completes. Which means there's no opportunity to roll back the database to the state prior to the statement having been run in cases like yours. When something goes wrong, the only restoration option available is to reconstruct the data from a backup (providing one exists). In MySQL, autocommit is on by default for InnoDB - MyISAM doesn't support transactions. It can be disabled by using:

通常,单个DML(插入,更新,删除)语句在自动提交事务中执行 - 它们在语句成功完成后立即提交。这意味着在像您这样的情况下运行语句之前,没有机会将数据库回滚到状态。当出现问题时,唯一可用的恢复选项是从备份重建数据(提供一个存在)。在MySQL中,InnoDB默认启用自动提交 - MyISAM不支持事务。可以使用以下方法禁用它:

SET autocommit = 0

An explicit transaction is when statement(s) are wrapped within an explicitly defined transaction code block - for MySQL, that's START TRANSACTION. It also requires an explicitly made COMMIT or ROLLBACK statement at the end of the transaction. Nested transactions is beyond the scope of this topic.

显式事务是将语句包装在显式定义的事务代码块中 - 对于MySQL,即START TRANSACTION。它还需要在事务结束时显式生成COMMIT或ROLLBACK语句。嵌套事务超出了本主题的范围。

Implicit transactions are slightly different from explicit ones. Implicit transactions do not require explicity defining a transaction. However, like explicit transactions they require a COMMIT or ROLLBACK statement to be supplied.

隐式事务与显式事务略有不同。隐式事务不需要明确定义事务。但是,与显式事务一样,它们需要提供COMMIT或ROLLBACK语句。

Conclusion

Explicit transactions are the most ideal solution - they require a statement, COMMIT or ROLLBACK, to finalize the transaction, and what is happening is clearly stated for others to read should there be a need. Implicit transactions are OK if working with the database interactively, but COMMIT statements should only be specified once results have been tested & thoroughly determined to be valid.

显式事务是最理想的解决方案 - 它们需要语句,COMMIT或ROLLBACK来完成事务,并且如果有需要,其他人可以清楚地说明正在发生的事情。如果以交互方式使用数据库,则隐式事务是可以的,但只有在测试结果并彻底确定有效后才应指定COMMIT语句。

That means you should use:

这意味着你应该使用:

SET autocommit = 0;START TRANSACTION;  UPDATE ...;

...and only use COMMIT; when the results are correct.

...并且只使用COMMIT;当结果是正确的。

That said, UPDATE and DELETE statements typically only return the number of rows affected, not specific details. Convert such statements into SELECT statements & review the results to ensure correctness prior to attempting the UPDATE/DELETE statement.

也就是说,UPDATE和DELETE语句通常只返回受影响的行数,而不是特定的详细信息。在尝试UPDATE / DELETE语句之前,将这些语句转换为SELECT语句并查看结果以确保正确性。

Addendum

DDL (Data Definition Language) statements are automatically committed - they do not require a COMMIT statement. IE: Table, index, stored procedure, database, and view creation or alteration statements.

DDL(数据定义语言)语句是自动提交的 - 它们不需要COMMIT语句。 IE:表,索引,存储过程,数据库和视图创建或更改语句。

#3


3  

Sorry to say that, but there is no way to restore the old field values without a backup.

很抱歉这样说,但没有备份就无法恢复旧的字段值。

Don't shoot the messenger...

不要射击信使......

#4


0  

Do you have binlogs enabled? You can recover by accessing the binlogs.

你有binlogs吗?您可以通过访问binlog来恢复。