Very Bad Idea

From Hashmysql
Jump to: navigation, search

It has been asked a few times "How do I search all databases?" The answer to that is "You can't. And really you shouldn't want to!". However the idiots persist so I wrote the following shell script.

#!/bin/bash

PASSWD=mysql_root_passwd

if [ "$1" = ""]; then
	read -p "Search string: " search_term
else
	search_term=$1
fi

if [ "$PASSWD" = "" ]; then
	USE_PASS=
else
	USE_PASS="$USE_PASS"
fi
for schema in `mysql -u root $USE_PASS -BNe 'SHOW SCHEMAS'`
do
	if [ "$schema" = "mysql" ]; then continue ; fi
	if [ "$schema" = "information_schema" ]; then continue; fi
	for table in `mysql -uroot $USE_PASS $schema -BNe 'SHOW TABLES'`
	do
		echo Serching in: $schema.$table
		for field in `mysql -u root $USE_PASS -BNe "SHOW FIELDS FROM $schema.$table" | awk '{print $1}'`
		do
			echo Field: $field
			mysql -u root $USE_PASS -e "SELECT * FROM $schema.$table WHERE $field LIKE '%$search_term%'"
			echo '---'
	        done
		echo "-------------- end $schema.$table -----------------"
	done
done
Personal tools
Development Guides
Documentation
Installing MySQL
Other Information