Working on manual backup of my WordPress site I developed a very simple approach to migrate the WordPress database user along with their password. This can be used on any or multiple users of the database.
Requirements
- A MySQL server with the user you want to copy to the backup
- A working installation of MySQL Workbench
- Server Connection to Source ( web-server for the source )
- MySQL Server connection to target ( localhost in my case )
Getting the user with CREATE Statement
From the source database open a new query and enter the following SQL: Replace mcp_user with the user you would like to migrate.
show create user 'mcp_user'@'localhost';
First, using the MySQL Workbench connect to the source that has your user.
It should look something like this:
Run the query and then you can copy the results by right clicking on the Result Grid row and selecting “Copy Row (Unquoted)”. Connect to your local MySQL instance and paste the result into a new query.
Retrieve the Permissions
we will use the same command but for grants, again replacing “mcp_user” with your user.
show grants for 'mcp_user'@'localhost';
Perform the same copy action from above but this time select both rows before right clicking.
Copy User to Local Database
When you are finished you should have in your local host the following query, add a USE database; statement at the top, where database is the name of your schema.
USE mcp_wordpress; CREATE USER 'mcp_user'@'localhost' IDENTIFIED WITH 'mysql_native_password' AS '*A4852D8F9BDEFAXXXDD35EED97BEXXXYYY4E5BA' REQUIRE NONE PASSWORD EXPIRE DEFAULT ACCOUNT UNLOCK; GRANT USAGE ON *.* TO 'mcp_user'@'localhost'; GRANT ALL PRIVILEGES ONmcp_wordpress
.* TO 'mcp_user'@'localhost'; GRANT ALL PRIVILEGES ONmcp_wordpress
.* TO 'mcp_user'@'localhost';
Test the User
Run this SQL and the attempt to login and do a show database to confirm.
mysql -u mcp_user -p
The password will be the same as the originating database
show databases;
Only the one database should be listed in my case.
That’s it, seems like a lot but it only 7 or 8 lines of SQL. If you know how to execute the results of the SHOW GRANTS or the SHOW CREATE, statements or an easier method, leave a comment I would love to learn that without a lot of digging around.