Installazione

Ubuntu:sudo apt-get install mysql-server

Centos: sudo yum install mysql-server

Start/Stop

Il servizio : /etc/init.d/mysql -h

Usage: /etc/init.d/mysql start|stop|restart|reload|force-reload|status

Accesso alla shell

mysql -u root -p
mysql -u root -p -h 127.0.0.1 -P 3306 

Creare/Cancellare un database

mysql>  CREATE DATABASE miodatabase;
Query OK, 1 row affected (0.00 sec)
mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| crsm_m2            |
| easytimesheet      |
| hdi-staging-db     |
| jaws               |
| lportal            |
| miodatabase        |
| mysql              |
| notifiche          |
| performance_schema |
| phpmyadmin         |
| sonar              |
+--------------------+
12 rows in set (0.00 sec)
mysql> DROP DATABASE miodatabase;
Query OK, 0 rows affected (0.01 sec)

Accedere ad un dababase

mysql>  USE miodatabase;
Database changed
mysql>  SHOW tables;
Empty set (0.00 sec)

Creare una tabella

mysql> CREATE TABLE miatabella (
    -> id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, 
    -> nome VARCHAR(20),
    -> cognome VARCHAR(30),
    -> anni INT, 
    -> compleanno DATE);
Query OK, 0 rows affected (0.00 sec)
mysql> SHOW tables;
+-----------------------+
| Tables_in_miodatabase |
+-----------------------+
| miatabella            |
+-----------------------+
1 row in set (0.00 sec)
mysql>  DESCRIBE miatabella;
+------------+-------------+------+-----+---------+----------------+
| Field      | Type        | Null | Key | Default | Extra          |
+------------+-------------+------+-----+---------+----------------+
| id         | int(11)     | NO   | PRI | NULL    | auto_increment |
| nome       | varchar(20) | YES  |     | NULL    |                |
| cognome    | varchar(30) | YES  |     | NULL    |                |
| anni       | int(11)     | YES  |     | NULL    |                |
| compleanno | date        | YES  |     | NULL    |                |
+------------+-------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)

Inserire dati

mysql> INSERT INTO `miatabella` (`id`,`nome`,`cognome`,`anni`,`compleanno`) VALUES (NULL, "Simon", "Usai",42, '1976-04-27');
Query OK, 1 row affected (0.00 sec)
mysql> SELECT * FROM miatabella;
+----+-------+---------+------+------------+
| id | nome  | cognome | anni | compleanno |
+----+-------+---------+------+------------+
|  1 | Simon | Usai    |   42 | 1976-04-27 |
+----+-------+---------+------+------------+
1 row in set (0.00 sec)

Modifica dei dati

mysql> INSERT INTO `miatabella` (`id`,`nome`,`cognome`,`anni`,`compleanno`) VALUES (NULL, "A", "A",1, '2001-01-01');
Query OK, 1 row affected (0.00 sec)
mysql> UPDATE `miatabella`  SET  `nome` = 'Bello', `cognome` = 'Fico'  WHERE `anni` = 1;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0
mysql> SELECT * FROM miatabella;
+----+-------+---------+------+------------+
| id | nome  | cognome | anni | compleanno |
+----+-------+---------+------+------------+
|  1 | Simon | Usai    |   42 | 1976-04-27 |
|  2 | Bello | Fico    |    1 | 2001-01-01 |
+----+-------+---------+------+------------+
2 rows in set (0.01 sec)

Aggiungere/Cancellare una colonna

mysql>  ALTER TABLE miatabella ADD email VARCHAR(40);
Query OK, 2 rows affected (0.08 sec)
Records: 2  Duplicates: 0  Warnings: 0
mysql>  DESCRIBE miatabella;
+------------+-------------+------+-----+---------+----------------+
| Field      | Type        | Null | Key | Default | Extra          |
+------------+-------------+------+-----+---------+----------------+
| id         | int(11)     | NO   | PRI | NULL    | auto_increment |
| nome       | varchar(20) | YES  |     | NULL    |                |
| cognome    | varchar(30) | YES  |     | NULL    |                |
| anni       | int(11)     | YES  |     | NULL    |                |
| compleanno | date        | YES  |     | NULL    |                |
| email      | varchar(40) | YES  |     | NULL    |                |
+------------+-------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)
mysql> ALTER TABLE miatabella DROP email;
Query OK, 2 rows affected (0.02 sec)
Records: 2  Duplicates: 0  Warnings: 0

Cancellare una riga

mysql> DELETE from miatabella  where anni=1;
Query OK, 1 row affected (0.01 sec)

Help

mysql> help

For information about MySQL products and services, visit:
   http://www.mysql.com/
For developer information, including the MySQL Reference Manual, visit:
   http://dev.mysql.com/
To buy MySQL Enterprise support, training, or other products, visit:
   https://shop.mysql.com/

List of all MySQL commands:
Note that all text commands must be first on line and end with ';'
?         (\?) Synonym for `help'.
clear     (\c) Clear the current input statement.
connect   (\r) Reconnect to the server. Optional arguments are db and host.
delimiter (\d) Set statement delimiter.
edit      (\e) Edit command with $EDITOR.
ego       (\G) Send command to mysql server, display result vertically.
exit      (\q) Exit mysql. Same as quit.
go        (\g) Send command to mysql server.
help      (\h) Display this help.
nopager   (\n) Disable pager, print to stdout.
notee     (\t) Don't write into outfile.
pager     (\P) Set PAGER [to_pager]. Print the query results via PAGER.
print     (\p) Print current command.
prompt    (\R) Change your mysql prompt.
quit      (\q) Quit mysql.
rehash    (\#) Rebuild completion hash.
source    (\.) Execute an SQL script file. Takes a file name as an argument.
status    (\s) Get status information from the server.
system    (\!) Execute a system shell command.
tee       (\T) Set outfile [to_outfile]. Append everything into given outfile.
use       (\u) Use another database. Takes database name as argument.
charset   (\C) Switch to another charset. Might be needed for processing binlog with multi-byte charsets.
warnings  (\W) Show warnings after every statement.
nowarning (\w) Don't show warnings after every statement.

For server side help, type 'help contents'

Status

mysql> \s;
--------------
mysql  Ver 14.14 Distrib 5.5.60, for debian-linux-gnu (x86_64) using readline 6.3

Connection id:		77
Current database:	
Current user:		root@localhost
SSL:			Not in use
Current pager:		stdout
Using outfile:		''
Using delimiter:	;
Server version:		5.5.60-0ubuntu0.14.04.1 (Ubuntu)
Protocol version:	10
Connection:		127.0.0.1 via TCP/IP
Server characterset:	latin1
Db     characterset:	latin1
Client characterset:	utf8
Conn.  characterset:	utf8
TCP port:		3306
Uptime:			5 hours 53 min 34 sec

Threads: 1  Questions: 259  Slow queries: 0  Opens: 386  Flush tables: 1  Open tables: 375  Queries per second avg: 0.012
--------------

ERROR: 
No query specified

Quit

mysql> quit;
Bye