Home Database Mysql Tutorial mysql二进制日志文件恢复数据库

mysql二进制日志文件恢复数据库

Jun 07, 2016 pm 04:28 PM
mysql binary recover database document log

二进制日志的文件的作用 mysql二进制日志文件用来记录所有用户对数据库操作,即记录用户对数据库操作的sql语句。如果有此文件,当数据库发生意外时,可以通过此文件查看到用户在此文件记录的时间段内用户所做的操作,再和数据库备份配合使用,即可再现用户操

二进制日志的文件的作用

    mysql二进制日志文件用来记录所有用户对数据库操作,即记录用户对数据库操作的sql语句。如果有此文件,当数据库发生意外时,可以通过此文件查看到用户在此文件记录的时间段内用户所做的操作,再和数据库备份配合使用,即可再现用户操作,使数据库恢复。

二进制日志文件的弊端

二进制日志文件开启后,所有对数据库操作的记录均会被记录到此文件,

所以,当长时间开启之后,日志文件会变得很大,占用磁盘空间。

使用二进制日志文件恢复数据库

开启日志文件

mysql默认是不开启日志文件的功能的,需要我们手动开启。操作如下:

1、  打开mysql安装目录\my.ini(windows系统是my.ini文件,linux是my.cnf文件)。

2、  找到[mysqld]这个标签,在此标签下面一行,添加语句:log-bin=binary-log。上面语句中,log-bin说明要开启二进制日志文件,binary-log是二进制日志文件的名字。

(测试的时候我设置的:log-bin=d:/mysql_log/logbin_ouyang.log)

3、  重启mysql服务。可以在mysql安装目录\data 文件夹下看到“binary-log.数字编号”文件,

   如binary-log.000001。以后每重启一次mysql服务,都会重新生成二进制日志文件,

   文件名中的数字编号依次递增。

(你的上一步给出的文件名,加了个序号 还有一个总的文件 index文件 如图1:)

 

恢复数据库

使用日志文件恢复数据库必须要有一个数据库备份(只要是在二进制日志文件功能开启之后备份的就行)。恢复数据库时,可以通过时间恢复,也可以通过操作点恢复。

1、通过时间恢复方法如下:

如果我在2011-04-02 00:00:00时刻开启了二进制日志文件功能,并备份了数据库文件。且在2011-04-02 12:00:00数据库发生异常,需要恢复到2011-04-02 10:00:00。则操作如下:

l         用备份将数据库回复到2011-04-02 00:00:00时候的状态。

l         在命令行输入如下命令:

 

mysqlbinlog --stop-date="你想要恢复的时间"  日志文件的目录\binary-log.000001 | mysql -u root –p

(D:\mysql_log>mysqlbinlog logbin_ouyang.000007 --stop-date="2011-10-23 15:05:00"|mysql -uroot -proot)

Copy after login

这样,系统会自动执行从二进制日志有记录开始截止到2011-04-02 10:00:00,用户所做过的所有操作。与stop-date对应的,还有start-date属性,可以设定执行记录的开始时间。也可以两个属性都设置。

2、通过操作点恢复方法如下:

在命令行中输入mysqlbinlog D:\binary-log.000003 > D:\log.txt,执行后打开log.txt,

查看里面语句,可以发现:在每个操作之前,都会有一个独特的编号,如下红字显示:

/*!*/;

# at 450 /*编号450*/

#110402 15:31:50 server id 1 end_log_pos 529     Query    thread_id=2   exec_time=0  error_code=0

SET TIMESTAMP=1301729510/*!*/;

DROP TABLE `jwc`

Copy after login

此编号随着操作数增多而变大。和时间一样是一个标记,通过操作点恢复的语句如下:

mysqlbinlog --stop-position="450" mysql安装目录\data\binary-log.000001 | mysql -u root –p

这样,系统会自动执行二进制日志中编号最低的语句到编号450语句。

与stop-position对应的,还有start-position属性,可以设定执行记录的开始编号。

附:(你应该看看)

1.0 mysqlbinlog 是mysql自带的一个日志操作工具!

2.0 如果直接打开log文件 可能会是乱码,所以我们一般会:

D:\mysql_log>mysqlbinlog logbin_ouyang.000003 >log_3.txt 复制下 然后用记事本打开!

就可以清楚的看到 操作的编号了 或者说(行号)

3.0 如果发生了错误的操作!如果可以的话!你应该立马停止数据库!然后恢复数据!

4.0 如果只是去掉某一个语句,你可以选择恢复两次 ,如图:

D:\mysql_log>mysqlbinlog logbin_ouyang.000009 --stop-position=10625|mysql -uroot
     -proot

   D:\mysql_log>mysqlbinlog logbin_ouyang.000009 --start-position=10843|mysql -uroo

     t –proot
Copy after login

5.0 如果发生 Could not read entry at offset 1478: Error in log format or read error. 类似的错误!

   请检查你的编号是否正确!注意这个编号 不是一些编辑器显示的那个行号哦,# at 450 /

经常delete 、update错了!所有给自己个文档!当然我也希望你永远的用不上这个文档 ! 周末愉快! (排版有的乱哈!多多包涵!)

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 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 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 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 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 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 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

How to use MySQL stored procedures in PHP? How to use MySQL stored procedures in PHP? Jun 02, 2024 pm 02:13 PM

To use MySQL stored procedures in PHP: Use PDO or the MySQLi extension to connect to a MySQL database. Prepare the statement to call the stored procedure. Execute the stored procedure. Process the result set (if the stored procedure returns results). Close the database connection.

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.

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())

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

See all articles