Cherian
Cherian

Reputation: 19418

Backup MySQL users

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

Upvotes: 34

Views: 55101

Answers (8)

stee
stee

Reputation: 11

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;

Upvotes: 0

cdbunch
cdbunch

Reputation: 1

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.

Upvotes: 0

Oleh Vasylyev
Oleh Vasylyev

Reputation: 714

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.

Upvotes: 1

Josh Wilson
Josh Wilson

Reputation: 3745

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

Upvotes: 9

jsist
jsist

Reputation: 5253

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...

Upvotes: 31

spirit
spirit

Reputation: 465

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

Upvotes: 44

simplyharsh
simplyharsh

Reputation: 36373

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

Upvotes: 14

Zoredache
Zoredache

Reputation: 39623

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

Upvotes: 6

Related Questions