I know how to export/import the databases using mysqldump & that's fine but how do I get the privileges into the new server.
For extra points, there are a couple of existing databases on the new one already, how do I import the old servers privileges without nuking the couple existing of ones.
Old server: 5.0.67-community
New server: 5.0.51a-24+lenny1
EDIT: I've got a dump of the db 'mysql' from the Old Server & now want to know the proper way to merge with the 'mysql' db on the New Server.
I tried a straight 'Import' using phpMyAdmin and ended up with an error regarding a duplicate (one that I've already migrated manually).
Anyone got an elegant way of merging the two 'mysql' databases?
Do not mess with the mysql db. There is a lot more going on there than just the users table. Your best bet is the "SHOW GRANTS FOR" command. I have a lot of CLI maintenance aliases and functions in my .bashrc (actually my .bash_aliases that I source in my .bashrc). This function:
The first mysql command uses SQL to generate valid SQL which is piped to the second mysql command. The output is then piped through sed to add pretty comments.
The $@ in the command will allow you to call it as: mygrants --host=prod-db1 --user=admin --password=secret
You can use your full unix tool kit on this like so:
That is THE right way to move users. Your MySQL ACL is modified with pure SQL.
There are two methods for extracting SQL Grants from a MySQL Instance
METHOD #1
You can use pt-show-grants from Percona Toolkit
METHOD #2
You can emulate
pt-show-grants
with the followingEither 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:
Give it a Try !!!
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
Or, utilize percona-toolkit (former maatkit) and use
pt-show-grants
(ormk-show-grants
) for that purpose. No need for cumbersome scripts and/or stored procedures.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.).
You could also do it as a stored procedure:
and call it with
then pipe the output through Richards sed command to get a backup of the privileges.
How about a PHP script? :)
View source on this script and you will have all the privileges listed:
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:
At this point, all my data was visible if I logged in as
root
, themysql.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 theGRANT
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:Hopefully that helps someone else achieve what should be a simple task!
create a shell script file with the following code:
##############################################3 #****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 yourmysql root
user can be read.