I'm setting up up a new MySQL server and I'd like to give it the same set of usernames, allowed hosts, and passwords as an existing server (which is going away).
Would it work to just do a dump of the users table and then load it on the new server?
Is there a better way than that?
Should do it, remember to add
-u $USER
and-p$PASSWORD
as requiredI would take a look at the Percona Toolkit. The description of the
pt-show-grants
tool says it all...Whilst a dump of the
mysql
database would probably work, in my experience, properly documenting all the access and setting it up again withGRANT
statements is much better. There are two benefits to doing it this way:The accepted answer (using mysqldump to backup mysql.user table) is a very dangerous approach if you are migrating to a new server version.
I did that in the past (migrating users from Mysql 4 to Mysql 5.1), and later I had problems trying to grant or modify privileges to my database users.
This happens because mysql.users table structure differs between mysql server versions, as explained here:
https://dba.stackexchange.com/a/16441/18472
So, take care if you are upgrading your server version. Some more hints here:
https://stackoverflow.com/a/13218162/710788
mysqldump will work, but since the mysql database uses ISAM, you can just copy the directory to the new server. You'll find this in different places, but the most common location for the mysql database will be /var/lib/mysql/mysql. Be sure to stop mysql on the new server, move the old directory out of the way, copy the directory and restart. An example using standard locations as root: