Grep through a MySQL database

Looking for a particular string in a set of locations is a common theme when debugging or reverse-engineering software. One of the interesting search locations might be a MySQL database. dbgrep.pl is a little Perl script that will select all data from all (or just the specified) tables and then attempt to find a string (or regular expression) match in any field. Needless to say, it is rather resource-intensive, but still fast enough to sift through moderately sized development databases (at a processing speed of 3-5 MB/s).

You can download dbgrep.pl from my web site.

This is dbgrep.pl, version 1.0, which scans for strings in a MySQL database.
Copyright (c) 2009 Jan Ploski (plosquare.com)
This software is licensed to you under Artistic License 2.0,
which is published at http://www.perlfoundation.org/artistic_license_2_0

Usage: dbgrep.pl [-u <db user>] [-h <db hostname>]
                 [-p] [-x] [-a] [-v] [-q]
                 <expr> <db name> [<table name> ...]
Switches: 
       -p a password is needed for authorization, read from console
       -x interpret <expr> as a Perl regular expression rather than string
       -a grep through all fields, not just text/(var)char/blob fields
       -v print matching values, not just table/field/primary key locations
       -q only print names of matching tables and fields

Some invocation examples:

./dbgrep.pl -u dbuser -h localhost -p somestring dbname
./dbgrep.pl -u root -xaq '^123' dbname

4 comments:

Anonymous said...

This script saved me TONS of time. Thanks!!

Anonymous said...

Wow... very, very useful.

Good job and thanks for share it!

Anonymous said...

awesome, Is there a way to search in a backup(somefile.sql) instead of a live database?

Thank you

jpl said...

@Anonymous: I don't know of a way to search through a dump file like with this script. You should first import the dump into a (temporary) database.

Post a Comment