Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 
like image 963
Cherian Avatar asked Feb 28 '09 08:02

Cherian


People also ask

Does Mysqldump include users?

user; Knowing this, it's pretty obvious that mysqldump shouldn't do anything with users. However, if you need an answer to exporting/importing users and perms I suggest you check the following article - it helped me out.

Where MySQL users are stored?

MySQL stores accounts in the user table of the mysql system database. An account is defined in terms of a user name and the client host or hosts from which the user can connect to the server.


2 Answers

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 
like image 137
spirit Avatar answered Oct 28 '22 19:10

spirit


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

like image 37
jsist Avatar answered Oct 28 '22 17:10

jsist