Per gestire un DB MySQL da terminale senza nessuno strumento (phpMyAdmin, MySQL fron ecc. ecc.), è necessario procedere come segue:
mysql -u root -p
viene richiesta la password, nel nostro esempio quella di ‘root’ e si entra nella console di mysql.
Da qui è possibile lanciare comandi e query mysql direttamente.
Vediamo alcuni esempi (ricordiamo che la digitazione del comando può essere interrotta andando a capo, per cui al temine è necessario sempre digitare il ‘;’ per lanciarne l’esecuzione).
Aggiungere un nuovo DB e dare all’utente ‘diego’ con password ‘ciccio’ tutti i permessi su quel particolare DB
mysql> create database nuovo_db; Query OK, 1 row affected (0.00 sec) mysql> grant ALL PRIVILEGES on nuovo_db.* mysql> to 'diego'@'localhost' mysql> IDENTIFIED BY 'ciccio' mysql> WITH GRANT OPTION; Query OK, 0 rows affected (0.00 sec) mysql> flush privileges;
Per uscire, anche negli esempi seguenti:
mysql> exit;
Al posto di “ALL PRIVILEGES” è possibile limitare l’utente utilizzando alternativamente o raggruppate le opzioni che seguono:
ALTER, CREATE, DELETE, DROP, INDEX, INSERT, LOCK TABLES, PROCESS, RELOAD, SELECT, SHOW DATABASES, UPDATE, GRANT OPTION
Maggiori info ovviamente su http://dev.mysql.com/doc/refman/5.1/en/grant.html
Lanciare una select su un DB
mysql> select * from biblio.anagrafica mysql> where Nome LIKE 'Dante'; ....
che avrà lo stesso effetto di:
mysql> show databases; +-------------+ | Database | +-------------+ | biblio | | discografia | | mysql | | test | +-------------+ 4 rows in set (0.00 sec) mysql> connect biblio; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Connection id: 877 Current database: biblio mysql> select * from anagrafica mysql> where Nome LIKE 'Dante'; ....
Visualizzare i DB presenti o le tabelle
mysql> show databases; .... mysql> connect biblio; ... mysql> show tables; ....
Per visualizzare i risultati di una query in colonna (utile per select di una riga o poche righe, con molte colonne) aggiungere ‘\G’
mysql> select * from tab where IT = 1; +----+--------+ | IT | desc | +----+--------+ | 1 | blabla | +----+--------+ 1 row in set (0.00 sec) mysql> select * from tab where IT = 1 \G; *********** 1. row ************* IT: 1 desc: blabla 1 row in set (0.00 sec)