Operazioni sugli Utenti

Per l'utente root è possibile creare nuovi utenti, cancellare utenti esistenti, assegnare privilegi agli utenti e revocarli. Le informazioni sugli account sono memorizzate nelle tabelle del database mysql, database che si trova già in mysql fin dalla sua installazione e che è posizionato nella cartella <mysql>\data, dove <mysql> è la cartella in cui è installato mysql.

Creazione e Revoca Utenti

Si può creare un nuovo account tramite il comando CREATE USER

  1. CREATE USER user [IDENTIFIED BY [PASSWORD] 'password']
  2. [, user [IDENTIFIED BY [PASSWORD] 'password']] ...;

con cui si crea l'utente user che ha password 'password'. Per poter creare l'account si devono avere i privilegi create user o i privilegi insert per il database mysql. Per ciascun nuovo utente creato si creerà una nuova riga nella tabella mysql.user. Se l'utente già esisteva si verificherà un errore.
L'utente verrà identificato con un nome della forma seguente 'user'@'localhost', dove user è il nome dell'utente e localhost l'host da cui l'utente si connette a mysql.

Si può cancellare un account esistente tramite il comando DROP USER

  1. DROP USER user [, user] ...;

con cui si elimina l'utente user. Per poter rimuovere l'account si devono avere i privilegi di create user o di delete per il database mysql.
Da notare che questo comando rimuove l'account ma non chiude le sessioni che sono in quel momento aperte per quell'account.
DROP USER non invalida automaticamente tutti gli oggetti, tabelle, viste, trigger, ecc, creati dall'utente rimosso.

Il comando RENAME USER consente di rinominare account esistenti.

  1. RENAME USER old_user TO new_user [, old_user TO new_user] ...;

Per poterlo usare si devono avere i privilegi globali CREATE USER o i privilegi UPDATE per il database mysql.
Da notare che questo comando non trasferisce i privilegi dal vecchio account al nuovo.

Il comando SET PASSWORD consente di impostare la password di un utente.

  1. SET PASSWORD [FOR user] = PASSWORD('some password');

Se non si usa la clausola FOR la password impostata è quella dell'utente corrente. Per impostare la password di un altro utente è necessario avere i privilegi di UPDATE del database mysql.

Autorizzazioni e Privilegi

Il comando GRANT consente agli amministratori di creare degli account utente e di assegnare dei diritti a questi account. Per poter usare questo comando si devono avere i privilegi GRANT OPTION e i privilegi che si concedono all'utente.

  1. GRANT priv_type [(column_list)] [, priv_type [(column_list)]] ...
  2. ON [object_type] {tbl_name | * | *.* | db_name.*}
  3. TO user [IDENTIFIED BY [PASSWORD] 'password']
  4. [, user [IDENTIFIED BY [PASSWORD] 'password']] ...
  5. [REQUIRE
  6. NONE |
  7. [{SSL| X509}]
  8. [CIPHER 'cipher' [AND]]
  9. [ISSUER 'issuer' [AND]]
  10. [SUBJECT 'subject']]
  11. [WITH with_option [with_option] ...]

Si concedono i privilegi priv_type sull'oggetto object_type all'utente user eventualmente concedendo i privilegi GRANT OPTION o impostando dei limiti alle query, agli update, ecc.

  1. with_option =
  2. GRANT OPTION
  3. | MAX_QUERIES_PER_HOUR count
  4. | MAX_UPDATES_PER_HOUR count
  5. | MAX_CONNECTIONS_PER_HOUR count
  6. | MAX_USER_CONNECTIONS count

Queste opzioni limitano il numero di query, update e login che un utente può effettuare in un periodo di un'ora. Se count = 0 non ci sono limitazioni.
object_type può essere una tabella, una funzione o una procedura

  1. object_type =
  2. TABLE
  3. | FUNCTION
  4. | PROCEDURE


Il comando REVOKE consente agli amministratori di sistema di revocare i diritti concessi agli account.

  1. REVOKE priv_type [(column_list)] [, priv_type [(column_list)]] ...
  2. ON [object_type] {tbl_name | * | *.* | db_name.*}
  3. FROM user [, user] ...

Si revocano i privilegi priv_type sull'oggetto object_type concessi all'utente user. Per revocare tutti i privilegi concessi ad un utente si usa

  1. REVOKE ALL PRIVILEGES, GRANT OPTION FROM user [, user] ...

per poter usare quest'ultimo comando si devono avere i privilegi globali CREATE USER o i privilegi UPDATE per il database mysql.

Il comando SHOW GRANT visualizza i privilegi che sono stati concessi ad un determinato utente

  1. SHOW GRANTS [FOR user];

Per visualizzare i propri privilegi è sufficiente usare SHOW GRANTS;

I privilegi possono essere concessi a diversi livelli Per le strutture GRANT e REVOKE il campo priv_type può assumere i seguenti valori

Privilegi
Privilegio Descrizione
ALL [PRIVILEGES] Sets all simple privileges except GRANT OPTION
ALTER Enables use of ALTER TABLE
ALTER ROUTINE Enables stored routines to be altered or dropped
CREATE Enables use of CREATE TABLE
CREATE ROUTINE Enables creation of stored routines
CREATE TEMPORARY TABLES Enables use of CREATE TEMPORARY TABLE
CREATE USER Enables use of CREATE USER, DROP USER, RENAME USER, and REVOKE ALL PRIVILEGES.
CREATE VIEW Enables use of CREATE VIEW
DELETE Enables use of DELETE
DROP Enables use of DROP TABLE
EVENT Enables creation of events for the event scheduler
EXECUTE Enables the user to run stored routines
FILE Enables use of SELECT ... INTO OUTFILE and LOAD DATA INFILE
INDEX Enables use of CREATE INDEX and DROP INDEX
INSERT Enables use of INSERT
LOCK TABLES Enables use of LOCK TABLES on tables for which you have the SELECT privilege
PROCESS Enables the user to see all processes with SHOW PROCESSLIST
REFERENCES Not implemented
RELOAD Enables use of FLUSH
REPLICATION CLIENT Enables the user to ask where slave or master servers are
REPLICATION SLAVE Needed for replication slaves (to read binary log events from the master)
SELECT Enables use of SELECT
SHOW DATABASES SHOW DATABASES shows all databases
SHOW VIEW Enables use of SHOW CREATE VIEW
SHUTDOWN Enables use of mysqladmin shutdown
SUPER Enables use of CHANGE MASTER, KILL, PURGE MASTER LOGS, and SET GLOBAL statements, the mysqladmin debug command; allows you to connect (once) even if max_connections is reached
TRIGGER Enables the user to create or drop triggers
UPDATE Enables use of UPDATE
USAGE Synonym for “no privileges”
GRANT OPTION Enables privileges to be granted

I privilegi FILE, PROCESS, RELOAD, REPLICATION CLIENT, REPLICATION SLAVE, SHOW DATABASES, SHUTDOWN, SUPER, CREATE USER possono essere concessi solo a livello globale. Gli altri privilegi possono essere concessi globalmente o a livelli più specifici.
I privilegi che si possono concedere per una tabella sono SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, GRANT OPTION, INDEX, ALTER, CREATE VIEW, SHOW VIEW, TRIGGER.
I privilegi che si possono concedere per una colonna sono SELECT, INSERT, UPDATE. I privilegi che si possono concedere per il livello routine sono ALTER ROUTINE, EXECUTE, GRANT OPTION.