MySQL copy a user

2019-04-07 17:03发布

问题:

I want to create two users on my MySQL test database, One with read-only access to tables relevant to generating reports, etc, the other with read-write access to the same tables. This is for testing a subsystem that normally connects with a read-only user but switches to a read-write user for certain tasks. I've created the read-write user with the correct privileges, and now I need a read-only version of the same user.

I'd rather not create the read-only version from scratch as I had to set a lot of privileges, which was rather laborious. Is there a way I can create a new user based on an existing user and then remove the INSERT/UPDATE/DELETE privilages from the new user? Something like CREATE USER 'user2' LIKE 'user1' or similar? I couldn't find it in the MySQL docs if it is possible to do this.

回答1:

I found two options.

1st if you are Windows User, you can use MySql Administrator. http://dev.mysql.com/doc/administrator/en/mysql-administrator-user-administration-user-accounts.html

2nd you can use mysquserclone command from Mysql Utilities: http://wb.mysql.com/utilities/man/mysqluserclone.html

Good luck.



回答2:

How about inserting into another table, update columns? Something like:

CREATE TABLE user_tmp LIKE user;
INSERT INTO user_tmp SELECT * FROM user WHERE host ='localhost' AND USER ='root';
UPDATE user_tmp SET user = 'readonlyuser', Insert_priv = 'N', Update_priv = 'N',
    Delete_priv = 'N', /* TODO: ADAPT TO YOUR SUITS */ LIMIT 1;
INSERT INTO user select * FROM user_tmp;
DROP TABLE user_tmp;