Restore MySQL database from binary log

To learn more about what is binary log and how to setup Click here

MySQL binary log : mysqlbinlog utility

mysqldump – MySQL Database Backup and restore program

Purge Binary log

MySQL Master-Master-Slave-Slave Replication


Database Recover from MySQL binary log:

  • Binary logs store all the queries (in STATEMENT format) or row changes (in ROW format) that happened since they were recorded.
  • Basically executes all insert, updates & deletes that occurred to the server during those binlogs, serially. That is why recovering from the binary logs is only useful for point in time recovery (recovering the latest changes since the last backup).
  • If the goal is to have a path to recovering the database, then consider using Replication and having a Slave. The Slave will usually be up to the minute with the Master. This is far better.

Using mysqlbinlog utility we can view the binlog file content and recover the database.

If it’s a single file you can recover using:

mysqlbinlog /var/lib/mysql-bin.000016 | mysql –uroot –pPassword
If it’s multiple files then just extract all content to one .sql file and directly restore it:

mysqlbinlog /var/lib/mysql-bin.000016 > /logs/allbinlog.sql

To append second binlog content to allbinlof.sql file use below command:

mysqlbinlog /var/lib/mysql-bin.000016 >> /logs/allbinlog.sql

 

Database Recover from MySQL binary log with start date and time:

Let’s suppose we have a file called mysql-binlog.000111. There are a number of ways this can be exported, based on your needs, and those choices are available via command line flags (man mysqlbinlog for details).

Let’s say you need data from a certain start time up to a certain end time. This can be specified on the command line. You can also specify the database that you wish to export the data for. This is because if you run multiple databases, they are all logged to the same set of binlogs.

mysqlbinlog –start-datetime=”2016-05-01 05:00:00” –stop-datetime=”2016-05-01 08:05:00” –database mydatabase mysql-binlog.000111 > output.txt

That command will pull all of the transactions from the mysql-binlog.000111 file for the ‘mydatabase’ database starting from the specified data and output them to the output.txt file.

Now you can restore this to the database:

mysql backupdatabase < output.txt

 

 

Satya Prakash

VOIP Expert: More than 8 years of experience in Asterisk Development and Call Center operation Management. Unique Combination of Skill Set as IT, Analytics and operation management.

One thought on “Restore MySQL database from binary log

  • June 25, 2016 at 12:43 am
    Permalink

    Fine post. I learn something more challenging on different blogs everyday.
    It’ll constantly be provoking to read content from other writers and practice a little something from their
    store. I’d prefer to use some with the content on my blog whether you do’t mind.

    Natually I’ll give you a link on your internet blog. Thanks for sharing.

Leave a Reply