Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?


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
up vote 1 down vote accepted

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

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;"

Do the same for granting privileges which you want.


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;"
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

Your Answer


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.