Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

So I'm in for a bit of a tedious work. We have about 10 servers, each with about 4 MySQL users (plus mysql-root). All of these users have full access to their respective schema.

Now we've decided that they should only have the basic SELECT, INSERT, UPDATE rights (because that's all they need).

Looking at the REVOKE I get the feeling that since they've been added with GRANT ALL ON db.* I need to first revoke all privs, then grant the ones i need.

Now for the question. Is there a good way to do this for all users (on one server) at the same time or do I need to review them one by one?

EDIT:

The setup is something like this:

Server 1:

User dbuser_a with access to db_a

User dbuser_b with access to db_b ...

Server 2:

User dbuser_c with access to db_c

User dbuser_d with access to db_d ...

share|improve this question
add comment

1 Answer 1

up vote 1 down vote accepted

Create a ~/.my.cnf file to store your MySQL password, something like this:

[mysql]
user            = root
password        = secr3t
host            = ...

Remember to chmod 600 for it.

You can then revoke all privileges for all users with the shell snippet below:

mysql --skip-column-names -e 'select user,host from mysql.user;' | grep -v root | \
while read account; do
    mysql -e "revoke all privileges on db.* from '`echo $account | \ 
       awk '{ print $1 }'`'@'`echo $account | awk '{ print $2 }'`'; flush privileges;"
done

Do the same for granting privileges which you want.


UPDATE

You can also revoke all privileges: global, database, table, routine, ... from an user by removing the on db.* from the above:

mysql --skip-column-names -e 'select user,host from mysql.user;' | grep -v root | \
while read account; do
    mysql -e "revoke all privileges, grant option from '`echo $account | \ 
       awk '{ print $1 }'`'@'`echo $account | awk '{ print $2 }'`'; flush privileges;"
done
share|improve this answer
    
That's revoke all privileges on db.* from USERNAME@HOST ; flush privileges; user, by user. –  Iñigo Sep 8 '11 at 11:04
    
Thanx for the tip. One thing though, perhaps it was not clear from my original post. On each server there is dbuser_a which access db_a, dbuser_b which accesses db_b etc. My take on your code is that it would go for several users but only one database? –  danneth Sep 8 '11 at 12:17
    
Please check out my above answer. –  quanta Sep 8 '11 at 16:02
    
Great work. I would never have figured that out –  danneth Sep 9 '11 at 7:25
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.