I have a database with multiple schemas. One of these schemas contains some data that needs to remain hidden from the other schemas while allowing limited access to other data. The intent is to only make the data accessible via Views and Stored Routines.
I’m using MySQL 5.7.33 on an Ubuntu server and MySQL Workbench 8.0 on Windows.
My question is, how do I set up the schema to make views, etc. accessible, but not the tables.
I’ve given users the following permissions on the restricted schema.
And the CREATE statement for the view begins:
CREATE
ALGORITHM = UNDEFINED
DEFINER = `DBA`@`%`
SQL SECURITY DEFINER
VIEW `CommonData`.`VW_SystemProperty` AS ...
When I run a select on the view I get the error:
Error Code: 1142. SELECT command denied to user ” for table ‘VW_SystemProperty’
What am I missing?
2
Answers
You can grant to all procedures or all procedures in a given schema this way:
Strangely, the optional
PROCEDURE
keyword for the object type does not support wildcards in the procedure name.But you can use this keyword if you grant to a specific procedure by name.
The only wildcard for table names or view names is
*
. You can’t use other wildcards like%
or_
in table names or view names.In MySQL 5.x, you could use wildcards in schema names, but this is now deprecated, because it’s a potential security weakness.
Although you cannot grant privileges using wildcards in the procedure or view names, you can use the following script to generate the
GRANT
statements.This is just a technique to save some effort and avoid errors.
Once generated, the statements will need to be run manually.
Of course, you can add/remove restrictions in the
WHERE
clause according to your requirements.A similar approach can be followed for stored procedures: