patternsqlModerate
Export all MySQL users
Viewed 0 times
mysqlusersexportall
Problem
I need export all of a MySQL database users (including permissions) for a migration. There is over 5,000 users, what is the best way to do this?
Should I just dump the
Should I just dump the
mysql.user table?Solution
If you are moving the users to another DB Server running the same major version of MySQL, copying
Then, you would have to copy the following
Here is logical way to dump it: as SQL GRANT commands !!!
SUGGESTION #1
If you already have Percona Toolkit installed, run pt-show-grants to a text file
SUGGESTION #2
If you don't have Percona Toolkit and can't wait, here is my personal emulation of it
EPILOGUE
On the new DB Server, after migrating the data, you login to mysql as root and run
GIVE IT A TRY !!!
mysql.user is not sufficient. If the users have access to specific databases, copying mysql.user brings the user and the password.Then, you would have to copy the following
mysql.dbfor database-level grants
mysql.tables_privfor table-level grants
mysql.columns_privfor column-level grants
Here is logical way to dump it: as SQL GRANT commands !!!
SUGGESTION #1
If you already have Percona Toolkit installed, run pt-show-grants to a text file
GRANT_FILE=MyDatabaseUSers.sql
pt-show-grants -uroot -p > ${GRANT_FILE}SUGGESTION #2
If you don't have Percona Toolkit and can't wait, here is my personal emulation of it
GRANT_CMDS=/tmp/ShowGrantsToExport.sql
GRANT_FILE=MyDatabaseUSers.sql
MYSQL_USER=root
MYSQL_PASS=rootpassword
MYSQL_CONN="-u${MYSQL_USER} -p${MYSQL_PASS}"
SQL="SELECT CONCAT('SHOW GRANTS FOR ',QUOTE(user),'@',QUOTE(host),';')"
SQL="${SQL} FROM mysql.user WHERE user<>'' AND host<>''"
mysql ${MYSQL_CONN} -ANe"${SQL}" > ${GRANT_CMDS}
mysql ${MYSQL_CONN} ${GRANT_FILE}EPILOGUE
On the new DB Server, after migrating the data, you login to mysql as root and run
mysql> source MyDatabaseUSers.sqlGIVE IT A TRY !!!
Code Snippets
GRANT_FILE=MyDatabaseUSers.sql
pt-show-grants -uroot -p > ${GRANT_FILE}GRANT_CMDS=/tmp/ShowGrantsToExport.sql
GRANT_FILE=MyDatabaseUSers.sql
MYSQL_USER=root
MYSQL_PASS=rootpassword
MYSQL_CONN="-u${MYSQL_USER} -p${MYSQL_PASS}"
SQL="SELECT CONCAT('SHOW GRANTS FOR ',QUOTE(user),'@',QUOTE(host),';')"
SQL="${SQL} FROM mysql.user WHERE user<>'' AND host<>''"
mysql ${MYSQL_CONN} -ANe"${SQL}" > ${GRANT_CMDS}
mysql ${MYSQL_CONN} < ${GRANT_CMDS} | sed 's/$/;/g' > ${GRANT_FILE}mysql> source MyDatabaseUSers.sqlContext
StackExchange Database Administrators Q#95928, answer score: 13
Revisions (0)
No revisions yet.