


Old server: 5.0.67-community

New server: 5.0.51a-24+lenny1




  • 1. Is it a requirement for you to use PHPMyAdmin? If it is I will write some PHPMyAdmin specific instructions for you. 2. From PHPMyAdmin if you try to "select * from mysql.user limit 1;" do you get results or an error. – Bruno Bronosky Jun 11 '09 at 20:35
  • 1
    As I mentioned below, I think Richard's mygrants script is a good way to get grant info. However, you can also try editing the dump file to comment out INSERTs to the user table for users who already exist. Privileges for the dbs restored from the old server will then be copied. If you've already assigned privileges manually for some of the dbs you restored to the new box, look for these table names in the privilege files and comment these out, too. Don't forget a flush_privileges afterwards. Good description of the mysql db at: grahamwideman.com/gw/tech/mysql/perms/index.htm – nedm Jun 13 '09 at 10:37

10 Answers 10



  mysql -B -N $@ -e "SELECT DISTINCT CONCAT(
    ) AS query FROM mysql.user" | \
  mysql $@ | \






  • This is actually a nice bash helper function that works great. Take the output from that and be able to run on the new server and the privileges would be entered properly and accurately. – Jeremy Bouse Jun 11 '09 at 14:12
  • 1
    I love your function, today it saved me a lot of work. Thank you, thank you, thank you... – Fabio Feb 15 '11 at 22:32
  • 2
    This is great but it has one big flaw. An extra "\" is added to underscores in database names, so if you have for example a user with specific privileges on a database called foo_bar, it will be rendered as foo\_bar instead of foo_bar, so that won't be imported correctly. At least, this happens if you save the output from you script into a SQL file and then import it into the new server. I haven't tried the direct piping of export and import in a single line. – matteo Oct 15 '13 at 9:41
  • 1
    Be careful with this command. If you have in user table an user with host %, but then in db table you have entries where host is an explicit value, those entries in db table are not retrieved using this method. – Mitar Jun 26 '15 at 7:27
  • 1
    @matteo Add -r to the 2nd mysql command in the function and the double escapes won't be output by mysql. eg: mysql -r $@ – lifo Sep 12 '16 at 12:08

There are two methods for extracting SQL Grants from a MySQL Instance


You can use pt-show-grants from Percona Toolkit

MYSQL_CONN="-uroot -ppassword"
pt-show-grants ${MYSQL_CONN} > MySQLUserGrants.sql


You can emulate pt-show-grants with the following

MYSQL_CONN="-uroot -ppassword"

Either method will produce a pure SQL dump of the MySQL grants. All there is left to do is to execute the script on a new server:

mysql -uroot -p -A < MySQLUserGrants.sql

Give it a Try !!!

  • 1
    pt-show-grants is exactly what you want for this, it works great. – Glenn Plas Dec 7 '14 at 22:14
  • Percona is just pure awesomeness. – sjas Oct 14 '15 at 13:02
  • 7
    But answer #2 is just as good and will work without additional software! – sjas Oct 14 '15 at 13:05

Richard Bronosky's answer was extremely useful for me. Many thanks!!!

Here is a small variation which was useful for me. It is helpful for transfering users e.g. between two Ubuntu installations running phpmyadmin. Just dump privileges for all users apart from root, phpmyadmin and debian-sys-maint. The code is then

) AS query FROM mysql.user WHERE user NOT IN ('root','phpmyadmin','debian-sys-maint')"  | \
mysql $@ | \
  • 2
    If you look at my example where I grep rails_admin you can infer how to do this without making a special function for every edge case. Use grep's "invert-match" option like so: mygrants --host=prod-db1 --user=admin --password=secret | grep -Ev 'root|phpmyadmin|debian-sys-maint' | mysql --host=staging-db1 --user=admin --password=secret – Bruno Bronosky Nov 8 '17 at 1:25



You can mysqldump the 'mysql' database and import to the new one; a flush_privileges or restart will be required and you'll definitely want to back up the existing mysq db first.

To avoid removing your existing privileges, make sure to append rather than replace rows in the privilege tables (db, columns_priv, host, func, etc.).

  • Thanks @nedm. Seems like the annoying cPanel server I'm trying to get the dbs off doesn't show the db 'mysql'. Otherwise I'd test and affirm your answer. Once I figure that out I'll check back. Thanks. – Gareth May 16 '09 at 8:49
  • That is unfortunate but understandable, you are probably prevented from accessing any database but the ones directly owned by your user on a shared db. – Dave Cheney May 17 '09 at 6:05
  • Ouch, yeah, without access to 'mysql' it will be difficult to do anything related to users or permissions. Do you have command line access? Can you run mysqldump from the terminal or command line (NOT from the mysql shell)? mysqldump -u username -ppassword mysql > mysqldump.sql – nedm May 18 '09 at 17:08
  • The db 'mysql' was accessible from the Cpanel WHM if I logged in as 'root'. From there I can access a version of phpmyadmin which has the 'mysql' database containing permissions – Gareth Jun 10 '09 at 7:53
  • Merging into a existing mysql schema, data extracted from a mysql schema via mysqldump is almost certain to be problematic. You are manipulating a complex schema with enforced relationships, etc. This schema is so complex, in fact, that they created dedicated SQL syntax (GRANT, REVOKE, DROP USER, etc.) for dealing with it. Your extract, however, consists of only INSERT statements. I'm running out of characters here. Need I continue? – Bruno Bronosky Jun 13 '09 at 3:52

You could also do it as a stored procedure:

    COMMENT 'Show GRANT statements for users'
        'SHOW GRANTS FOR ', user, '@', host, ';'
    ) AS query FROM mysql.user;
    OPEN c;
        FETCH c INTO v;
        SET @v = v;
        PREPARE stmt FROM @v;
        EXECUTE stmt;
    CLOSE c;

and call it with

$ mysql -p -e "CALL spShowGrants" mysql

then pipe the output through Richards sed command to get a backup of the privileges.


While it appears @Richard Bronosky's answer is the correct one, I came across this question after trying to migrate a set of databases from one server to another and the solution was much simpler:

server1$ mysqldump -u root -p --all-databases > dbdump.sql

server2$ mysql -u root -p < dbdump.sql

At this point, all my data was visible if I logged in as root, the mysql.user table had everything in it I was expecting, but I couldn't log in as any of the other users and found this question assuming I'd have to re-issue the GRANT statements.

However, it turns out that the mysql server simply needed to be restarted for the updated privileges in the mysql.* tables to take effect:

server2$ sudo restart mysql

Hopefully that helps someone else achieve what should be a simple task!

  • Oh, and my situation was slightly different to the OP's, in that I wasn't trying to merge a dump into a server with existing databases/users. – Tom Sep 3 '11 at 1:17
  • 2
    You don't actually have to restart MySQLd to 'update the privileges'. It can be done with the MySQL command 'flush privileges;' – CloudWeavers May 3 '12 at 18:16
  • The issue with this approach is that it works only when both source and destination MySQL version are the same. Otherwise you might have issues because source mysql.user table has different columns than destination mysql.user table, for example. – Mitar Jun 26 '15 at 5:30

How about a PHP script? :)

View source on this script and you will have all the privileges listed:


//create grants select statements
$rs = mysql_query("SELECT DISTINCT CONCAT('SHOW GRANTS FOR ''', user, '''@''', host, ''';') AS query FROM user");

//iterate through grants
while ($row=mysql_fetch_array($rs)) {
    //run grant query
    $rs2 = mysql_query($row['query']);
    //iterate through results
    while($row2 = mysql_fetch_array($rs2)){
        //print results
        echo $row2[0] . ";\n\n";

create a shell script file with the following code:

echo "SELECT DISTINCT CONCAT (\"show grants for '\", user, \"'@'\", host, \"';\") AS query FROM mysql.user; " >   script.sql    
echo "*** You will be asked to enter the root password twice ******"    
mysql -u root -p  < script.sql > output.sql ;    
cat output.sql | grep show > output1.sql  ; rm output.sql -f ; 
mysql -u root -p  < output1.sql > output.sql ;
echo "-----Exported Grants-----"    
cat  output.sql ; rm  output.sql   output1.sql -f    
rm  script.sql -f

****then run it on the shell like this: you will be asked to enter the root password twice and then the GRANTS SQL will be displayed on the screen.****


One-liner doing pretty much the same as the awesome pt-show-grants:


Based on unix tools only, no additional software needed.

Execute from within a bash shell, assumed is you have a working .my.cnf where the password of your mysql root user can be read.

  • i duplicated half of @rolandomysqldba 's post after coming back half a year later, i just realized. – sjas Apr 13 '16 at 18:36

protected by Chris S May 3 '12 at 18:25


Would you like to answer one of these unanswered questions instead?
