Home Database Mysql Tutorial 如何使用MYSQL数据库进行备份数据恢复

如何使用MYSQL数据库进行备份数据恢复

Jun 07, 2016 pm 04:16 PM
mysql use backup how database Data Recovery conduct

数据库毁坏发生的原因有许多,且程度各不相同。如果幸运的话,可能是一两个表的小毁坏(例如,如果您的机器由于断电而暂时停机)。如果不是这 样,可能需要置换整个的数据目录(例如,如果某个磁盘瘫痪而且数据目录在它上)。在其他情况下也需要恢复操作,例

  数据库毁坏发生的原因有许多,且程度各不相同。如果幸运的话,可能是一两个表的小毁坏(例如,如果您的机器由于断电而暂时停机)。如果不是这 样,可能需要置换整个的数据目录(例如,如果某个磁盘瘫痪而且数据目录在它上)。在其他情况下也需要恢复操作,例如,当用户错误地删除数据库或表时,或者 错误地删除表的内容时。不论这些不幸的事件发生是由于什么原因,都需要恢复它们。

  如果表被毁坏但没有丢失,可试着用myisamchk 或isamchk 来修复它们。如果修复实用程序能修复它们,就根本没有必要使用备份文件。如果表被丢失或不能修复,则需要恢复它们。

  恢复过程包括两个信息源:备份文件和更新日志。备份文件将表恢复到进行该备份时的状态。但是,在备份和故障发生这段时间中,表通常已经被修改。 更新日志包含了用来完成这些修改的查询。可以通过将更新日志作为对mysql的输入来重复这些查询(这就是为什么需要更新日志的原因。如果您还没有使更新 日志有效,现在赶快做,并在进一步读取之前生成一个新的备份)。

  恢复过程根据必须恢复的信息的多少而变化。事实上,恢复整个数据库比恢复单个的表要容易,因为对数据库应用更新日志比对表要容易。

  恢复整个数据库

  首先,如果要恢复的数据库是含有授权表的mysql数据库,将需要使用--skip-grant-tables选项运行服务器。否则,服务器将 抱怨无法找到授权表。在恢复表之后,执行mysqladmin flush-privileges 来告诉服务器加载授权表,并用它们启动。

  将原数据库目录的内容拷贝到其他的地方。例如,您可能会在稍后用它们进行崩溃表的事后分析检查(post-mortem examination)。

  用最新的备份文件重新加载数据库。如果您打算使用由mysqldump 加载的文件,则需要将它们作为mysql的输入。如果打算使用从数据库中直接拷贝的文件(如,用tar 或c p),则将它们直接拷贝回到该数据库目录中。但是,在这种情况下,应该在拷贝这些文件之前关闭服务器,然后再重新启动它。

  用更新日志重做在进行备份后又修改了数据库表的查询。对于所有可用的更新日志,可使用它作为mysql的输入。指定--one- database 选项,使mysql只对想要恢复的数据库执行查询。如果您知道需要使用所有的更新日志文件,可在包含日志的目录中使用下列命令:

  % ls-t-r-l update.(0-9)* | xargs cat | mysql--one-database db_name

  ls 命令产生更新日志文件的单列列表,更新日志文件根据服务器生成的顺序进行排序(要知道,如果您修改了其中的任何文件,排序的顺序都将改变,这将导致更新日志按错误的顺序使用)。

  您很可能必须使用某些更新日志。例如,如果自备份以来所产生的日志命名为update.392、pdate.393 等等,可以重新运行它们中的命令: % mysql--one-database db_name

  如果正在运行恢复并打算使用更新日志恢复由于失策的DROP DATA BASE、DROPTABLE或DELETE 语句而丢失的信息,应确保先从更新日志中删除这些语句。

  恢复单个的表

  恢复单个表是很困难的。如果有通过mysqldump 生成的备份文件并且它恰好不包含您想要的表数据,则需要抽取相关的行并用它们作为mysql的输入,这部分较容易。困难的是抽取应用于该表的更新日志的片 段。您会发现: mysql_find_rows 实用程序对这方面有帮助,它可以从更新日志中抽取多行查询。

  另一种可能性是用另一个服务器恢复整个数据库,然后将所要的该表的文件拷贝到原始数据库中。这实际很容易!在将文件拷贝回数据库目录时,应确保原始数据库的服务器关闭。

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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use MySQL backup and restore in PHP? How to use MySQL backup and restore in PHP? Jun 03, 2024 pm 12:19 PM

Backing up and restoring a MySQL database in PHP can be achieved by following these steps: Back up the database: Use the mysqldump command to dump the database into a SQL file. Restore database: Use the mysql command to restore the database from SQL files.

How to optimize MySQL query performance in PHP? How to optimize MySQL query performance in PHP? Jun 03, 2024 pm 08:11 PM

MySQL query performance can be optimized by building indexes that reduce lookup time from linear complexity to logarithmic complexity. Use PreparedStatements to prevent SQL injection and improve query performance. Limit query results and reduce the amount of data processed by the server. Optimize join queries, including using appropriate join types, creating indexes, and considering using subqueries. Analyze queries to identify bottlenecks; use caching to reduce database load; optimize PHP code to minimize overhead.

How to insert data into a MySQL table using PHP? How to insert data into a MySQL table using PHP? Jun 02, 2024 pm 02:26 PM

How to insert data into MySQL table? Connect to the database: Use mysqli to establish a connection to the database. Prepare the SQL query: Write an INSERT statement to specify the columns and values ​​to be inserted. Execute query: Use the query() method to execute the insertion query. If successful, a confirmation message will be output.

How to create a MySQL table using PHP? How to create a MySQL table using PHP? Jun 04, 2024 pm 01:57 PM

Creating a MySQL table using PHP requires the following steps: Connect to the database. Create the database if it does not exist. Select a database. Create table. Execute the query. Close the connection.

How to fix mysql_native_password not loaded errors on MySQL 8.4 How to fix mysql_native_password not loaded errors on MySQL 8.4 Dec 09, 2024 am 11:42 AM

One of the major changes introduced in MySQL 8.4 (the latest LTS release as of 2024) is that the "MySQL Native Password" plugin is no longer enabled by default. Further, MySQL 9.0 removes this plugin completely. This change affects PHP and other app

iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos Jul 18, 2024 am 05:48 AM

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

Detailed tutorial on establishing a database connection using MySQLi in PHP Detailed tutorial on establishing a database connection using MySQLi in PHP Jun 04, 2024 pm 01:42 PM

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

How to handle database connection errors in PHP How to handle database connection errors in PHP Jun 05, 2024 pm 02:16 PM

To handle database connection errors in PHP, you can use the following steps: Use mysqli_connect_errno() to obtain the error code. Use mysqli_connect_error() to get the error message. By capturing and logging these error messages, database connection issues can be easily identified and resolved, ensuring the smooth running of your application.

See all articles