Skip to content

Instantly share code, notes, and snippets.

@mkubenka
Last active May 24, 2021 20:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mkubenka/8c74809d9b7e710ac4fbb66faac72486 to your computer and use it in GitHub Desktop.
Save mkubenka/8c74809d9b7e710ac4fbb66faac72486 to your computer and use it in GitHub Desktop.
How to search whole MySQL database for a particular string

How to search whole MySQL database for a particular string

https://dba.stackexchange.com/a/37041/7994

SELECT
    CONCAT('SELECT ',QUOTE(db),',',QUOTE(tb),',',QUOTE(col),',COUNT(1) FieldHasIt
    FROM `',db,'`.`',tb,'` WHERE \`',col,'\` LIKE ''%',SearchString,''%';') SearchSQL
FROM
(
    SELECT table_schema db,table_name tb,column_name col FROM information_schema.columns
    WHERE table_schema = 'mydb' AND
    (column_type LIKE 'char(%' OR column_type LIKE 'varchar(%' OR column_type LIKE '%text')
) A,(SELECT 'Hello' SearchString) B;
mysql -AN < prepare_search.sql > search.sql
mysql -AN < search.sql > results_search.txt

# Get table list with non-zero occurences.
grep -Ev "\s+0$" results_search.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment