Friday, May 29, 2009

Reverse Engineering MySQL Database Driven Applications on Windows

Version:1.0 StartHTML:0000000168 EndHTML:0000008367 StartFragment:0000000499 EndFragment:0000008350
Introduction

You've just started a new job, you need to hit the ground running and quickly, but the rest of the team are “too busy” with their work and you don't want to keep pestering your new colleagues or boss with questions. Enter the MySQL Binary Log.

If you have never heard of the Binary Log I suggest at least finding out what it is and what it is usually used for before you continue with the tutorial.
Tools of the trade

O.K. Let's start with the tools required for the job. You will need to download a neat little toolkit called unxutils from Source Forge and make sure it's install path is referenced in your Path. Unxutils is a handy tool which brings many Unix commands to the windows desktop. It especially useful if you commonly switch between Windows and Linux platforms. Once you have done this you need to open a Command Prompt window and test to make sure you can access the commands. Type the following command and hit enter.

ls -l

If you get a directory listing you can happily move on to the next step. Enabling the Binary Log on Windows. The above command was simply to test if unxutils was installed correctly.
The Service

If you search Google for "Enabling MySQL Binary Log on Windows" you will get a stack of results returned some of which will lead you to MySQL bug status results. The reason for this is that since most Windows users will have MySQL installed, using a Windows installer, on their system as a Service and by default Binary Logging is not enabled. If you follow the below instructions to the letter you will be one of few who has Binary Logging enabled on a Windows box.

For this example I am going to install another MySQL Service along side my original one as I do not wish to use Binary Logging all the time. If you already have a MySQL Service running make sure you stop it at this point. If you don't already have the command line open from the first task then fire it up and type the following command.

sc create MySQLBinLogging binPath= ""C:PathToMySQL Serverbinmysqld" --log-bin=yourmachinename MySQL"

After you have run this command you should see a success message at which point you can open the services manager and start the service. On a typical MySQL installation the binary log file will be located in the MySQL data directory on Vista this is typically in “C:ProgramDataMySQL Serverdata” and will be named as you specified with the –log-bin directive when installing the service. The log file is rotated on a size basis so the first log file should have a suffix of 000001 and so on. At this point we are ready to extract the juicy data we have been waiting to get at.
The Data

Again we will be operating from the command line for now so assuming you still have it open change your path to the data directory where the log file is type the following command and hit enter.

tail -f mylogfile

And that is it. If you want to output the data into a text file for further analysis just type the following.

tail -f mylogfile > myoutputfile.txt

NOTE: The binary log only stored update data so you won't get your select data here. You will need to work with the General Query Log for selects.

No comments: