How to Reinstall MySQL (And Bring Your Grants and Users With You)
Reinstalling MySQL while preserving permissions and users can be a headache. Fortunately, the Percona Toolkit has a way to bring what you need along for the ride.
Join the DZone community and get the full member experience.Join For Free
Every so often, I need to reinstall a MySQL version from scratch and preserve all the user accounts and their permissions (or move the same users and privileges to another server).
As of MySQL 5.7, MySQL does not make this easy! MySQL SHOW GRANTS only shows permissions for one user, and the method suggested on StackExchange — dumping tables containing grants information directly — is not robust (as Rick James mentions in the comments). It also doesn’t work between different MySQL versions.
All you need to do is:
- On the source, or to backup MySQL privileges, run:
pt-show-grants > grants.sql
- On the target, or to restore MySQL privileges, run:
mysql < grants.sql
- If you would like to clean up the old privileges from MySQL before loading new ones, use:
pt-show-grants --drop --ignore root@localhost | grep "^DROP USER " | mysql
This removes all the users (except the root user, which you will need to connect back and load new privileges).
Published at DZone with permission of Peter Zaitsev, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.