# Bases



### Voir toutes les bases:

`mysql> SHOW DATABASES;`

```mysql
MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| wordpress          |
+--------------------+
```



### Utiliser une base existante:

`mysql> USE database;`

```mysql
MariaDB [(none)]> USE wordpress;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [wordpress]>
```



### Créer une base de donnée:

`mysql> CREATE DATABASE database;`

```mysql
MariaDB [(none)]> CREATE DATABASE bdd;
Query OK, 1 row affected (0.001 sec)
```

et donner les privilèges à un utilisateur (existant) sur cette base:

```mysql
MariaDB [(none)]> GRANT ALL ON bdd.* TO 'theuser'@'localhost';
Query OK, 0 rows affected (0.021 sec)

MariaDB [(none)]> FLUSH PRIVILEGES;
```

ou à un utilisateur qui n'existe pas encore:

```mysql
MariaDB [(none)]> GRANT ALL ON bdd.* TO 'matrix'@'localhost' IDENTIFIED BY 'password';
Query OK, 0 rows affected (0.030 sec)

MariaDB [(none)]> FLUSH PRIVILEGES;
```

L'utilisateur 'matrix' sera crée.



`mysql> CREATE DATABASE IF NOT EXISTS database;`

Avec `IF NOT EXISTS`, la base est créée uniquement si il n'y a pas de conflit de nom. Sans, MySQL envoie une erreur.

```mysql
MariaDB [(none)]> CREATE DATABASE IF NOT EXISTS bdd;
Query OK, 1 row affected (0.003 sec)
```



`CHARACTER SET`  `COLLATE`

```mariadb
MariaDB [(none)]> CREATE DATABASE mydatabase CHARACTER SET utf8 COLLATE utf8_general_ci;
Query OK, 1 row affected (0.005 sec)
```



### Supprimer une base:

`mysql> DROP DATABASE database;`

```mysql
MariaDB [(none)]> DROP DATABASE bdd;
Query OK, 0 rows affected (0.074 sec)
```

Ajouter `IF EXISTS` évite le message d'erreur si la base n'existe pas:

```mariadb
MariaDB [(none)]> DROP DATABASE IF EXISTS zenphoto;
Query OK, 0 rows affected, 1 warning (0.000 sec)
```

```mariadb
MariaDB [(none)]> DROP DATABASE zenphoto;
ERROR 1008 (HY000): Can't drop database 'zenphoto'; database doesn't exist
```



#### Bases dans macOS:

Les bases se trouvent dans: `$(brew --prefix)/var/mysql`

**mac intel:**

```bash
bruno@SilverBook: /usr/local/var/mysql $ l
total 245920
-rw-rw----   1 bruno  admin      6519 15 oct 17:48 SilverBook.local.err
-rw-rw----   1 bruno  admin         5 15 oct 17:48 SilverBook.local.pid
-rw-rw----   1 bruno  admin     24576 15 oct 17:46 aria_log.00000001
-rw-rw----   1 bruno  admin        52 15 oct 17:46 aria_log_control
-rw-r-----   1 bruno  admin       976 15 oct 17:46 ib_buffer_pool
-rw-rw----   1 bruno  admin  50331648 15 oct 17:48 ib_logfile0
-rw-rw----   1 bruno  admin  50331648  6 aoû 20:18 ib_logfile1
-rw-rw----   1 bruno  admin  12582912 15 oct 17:46 ibdata1
-rw-rw----   1 bruno  admin  12582912 15 oct 17:48 ibtmp1
-rw-rw----   1 bruno  admin         0  6 aoû 20:18 multi-master.info
drwx------  90 bruno  admin      2880  6 aoû 20:18 mysql
drwx------   3 bruno  admin        96  6 aoû 20:18 performance_schema
-rw-rw----   1 bruno  admin      3047 11 aoû 22:49 silverbook-1.home.err
-rw-rw----   1 bruno  admin     30926 15 oct 17:46 silverbook.home.err
```

**mac M1:**

```bash
❯ cd /opt/homebrew/var/mysql
total 123324
-rw-rw----   1 bruno admin      7308 sep  2 16:19 AirBook.err
-rw-rw----   1 bruno admin         5 aoû 11 23:09 airbook.pid
-rw-rw----   1 bruno admin    417792 sep  3 18:18 aria_log.00000001
-rw-rw----   1 bruno admin        52 aoû 11 22:49 aria_log_control
-rw-rw----   1 bruno admin     16384 sep  3 18:18 ddl_recovery.log
-rw-r-----   1 bruno admin       946 aoû 11 22:49 ib_buffer_pool
-rw-rw----   1 bruno admin 100663296 sep  3 18:18 ib_logfile0
-rw-rw----   1 bruno admin  12582912 aoû 11 22:49 ibdata1
-rw-rw----   1 bruno admin  12582912 aoû 11 23:09 ibtmp1
-rw-rw----   1 bruno admin         0 jul 29 09:44 multi-master.info
drwx------  90 bruno admin      2880 sep  3 18:18 mysql
drwx------   3 bruno admin        96 jul 29 09:43 performance_schema
drwx------ 109 bruno admin      3488 sep  3 18:18 sys
drwx------  27 bruno admin       864 sep  3 18:18 wordpress
```

