34

How do I backup MySQL users and their privileges?

Anything like mysqldump?

I am looking for something like:

mysqldump -d -u root -p MyTable > Schema.sql

8 Answers 8

42
mysql -BNe "select concat('\'',user,'\'@\'',host,'\'') from mysql.user where user != 'root'" | \
while read uh; do mysql -BNe "show grants for $uh" | sed 's/$/;/; s/\\\\/\\/g'; done > grants.sql
0
31

You can backup mysql database using

mysqldump -u root -p mysql > mysql.sql

and restore mysql database by executing

 mysql -uroot -p mysql < mysql.sql

Dont forget to

FLUSH PRIVILEGES

after restoring dump.

Hope it helps...

3
  • 1
    Thanks! After the FLUSH PRIVILEGES command, it all started working
    – Dzamir
    Jun 16, 2013 at 7:27
  • This is the better answer because it includes the flush privileges command. Another alternative is to just restart the server process instead of flushing privileges.
    – akahunahi
    Oct 1, 2013 at 17:49
  • 3
    This is only guranteed to work if restoring to the same MySQL version, the the 'mysql' database may differ accross versions and contains many more things than just users/privileges. If your intention is to migrate your users and privileges to another server, @spirit 's answer above is the way to go.
    – kostas
    Mar 11, 2015 at 12:47
14

So far my experience with MySQL i didn't see anything to backup user and their privileges through a command line.

But i can backup those critical data by backing up mysql

mysqldump -u root -p mysql > mysql.sql
1
  • how should this be restored? is it just by executing the resulting file on the information_schema?? Oct 31, 2012 at 8:21
9

Percona has a great tool for this. pt-show-grants will dump users and their permissions so you can easily reload them.

https://www.percona.com/doc/percona-toolkit/LATEST/pt-show-grants.html

1
  • This worked beautifully, and dumped hashed passwords unlike the other solutions. Use --ignore to skip grants for certain users, or --only to only include grants for certain users. Option syntax differs from mysqldump in that --ask-pass is its own option. Jan 27, 2020 at 9:09
6

The users and privileges are stored in the databased named 'mysql'. You can use mysqldump to backup the tables in the databased named 'mysql'.

1
1

Good practice is using script for daily backup MySQL users and their privileges. Take take a look on a one:

#!/bin/sh

HOSTNAME="localhost"

mysql -h $HOSTNAME -B -N -e "SELECT CONCAT('\'', user,'\'@\'', host, '\'') FROM user WHERE user != 'debian-sys-maint' AND user != 'root' AND user != ''" mysql > mysql_all_users_$HOSTNAME.txt

while read line; do mysql -h $HOSTNAME -B -N -e "SHOW GRANTS FOR $line"; done < mysql_all_users_$HOSTNAME.txt > mysql_all_users_$HOSTNAME.sql

sed -i.bak 's/$/;/' mysql_all_users_$HOSTNAME.sql

rm mysql_all_users_$HOSTNAME.txt
rm mysql_all_users_$HOSTNAME.sql.bak

Result of this script will be mysqldump file with users and privileges.

P.S. If your MySQL requires password - put -p or -u username -p after mysql -h $HOSTNAME in two places.

0

The scripts given above give the general idea, but they're inefficient. They're forking/execing mysql n+1 times. It can be done in only two calls to mysql

mysql ${logininfo} -B -N -e "SELECT CONCAT('\'',user,'\'@\'',host,'\'') from user where user != 'root'" mysql | \
while read uh
do
   echo "SHOW GRANTS FOR ${uh};"
done | mysql ${logininfo} -B -N | sed -e 's/$/;/' > ${outfile}

If there are users other than root that you don't want to backup use or and specify user != 'whatever' in the where clause of the first mysql call.

0

probably pretty obvious for mysql command liners but for @spirit's answer above had to add -u root -ppassword after both mysql commands

mysql -u root -ppassword -BNe "select concat(''',user,''@'',host,''') from mysql.user where user != 'root'" | while read uh; do mysql -u root -ppassword -BNe "show grants for $uh" | sed 's/$/;/; s/\\/\/g'; done > grants.sql;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.