Skip to content

Instantly share code, notes, and snippets.

@chorn
Created December 23, 2010 15:41
Show Gist options
  • Save chorn/753132 to your computer and use it in GitHub Desktop.
Save chorn/753132 to your computer and use it in GitHub Desktop.
Check all of the databases and tables on a mysql host.
#!/bin/bash
USER="root"
PASS=""
HOST="localhost"
TYPE="QUICK"
usage() {
echo "$0 [--host hostname] [--user username] [--pass password] [--type {valid mysql check table type}]"
echo " [--scan {scans ~/.my.cnf for user & password]"
exit 0
}
while [[ $# -gt 0 ]] ; do
curr_arg="$1" ; shift
if [[ $# -gt 0 ]] ; then next_arg="$1" ; shift ; else next_arg="" ; fi
case "$curr_arg" in
-u|--user|--username) USER=$next_arg ;;
-p|--pass|--password) PASS=$next_arg ;;
-t|--type) TYPE=$next_arg ;;
-h|--host|--hostname) HOST=$next_arg ;;
-s|--scan)
USER=`grep '^user=' ~/.my.cnf | sed -e 's/^user=//'`
PASS=`grep '^password=' ~/.my.cnf | sed -e 's/^password=//'`
;;
*) usage ;;
esac
done
AUTH="-u${USER} -p${PASS} -h${HOST}"
for db in `mysql $AUTH -Be "SHOW DATABASES" | grep -Ev '(information_schema|Database)'` ; do
for table in `mysql $AUTH -Be "SHOW TABLES" "$db" | grep -v Tables_in` ; do
mysql $AUTH "$db" -Be "CHECK TABLE $table $TYPE" | grep -Ev "(Msg_text|doesn't support check)"
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment