How to Read MySQL Binary Log Files (BinLog) with mysqlbinlog

MySQL database server generates binary log files for every transaction to the databases, provided administrator does not disable or comment out the “log-bin” parameter in my.cny configuration file. The binary log files are written in binary format. Although the binary logs, or also known as logbin are mainly used for MySQL database replication purpose, sometimes you may need to examine or read the contents of binary logs in text format, where the mysqlbinlog utility will come in handy.

Binary log file, which normally has name with the format host_name-bin.xxxxxx and store in /var/lib/mysql directory, could not be opened and read straight away as it’s in unreadable binary format. To read the binary logs in text format, we can make use of mysqlbinlog command, which also able to readrelay log files written by a slave server in a replication setup. Relay logs have the same format as binary log files.

To use mysqlbinlog utility is simple, simply use the following command syntax to invoke mysqlbinlog after login in as root (else you have to specify user name and password) to shell via SSH:

mysqlbinlog [options] log_file ...

So to read and display the contents of the binary log file named binlog.000001, use this command:

mysqlbinlog binlog.000001

The binary log files and its data are likely to be very huge, thus making it almost impossible to read anything on screen. However, you can pipe the output of mysqlbinlog into a file which can be open up for later browsing in text editor, by using the following command:

mysqlbinlog binlog.000001 > filename.txt

To reduce the amount of data retrieved from binary logs, there are several options that can be used to limit the data that is been returned. Among the useful ones are listed below:

–start-datetime=datetime

Start reading the binary log at the first event having a timestamp equal to or later than the datetime argument. The datetime value is relative to the local time zone on the machine where you run mysqlbinlog. The value should be in a format accepted for the DATETIME or TIMESTAMP data types. For example:

mysqlbinlog --start-datetime="2005-12-25 11:25:56" binlog.000001

–stop-datetime=datetime

Stop reading the binary log at the first event having a timestamp equal or posterior to the datetime argument. This option is useful for point-in-time recovery. See the description of the –start-datetime option for information about the datetime value.

–start-position=N

Start reading the binary log at the first event having a position equal to the N argument. This option applies to the first log file named on the command line.

–stop-position=N

Stop reading the binary log at the first event having a position equal or greater than the N argument. This option applies to the last log file named on the command line.

For more usage information on mysqlbinlog, visit here.


2 Responses to “How to Read MySQL Binary Log Files (BinLog) with mysqlbinlog”

  1. nicolas
    September 5th, 2009 05:04
    2

    don’t forget (as i did) to execute mysqlbinlog with a user that has permission to read the binlog or sudo works nicely too.

  2. Pisu
    June 29th, 2008 16:33
    1

    This post saved my life today… when the binary log were the only way to retrieve important data get lost… thanks!!

Leave a Reply

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Subscribe to comments feature has been disabled. To receive notification of latest comments posted, subscribe to My Digital Life Comments RSS feed or register to receive new comments in daily email digest.
Custom Search

New Articles

Incoming Search Terms for the Article

mysqlbinlog - mysql binary log - mysqlbinlog command - mysql log viewer - mysql log files - mysql bin log reader - mysql bin log viewer - mysql binlog - BINLOG - mysql binary logs - mysql logs - mysql search binary log - read mysql binary log - view mysql log - view mysql binary log - windows mysqlbinlog - mysql binary log viewer - mysql binary log view - mysql view bin log - read binlog - how to read mysql bin logs - mysql read log - mysql log reader - mysql view binary log - mysql view binary logs - read mysql logs - read mysql log - mysql log file - windows mysqlbinlog file is not a binary log file - search mysql binary log - binlog mysql - mysql binlog viewer - mysqlbinlog usage - reader mysql binary log - reading mysql-bin logs - view mysql bin-log - mysql view log - mysql binlog format - mysql transaction log view - read binary log - mysql log view - mysql read binary log - read mysql error log - manually read bin log file - mysql import bin log - mysql The binary log - how to read mysql binary log - mysql binary log - read mysql bin log - read mysql bin -