Skip to content

Instantly share code, notes, and snippets.

@jcook793
Created April 22, 2014 22:52
Show Gist options
  • Save jcook793/11197121 to your computer and use it in GitHub Desktop.
Save jcook793/11197121 to your computer and use it in GitHub Desktop.
Generates MySQL GRANT statements for all users in a DB
#!/bin/bash
users_hosts=`mysql -B -N -e "select user, host from mysql.user;"`
echo "${users_hosts}" | while read user_host; do
user=`echo "${user_host}" | cut -f 1`
host=`echo "${user_host}" | cut -f 2`
if [ "${user}" != "root" ]; then
grants=`mysql -N -e "show grants for '${user}'@'${host}';"`
echo "${grants}" | while read grant; do
echo "${grant};"
done
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment