Mysql
 sql >> Database >  >> RDS >> Mysql

Ulteriori informazioni sulle autorizzazioni a livello di tabella MySQL

È molto comune vedere una dichiarazione di sovvenzione come la seguente che dà accesso a tutte le tabelle in un determinato database.

GRANT SELECT, SHOW VIEW
ON mydatabase.*
TO myreaduser@myhost IDENTIFIED BY 'somepassword';
FLUSH PRIVILEGES;

Dove mydatabase , myreaduser , myhoost e somepassword sono le variabili appropriate per il tuo database. Nota che i FLUSH PRIVILEGES il comando reimposta i privilegi di MySQL e non sarai in grado di utilizzare le nuove autorizzazioni utente fino a quando non verrà eseguito.

Tale utente sarà in grado di leggere e accedere a tutte le tabelle in un database. Per questo esempio useremo il database boatio che ha 3 tabelle:boats , trips e users .

mysql> show tables;
+------------------+
| Tables_in_boatio |
+------------------+
| boats            |
| trips            |
| users            |
+------------------+
3 rows in set (0.00 sec)

Se vogliamo creare un utente che ha accesso solo ai trips table, sostituiamo semplicemente il jolly splat (*) che rappresenta tutte le tabelle, con le tabelle specifiche che desideri (in questo caso:trips ).

GRANT SELECT, SHOW VIEW
ON boatio.trips
TO myreaduser@localhost IDENTIFIED BY 'somepassword';
FLUSH PRIVILEGES;

Ora possiamo accedere come nuovo utente ed eseguire le tabelle di visualizzazione per vedere che ha accesso solo ai trips tavolo e non gli altri due. Questo nuovo utente semplicemente non sa nemmeno che esistono le altre tabelle.

$ mysql -umyreaduser boatio -psomepassword
mysql> show tables;
+------------------+
| Tables_in_boatio |
+------------------+
| trips            |
+------------------+
1 row in set (0.00 sec)

Per consentire all'utente di accedere a più tabelle, riesegui semplicemente lo stesso GRANT istruzione con i nomi di tabella aggiuntivi. Ad esempio, quanto segue concederà l'accesso ai trips e users tabelle ma non le boats .

GRANT SELECT, SHOW VIEW ON boatio.trips TO myreaduser@localhost IDENTIFIED BY 'somepassword';
GRANT SELECT, SHOW VIEW ON boatio.users TO myreaduser@localhost IDENTIFIED BY 'somepassword';
FLUSH PRIVILEGES;

E la prova:l'utente ora ha accesso ai trips e users tabelle ma non boats .

$ mysql -umyreaduser boatio -psomepassword

mysql> show tables;
+------------------+
| Tables_in_boatio |
+------------------+
| trips            |
| users            |
+------------------+
2 rows in set (0.00 sec)