Mysql – grant access to user ‘root’ to all databases, but least specific one

MySQLpermissions

I've got the users: root, lmanager.
And too, a few databases: db1, db2, db3,…
I want to grant all privileges to user 'root' over all databases, except for the db2.
The user 'lmanager' will get all permissions only to db2.

It is possible to make it work?
it is possible to make db2 'invisible' to user root?
If it's possible, how i can do it?
Thanks in advance!

Best Answer

Unfortunately, you must do lots of these:

GRANT ALL ON db1.* TO ...;
GRANT ALL ON db3.* TO ...;  -- skipping db2
...

The syntax of GRANT/REVOKE is too clumsy to get what you want in any easier way.

You could write a query something like this to generate them:

SELECT
       CONCAT('GRANT ALL ON ',
              SCHEMA_NAME,
              '.* TO ...;')
    FROM information_schema.SCHEMATA
    WHERE SCHEMA_NAME NOT IN ('db2', 'mysql');

Then manually execute them.

Precede that by doing a GRANT USAGE TO ... to provide the password.