Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL copy a user

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.

like image 912
GordonM Avatar asked Jul 06 '11 14:07

GordonM


2 Answers

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;
like image 176
pevik Avatar answered Sep 18 '22 20:09

pevik


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.

like image 42
fdaines Avatar answered Sep 21 '22 20:09

fdaines