Skip to content

Dumping MySQL users and grants

MySQL stores its users and grants in a very annoying, non-clonable format. I found this post, describing how you can extra them.

This bash function generates grants:

  mysql -B -N $@ -e "SELECT DISTINCT CONCAT(
    'SHOW GRANTS FOR \'', user, '\'@\'', host, '\';'
    ) AS query FROM mysql.user" | \
  mysql $@ | \
  sed 's/\(GRANT .*\)/\1;/;s/^\(Grants for .*\)/## \1 ##/;/##/{x;p;x;}'

    No Comments ( Add comment / trackback )

    Post a comment


    Your email is never published nor shared.

    Allowed HTML: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>