To learn more about what is binary log and how to setup Click here
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