Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grant user access to limited number of tables in MySQL

Tags:

mysql

I'm running some tests and I'm trying to see if I can create a user with access to only 1 or 2 tables in my db. Does anyone know how this is done? My code below fails:

GRANT SELECT ON testdb.fruits, testdb.sports TO joe@localhost IDENTIFIED BY 'pass'; 

The error says I have an error in my syntax.

like image 877
enchance Avatar asked Dec 03 '11 16:12

enchance


People also ask

How do I grant permission to SELECT a table in MySQL?

To GRANT ALL privileges to a user , allowing that user full control over a specific database , use the following syntax: mysql> GRANT ALL PRIVILEGES ON database_name. * TO 'username'@'localhost';

How do I grant grants in MySQL?

The WITH GRANT OPTION clause gives the user the ability to give to other users any privileges the user has at the specified privilege level. To grant the GRANT OPTION privilege to an account without otherwise changing its privileges, do this: GRANT USAGE ON *.

What are the four privilege levels in MySQL?

Privilege levels in MySQL There are six privilege levels used for granting privileges to the user: global, database, table, column, stored procedure or function, and proxy, as shown in the below image.


1 Answers

Run them as two individual GRANT statements:

GRANT SELECT ON testdb.fruits TO joe@localhost IDENTIFIED BY 'pass'; GRANT SELECT ON testdb.sports TO joe@localhost IDENTIFIED BY 'pass'; 

The MySQL GRANT syntax only permits one object in the priv_level position:, though it may use a * as a wildcard:

GRANT     priv_type [(column_list)]       [, priv_type [(column_list)]] ...     ON [object_type] priv_level     TO user_specification [, user_specification] ...     [REQUIRE {NONE | ssl_option [[AND] ssl_option] ...}]     [WITH with_option ...]  object_type:     TABLE   | FUNCTION   | PROCEDURE  priv_level:     *   | *.*   | db_name.*   | db_name.tbl_name   | tbl_name   | db_name.routine_name 

The part below does not appear to work on MySQL 5.5. How to "subtract" privileges in MySQL addresses why.

To grant SELECT on all tables then selectively revoke, you could do:

GRANT SELECT ON testdb.* TO joe@localhost IDENTIFIED BY 'pass'; REVOKE ALL PRIVILEGES ON testdb.tblname FROM joe@localhost; 

This seems to be an odd method though, and I think I would individually GRANT rather than individually REVOKE.

like image 171
Michael Berkowski Avatar answered Sep 24 '22 15:09

Michael Berkowski