15-09-2021
This commit is contained in:
@@ -1,8 +0,0 @@
|
||||
# Fix the 2002 MySQL Socket error (OSX)
|
||||
|
||||
```bash
|
||||
$ sudo mkdir /var/mysql
|
||||
|
||||
$ sudo ln -s /tmp/mysql.sock /var/mysql/mysql.sock
|
||||
```
|
||||
|
||||
158
docs/MySQL/bases.md
Normal file
158
docs/MySQL/bases.md
Normal file
@@ -0,0 +1,158 @@
|
||||
# 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
|
||||
```
|
||||
|
||||
78
docs/MySQL/connexion.md
Normal file
78
docs/MySQL/connexion.md
Normal file
@@ -0,0 +1,78 @@
|
||||
# Connexion à la base:
|
||||
|
||||
|
||||
|
||||
### Se connecter à MySQL:
|
||||
|
||||
```bash
|
||||
$ mysql -u root -p
|
||||
Enter password:
|
||||
Welcome to the MariaDB monitor. Commands end with ; or \g.
|
||||
Your MariaDB connection id is 18
|
||||
Server version: 10.3.11-MariaDB Homebrew
|
||||
|
||||
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
|
||||
|
||||
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
|
||||
|
||||
MariaDB [(none)]>
|
||||
```
|
||||
|
||||
|
||||
|
||||
### Se connecter directement à une base:
|
||||
|
||||
```bash
|
||||
$ mysql -u root -p wordpress
|
||||
Enter password:
|
||||
Reading table information for completion of table and column names
|
||||
You can turn off this feature to get a quicker startup with -A
|
||||
|
||||
Welcome to the MariaDB monitor. Commands end with ; or \g.
|
||||
Your MariaDB connection id is 139
|
||||
Server version: 10.6.4-MariaDB Homebrew
|
||||
|
||||
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
|
||||
|
||||
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
|
||||
|
||||
MariaDB [wordpress]>
|
||||
```
|
||||
|
||||
|
||||
|
||||
`$ mysql -u root -pwordpress`: se connecter directement à MySQL avec le mot de passe 'wordpress'.
|
||||
|
||||
`$ mysql -u root -p wordpress`: se connecter à la base wordpress. Le mot de passe est demandé.
|
||||
|
||||
`$ mysql -u root -ppassword wordpress`: se connecter directement à la base wordpress avec le mot de passe 'password'.
|
||||
|
||||
|
||||
|
||||
### Configuration:
|
||||
|
||||
#### my.cnf
|
||||
|
||||
```bash
|
||||
bruno@SilverBook: /usr/local/etc $ ls -la
|
||||
total 160
|
||||
-rw-r--r-- 1 bruno admin 212 4 sep 16:53 my.cnf
|
||||
drwxr-xr-x 3 bruno admin 96 16 aoû 10:43 my.cnf.d
|
||||
-rw-r--r-- 1 bruno admin 113 4 sep 16:53 my.cnf.default
|
||||
```
|
||||
|
||||
```bash
|
||||
bruno@SilverBook: /usr/local/etc/my.cnf.d $ l
|
||||
total 0
|
||||
-rw-r--r-- 1 bruno admin 0 16 aoû 10:43 wont_prune.txt
|
||||
```
|
||||
|
||||
|
||||
|
||||
### Quitter MySQL:
|
||||
|
||||
```bash
|
||||
MariaDB [wordpress4]> exit
|
||||
Bye
|
||||
```
|
||||
|
||||
14
docs/MySQL/host.md
Normal file
14
docs/MySQL/host.md
Normal file
@@ -0,0 +1,14 @@
|
||||
# Host
|
||||
|
||||
|
||||
|
||||
Host est la machine à partir de laquelle l'utilisateur se connecte se connecte à la base.
|
||||
|
||||
|
||||
|
||||
| host | |
|
||||
| ---------------- | ------------------------------------------------------------ |
|
||||
| '%' | - depuis n'importe quel host (remote & local) (wildcard)<br />- n'inclue pas 'localhost' qui suppose un connexion par socket unix au lieu du standard TCP/IP |
|
||||
| 'localhost' | - uniquement des connections locales |
|
||||
| '198.51.100.177' | - depuis le host dont l'IP est '198.51.100.177' |
|
||||
|
||||
@@ -2,84 +2,36 @@
|
||||
|
||||
|
||||
|
||||
### Install MariaDB avec Homebrew:
|
||||
1. [Installation (Homebrew)](install.md)
|
||||
2. [Connexion](connexion.md)
|
||||
3. [Type de bases](type_bases.md)
|
||||
4. [Host](host.md)
|
||||
5. [Bases (requêtes)](bases.md)
|
||||
6. [Utilisateurs (requêtes)](users.md)
|
||||
7. [Privilèges (requêtes)](privileges.md)
|
||||
8. [Tables (requêtes)](tables.md)
|
||||
9. [select](select.md)
|
||||
10. [Réparer](repair.md)
|
||||
11. [Backup](Backup.md)
|
||||
12. [mysqlcheck](mysqlcheck.md)
|
||||
13. [Passwords](password.md)
|
||||
14. [Trucs](trucs.md)
|
||||
|
||||
|
||||
|
||||
```bash
|
||||
bruno@SilverBook: ~ $ sudo find / -name mysql
|
||||
Password:
|
||||
/usr/local/bin/mysql
|
||||
/usr/local/include/mysql
|
||||
/usr/local/etc/init.d/mysql
|
||||
/usr/local/etc/logrotate.d/mysql
|
||||
/usr/local/var/mysql
|
||||
/usr/local/var/mysql/mysql
|
||||
/usr/local/Cellar/mariadb/10.4.6_1/bin/mysql
|
||||
/usr/local/Cellar/mariadb/10.4.6_1/include/mysql
|
||||
/usr/local/Cellar/mariadb/10.4.6_1/include/mysql/server/mysql
|
||||
/usr/local/Cellar/mariadb/10.4.6_1/include/mysql/mysql
|
||||
/usr/local/Cellar/mariadb/10.4.6_1/.bottle/etc/init.d/mysql
|
||||
/usr/local/Cellar/mariadb/10.4.6_1/.bottle/etc/logrotate.d/mysql
|
||||
/usr/local/Cellar/mariadb/10.4.6_1/share/mysql
|
||||
/usr/local/share/mysql
|
||||
Voir le n° de version:
|
||||
|
||||
```mariadb
|
||||
MariaDB [(none)]> SELECT VERSION(), CURRENT_DATE;
|
||||
+----------------+--------------+
|
||||
| VERSION() | CURRENT_DATE |
|
||||
+----------------+--------------+
|
||||
| 10.6.4-MariaDB | 2021-09-04 |
|
||||
+----------------+--------------+
|
||||
```
|
||||
|
||||
|
||||
|
||||
#### my.cnf
|
||||
|
||||
```bash
|
||||
bruno@SilverBook: /usr/local/etc $ ls -la
|
||||
total 160
|
||||
-rw-r--r-- 1 bruno admin 212 4 sep 16:53 my.cnf
|
||||
drwxr-xr-x 3 bruno admin 96 16 aoû 10:43 my.cnf.d
|
||||
-rw-r--r-- 1 bruno admin 113 4 sep 16:53 my.cnf.default
|
||||
```
|
||||
|
||||
```bash
|
||||
bruno@SilverBook: /usr/local/etc/my.cnf.d $ l
|
||||
total 0
|
||||
-rw-r--r-- 1 bruno admin 0 16 aoû 10:43 wont_prune.txt
|
||||
```
|
||||
|
||||
|
||||
|
||||
#### Bases
|
||||
|
||||
```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
|
||||
```
|
||||
|
||||
|
||||
|
||||
[Reset Expired root Password for MySQL 5.7 on Mac OS X](Expired-root-Password.md)
|
||||
|
||||
[mysqlcheck](mysqlcheck.md)
|
||||
|
||||
[backup](Backup.md)
|
||||
|
||||
[Socket error](Socket-error.md)
|
||||
|
||||
[Commandes diverses](diverses.md)
|
||||
|
||||
|
||||
|
||||
### Liens:
|
||||
|
||||
[:fa-link: https://coolestguidesontheplanet.com/get-apache-mysql-php-and-phpmyadmin-working-on-macos-sierra/](https://coolestguidesontheplanet.com/get-apache-mysql-php-and-phpmyadmin-working-on-macos-sierra/)
|
||||
|
||||
55
docs/MySQL/install.md
Normal file
55
docs/MySQL/install.md
Normal file
@@ -0,0 +1,55 @@
|
||||
# Install MariaDB avec Homebrew:
|
||||
|
||||
#### Installation:
|
||||
|
||||
```bash
|
||||
$ brew install mariadb
|
||||
```
|
||||
|
||||
|
||||
|
||||
#### Démarrer le serveur:
|
||||
|
||||
```bash
|
||||
$ brew services start mariadb
|
||||
```
|
||||
|
||||
|
||||
|
||||
#### Changer le mot-de-passe MySQL:
|
||||
|
||||
```bash
|
||||
$ sudo /usr/local/bin/mysql_secure_installation
|
||||
```
|
||||
|
||||
|
||||
|
||||
#### Arrêter le serveur:
|
||||
|
||||
```bash
|
||||
$ brew services stop mariadb
|
||||
```
|
||||
|
||||
|
||||
|
||||
#### Fichiers installés:
|
||||
|
||||
```bash
|
||||
bruno@SilverBook: ~ $ sudo find / -name mysql
|
||||
Password:
|
||||
/usr/local/bin/mysql
|
||||
/usr/local/include/mysql
|
||||
/usr/local/etc/init.d/mysql
|
||||
/usr/local/etc/logrotate.d/mysql
|
||||
/usr/local/var/mysql
|
||||
/usr/local/var/mysql/mysql
|
||||
/usr/local/Cellar/mariadb/10.4.6_1/bin/mysql
|
||||
/usr/local/Cellar/mariadb/10.4.6_1/include/mysql
|
||||
/usr/local/Cellar/mariadb/10.4.6_1/include/mysql/server/mysql
|
||||
/usr/local/Cellar/mariadb/10.4.6_1/include/mysql/mysql
|
||||
/usr/local/Cellar/mariadb/10.4.6_1/.bottle/etc/init.d/mysql
|
||||
/usr/local/Cellar/mariadb/10.4.6_1/.bottle/etc/logrotate.d/mysql
|
||||
/usr/local/Cellar/mariadb/10.4.6_1/share/mysql
|
||||
/usr/local/share/mysql
|
||||
```
|
||||
|
||||
150
docs/MySQL/privileges.md
Normal file
150
docs/MySQL/privileges.md
Normal file
@@ -0,0 +1,150 @@
|
||||
# Privileges:
|
||||
|
||||
|
||||
|
||||
Le contrôle d'accès de MySQL se fait en deux étapes :
|
||||
|
||||
- Etape 1 : Le serveur vérifie que vous êtes autorisé à vous connecter.
|
||||
- Etape 2 : En supposant que vous pouvez vous connecter, le serveur vérifie chaque requête que vous soumettez, pour vérifier si vous avez les droits suffisants pour l'exécuter. Par exemple, si vous sélectionnez des droits dans une table, ou effacez une table, le serveur s'assure que vous avez les droits de SELECT pour cette table, ou les droits de DROP, respectivement.
|
||||
|
||||
|
||||
|
||||
#### Les commandes les plus courantes:
|
||||
|
||||
- **ALL PRIVILEGES** – Donne à l'utilisateur de MySQL un total accès à une base de données désignée (ou un accès global à l'ensemble du système si aucune base de données n'est sélectionnée)
|
||||
- **CREATE** – Permet aux utilisateurs de créer des bases de données/tableaux
|
||||
- **ALTER** –
|
||||
|
||||
- **SELECT** – Permet aux utilisateurs de récupérer des données
|
||||
|
||||
- **INSERT** – Permet aux utilisateurs d’ajouter de nouvelles entrées dans les tableaux
|
||||
|
||||
- **UPDATE** – Permet aux utilisateurs de modifier les entrées existantes dans les tableaux
|
||||
|
||||
- **DELETE** – Permet aux utilisateurs de supprimer les entrées de la tableau
|
||||
|
||||
- **DROP** – Permet aux utilisateurs de supprimer des bases de données/tableaux entiers
|
||||
- **GRANT OPTION** – Permet d'accorder ou de supprimer les privilèges des autres utilisateurs
|
||||
|
||||
|
||||
|
||||
#### Liste des commandes:
|
||||
|
||||
```
|
||||
GRANT
|
||||
SELECT, INSERT, DROP, UPDATE, DELETE, CREATE, RELOAD, SHUTDOWN, PROCESS, FILE, REFERENCES, INDEX, ALTER, SHOW DATABASES, SUPER, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, REPLICATION SLAVE, BINLOG MONITOR, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, CREATE USER, EVENT, TRIGGER, CREATE TABLESPACE, DELETE HISTORY, SET USER, FEDERATED ADMIN, CONNECTION ADMIN, READ_ONLY ADMIN, REPLICATION SLAVE ADMIN, REPLICATION MASTER ADMIN, BINLOG ADMIN, BINLOG REPLAY, SLAVE MONITOR
|
||||
ON *.* TO
|
||||
```
|
||||
|
||||
|
||||
|
||||
#### Voir les droits de l'utilisateur courant:
|
||||
|
||||
```mysql
|
||||
MariaDB [(none)]> SHOW GRANTS;
|
||||
+----------------------------------------------------------------------------------------------+
|
||||
| Grants for root@localhost |
|
||||
+----------------------------------------------------------------------------------------------+
|
||||
| GRANT ALL PRIVILEGES ON *.* TO `root`@`localhost` IDENTIFIED VIA mysql_native_password USING '*1111111111111111111111111111111111' OR unix_socket WITH GRANT OPTION |
|
||||
| GRANT PROXY ON ''@'%' TO 'root'@'localhost' WITH GRANT OPTION |
|
||||
+----------------------------------------------------------------------------------------------+
|
||||
```
|
||||
|
||||
|
||||
|
||||
#### Voir les droits d'un utilisateur 'mysqlbackupuser' sur un host:
|
||||
|
||||
```mysql
|
||||
MariaDB [(none)]> SHOW GRANTS FOR 'mysqlbackupuser'@'localhost';
|
||||
+----------------------------------------------------------------------------------------------+
|
||||
| Grants for mysqlbackupuser@localhost |
|
||||
+----------------------------------------------------------------------------------------------+
|
||||
| GRANT SELECT, LOCK TABLES ON *.* TO `mysqlbackupuser`@`localhost` IDENTIFIED BY PASSWORD '*22222222222222222222222222222222222222222' |
|
||||
+----------------------------------------------------------------------------------------------+
|
||||
1 row in set (0.000 sec)
|
||||
```
|
||||
|
||||
Si le host n'est pas précisé, il s'agit de **'%'**
|
||||
|
||||
```mysql
|
||||
MariaDB [(none)]> SHOW GRANTS FOR 'mysqlbackupuser';
|
||||
ERROR 1141 (42000): There is no such grant defined for user 'mysqlbackupuser' on host '%'
|
||||
```
|
||||
|
||||
Les droits peuvent être différents d'une base à l'autre:
|
||||
|
||||
```mysql
|
||||
MariaDB [(none)]> SHOW GRANTS FOR 'the_user'@'localhost';
|
||||
+----------------------------------------------------------------------------------------------+
|
||||
| Grants for the_user@localhost |
|
||||
+----------------------------------------------------------------------------------------------+
|
||||
| GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, RELOAD, SHUTDOWN, PROCESS, FILE, REFERENCES, INDEX, ALTER, SHOW DATABASES, SUPER, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, REPLICATION SLAVE, BINLOG MONITOR, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, CREATE USER, EVENT, TRIGGER, CREATE TABLESPACE, DELETE HISTORY, SET USER, FEDERATED ADMIN, CONNECTION ADMIN, READ_ONLY ADMIN, REPLICATION SLAVE ADMIN, REPLICATION MASTER ADMIN, BINLOG ADMIN, BINLOG REPLAY, SLAVE MONITOR ON *.* TO `the_user`@`localhost` IDENTIFIED BY PASSWORD '*201716EF6717C367868F777B9C6E17796F19F379' |
|
||||
|
|
||||
| GRANT ALL PRIVILEGES ON `bdd`.* TO `the_user`@`localhost` |
|
||||
+----------------------------------------------------------------------------------------------+
|
||||
```
|
||||
|
||||
|
||||
|
||||
#### Donner tous les droits à l'utilisateur:
|
||||
|
||||
-sur une base:
|
||||
|
||||
```mariadb
|
||||
MariaDB [(none)]> GRANT ALL PRIVILEGES ON mydatabase.* TO the_user@localhost;
|
||||
Query OK, 0 rows affected (0.029 sec)
|
||||
```
|
||||
|
||||
-sur le tout le serveur:
|
||||
|
||||
```mariadb
|
||||
MariaDB [(none)]> GRANT ALL PRIVILEGES ON *.* TO the_user@localhost;
|
||||
Query OK, 0 rows affected (0.025 sec)
|
||||
```
|
||||
|
||||
Pour que les changements prennent effets:
|
||||
|
||||
```mysql
|
||||
MariaDB [(none)]> FLUSH PRIVILEGES;
|
||||
```
|
||||
|
||||
|
||||
|
||||
#### Révocation de privilèges:
|
||||
|
||||
`mysql> REVOKE ALL PRIVILEGES;`
|
||||
|
||||
```mariadb
|
||||
MariaDB [(none)]> REVOKE ALL PRIVILEGES ON *.* FROM the_user@localhost;
|
||||
Query OK, 0 rows affected (0.023 sec)
|
||||
```
|
||||
|
||||
`mysql> REVOKE <PRIVILEGES>;`
|
||||
|
||||
```mariadb
|
||||
MariaDB [(none)]> SHOW GRANTS FOR 'the_user'@'localhost';
|
||||
+----------------------------------------------------------------------------------------------+
|
||||
| Grants for the_user@localhost |
|
||||
+----------------------------------------------------------------------------------------------+
|
||||
| GRANT ALL PRIVILEGES ON *.* TO `the_user`@`localhost` IDENTIFIED BY PASSWORD '*201716EF6717C367868F777B9C6E17796F19F379' |
|
||||
+----------------------------------------------------------------------------------------------+
|
||||
|
||||
```
|
||||
|
||||
On supprime les privilèges DROP et SHUTDOWN:
|
||||
|
||||
```mariadb
|
||||
MariaDB [(none)]> REVOKE DROP, SHUTDOWN ON *.* FROM the_user@localhost;
|
||||
Query OK, 0 rows affected (0.022 sec)
|
||||
```
|
||||
|
||||
```mariadb
|
||||
MariaDB [(none)]> SHOW GRANTS FOR 'the_user'@'localhost';
|
||||
+----------------------------------------------------------------------------------------------+
|
||||
| Grants for the_user@localhost |
|
||||
+----------------------------------------------------------------------------------------------+
|
||||
| GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, RELOAD, PROCESS, FILE, REFERENCES, INDEX, ALTER, SHOW DATABASES, SUPER, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, REPLICATION SLAVE, BINLOG MONITOR, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, CREATE USER, EVENT, TRIGGER, CREATE TABLESPACE, DELETE HISTORY, SET USER, FEDERATED ADMIN, CONNECTION ADMIN, READ_ONLY ADMIN, REPLICATION SLAVE ADMIN, REPLICATION MASTER ADMIN, BINLOG ADMIN, BINLOG REPLAY, SLAVE MONITOR ON *.* TO `the_user`@`localhost` IDENTIFIED BY PASSWORD '*201716EF6717C367868F777B9C6E17796F19F379' |
|
||||
+----------------------------------------------------------------------------------------------+
|
||||
|
||||
```
|
||||
|
||||
179
docs/MySQL/repair.md
Normal file
179
docs/MySQL/repair.md
Normal file
@@ -0,0 +1,179 @@
|
||||
# Réparer des tables :
|
||||
|
||||
[MySQL Server logs](https://dev.mysql.com/doc/refman/5.7/en/server-logs.html)
|
||||
|
||||
La 1ere chose à faire en cas de problèmes est de consulter les fichiers logs. Ceux-çi sont généralement avec les tables:
|
||||
|
||||
```bash
|
||||
#osx (Homebrew):
|
||||
|
||||
$ cd /usr/local/var/mysql
|
||||
total 388560
|
||||
drwxr-xr-x 32 bruno admin 1024 28 nov 16:53 .
|
||||
drwxrwxr-x 13 bruno admin 416 16 fév 2018 ..
|
||||
-rw-rw---- 1 bruno admin 16384 28 nov 08:08 aria_log.00000001
|
||||
-rw-rw---- 1 bruno admin 52 28 nov 08:08 aria_log_control
|
||||
drwx------ 48 bruno admin 1536 1 déc 17:05 funnymac
|
||||
drwx------ 53 bruno admin 1696 1 déc 17:05 ghost_prod
|
||||
-rw-r----- 1 bruno admin 15114 28 nov 08:08 ib_buffer_pool
|
||||
-rw-rw---- 1 bruno admin 50331648 3 déc 07:19 ib_logfile0
|
||||
-rw-rw---- 1 bruno admin 50331648 24 oct 12:51 ib_logfile1
|
||||
-rw-rw---- 1 bruno admin 79691776 3 déc 07:19 ibdata1
|
||||
-rw-rw---- 1 bruno admin 12582912 28 nov 16:53 ibtmp1
|
||||
drwx------ 9 bruno admin 288 1 déc 17:05 mgpt
|
||||
-rw-rw---- 1 bruno admin 0 24 jul 2017 multi-master.info
|
||||
drwx------ 89 bruno admin 2848 1 déc 17:05 mysql
|
||||
drwx------ 3 bruno admin 96 24 jul 2017 performance_schema
|
||||
drwx------ 41 bruno admin 1312 1 déc 17:05 phpmyadmi
|
||||
-rw-rw---- 1 bruno admin 1271878 3 déc 07:19 silverbook.home.err
|
||||
-rw-rw---- 1 bruno admin 5 28 nov 16:53 silverbook.home.pid
|
||||
```
|
||||
|
||||
|
||||
|
||||
<u>Pour réparer, 2 solutions:</u>
|
||||
|
||||
**1) REPAIR TABLE:**
|
||||
|
||||
- ne nécessite pas l’arrêt de MySQL
|
||||
- uniquement base MyISAM
|
||||
|
||||
|
||||
|
||||
```bash
|
||||
MariaDB [zenphoto]> REPAIR TABLE _tags;
|
||||
+----------------+--------+----------+---------------------------------------------------------+
|
||||
| Table | Op | Msg_type | Msg_text |
|
||||
+----------------+--------+----------+---------------------------------------------------------+
|
||||
| zenphoto._tags | repair | note | The storage engine for the table doesn't support repair |
|
||||
+----------------+--------+----------+---------------------------------------------------------+
|
||||
|
||||
1 row in set (0.006 sec)
|
||||
```
|
||||
|
||||
Si l’erreur persiste, **vider le cache** (Flush all cache)
|
||||
|
||||
|
||||
|
||||
**2) mysqlcheck -r:**
|
||||
|
||||
- nécessite un arrêt du service MySQL
|
||||
|
||||
- uniquement base MyISAM
|
||||
|
||||
|
||||
|
||||
[MyISAM](https://dev.mysql.com/doc/refman/5.5/en/myisam-storage-engine.html):
|
||||
|
||||
```bash
|
||||
$ mysqlcheck -u root -ppassword -r zenphoto .comments
|
||||
|
||||
zenphoto..comments OK
|
||||
```
|
||||
|
||||
|
||||
|
||||
[InnoDB](https://dev.mysql.com/doc/refman/5.5/en/innodb-storage-engine.html):
|
||||
|
||||
```bash
|
||||
$ mysqlcheck -u root -ppassword -r zenphoto _tags
|
||||
|
||||
zenphoto._tags
|
||||
|
||||
note : The storage engine for the table doesn't support repair
|
||||
```
|
||||
|
||||
Pour les tables InnoDB, il faut [forcer la récupération](https://dev.mysql.com/doc/refman/5.5/en/forcing-innodb-recovery.html):
|
||||
|
||||
1) Arrêter **mysqld**:
|
||||
|
||||
2) Sauvegarder **mysql**:
|
||||
|
||||
```bash
|
||||
#Linux
|
||||
/var/lib/mysql/
|
||||
|
||||
#osx (Homebrew)
|
||||
/usr/local/var/mysql/
|
||||
```
|
||||
|
||||
3) Ajouter au fichier de configuration MySQL my.cnf:
|
||||
|
||||
```
|
||||
#osx (Homebrew): /usr/local/etc/my.cnf
|
||||
|
||||
[mysqld]
|
||||
innodb_force_recovery = 4
|
||||
```
|
||||
|
||||
4) Redémarrer **mysqld**:
|
||||
|
||||
5a) Faire un dump des tables de la base avec **SELECT ... INTO OUTFILE**:
|
||||
|
||||
```mysql
|
||||
MariaDB [zenphoto]> SELECT * FROM _tags INTO OUTFILE '/tmp/corrupted.txt';
|
||||
Query OK, 170 rows affected (0.011 sec)
|
||||
|
||||
# Pour exporter au format csv:
|
||||
MariaDB [zenphoto]> SELECT * FROM _tags INTO OUTFILE '/tmp/corrupted.csv' FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\r\n';
|
||||
Query OK, 170 rows affected (0.006 sec)
|
||||
|
||||
```
|
||||
|
||||
5b) Faire. un dump de toutes les tables:
|
||||
|
||||
```bash
|
||||
$ mysqldump -A -u root -ppassword > '/tmp/dump.sql';
|
||||
```
|
||||
|
||||
6) Parfois cela peut suffire. Sinon,
|
||||
|
||||
7) Supprimer les tables / bases corrompues (DROP ...)
|
||||
|
||||
8) Arrêter **mysqld**:
|
||||
|
||||
9) Supprimer les ib*:
|
||||
|
||||
```bash
|
||||
#Linux
|
||||
/var/lib/mysql/ib*
|
||||
|
||||
#osx (Homebrew)
|
||||
/usr/local/var/mysql/ib*
|
||||
```
|
||||
|
||||
10) Quitter le mode force recovery:
|
||||
|
||||
```
|
||||
[mysqld]
|
||||
#innodb_force_recovery = 0
|
||||
```
|
||||
|
||||
11) Redémarrer **mysqld**:
|
||||
|
||||
12) Restaurer les bases:
|
||||
|
||||
```bash
|
||||
$ mysql -u root -ppassword < dump.sql
|
||||
```
|
||||
|
||||
|
||||
|
||||
https://www.nixpal.com/mysql-innodb-corruption-repair-guide/
|
||||
|
||||
|
||||
|
||||
### Vérifier et réparer:
|
||||
|
||||
Vérifier toutes les bases et réparer les tables corrompues:
|
||||
|
||||
```bash
|
||||
$ mysqlcheck -u root -p -A --auto-repair
|
||||
|
||||
Enter password:
|
||||
|
||||
funnymac.download OK
|
||||
funnymac.downloads OK
|
||||
…/…
|
||||
```
|
||||
|
||||
320
docs/MySQL/select.md
Normal file
320
docs/MySQL/select.md
Normal file
@@ -0,0 +1,320 @@
|
||||
# select
|
||||
|
||||
|
||||
|
||||
La commande SELECT est utilisée pour récupérer des informations à partir d'une table.
|
||||
|
||||
La forme usuelle est : `SELECT colonne FROM table WHERE condition;`
|
||||
|
||||
|
||||
|
||||
#### Sélectionner toutes les colonnes d'une table:
|
||||
|
||||
`SELECT * FROM table;`
|
||||
|
||||
```mariadb
|
||||
MariaDB [mydatabase]> SELECT * FROM animal;
|
||||
+----------+--------+---------+------+------------+------------+
|
||||
| nom | maitre | espece | sexe | naissance | mort |
|
||||
+----------+--------+---------+------+------------+------------+
|
||||
| Puffball | Diane | hamster | f | 1999-03-30 | NULL |
|
||||
| Slim | Benny | serpent | f | 1996-03-30 | NULL |
|
||||
| Chirpy | Gwen | oiseau | m | 1996-03-10 | NULL |
|
||||
| Fluffy | Harold | chat | m | 1984-11-10 | NULL |
|
||||
| Bowser | Diane | chien | m | 1998-08-31 | 1995-07-29 |
|
||||
+----------+--------+---------+------+------------+------------+
|
||||
5 rows in set (0.001 sec)
|
||||
```
|
||||
|
||||
|
||||
|
||||
#### Sélectionner certaines colonnes d'une table:
|
||||
|
||||
`SELECT col1, col4 FROM table;`
|
||||
|
||||
```mariadb
|
||||
MariaDB [mydatabase]> SELECT nom,sexe FROM animal;
|
||||
+----------+------+
|
||||
| nom | sexe |
|
||||
+----------+------+
|
||||
| Puffball | f |
|
||||
| Slim | f |
|
||||
| Chirpy | m |
|
||||
| Fluffy | m |
|
||||
| Bowser | m |
|
||||
+----------+------+
|
||||
```
|
||||
|
||||
|
||||
|
||||
#### Sélectionner certaines lignes:
|
||||
|
||||
`SELECT col1, col4 FROM table WHERE col5="x";`
|
||||
|
||||
```mariadb
|
||||
MariaDB [mydatabase]> SELECT nom,sexe FROM animal WHERE sexe="m";
|
||||
+--------+------+
|
||||
| nom | sexe |
|
||||
+--------+------+
|
||||
| Chirpy | m |
|
||||
| Fluffy | m |
|
||||
| Bowser | m |
|
||||
+--------+------+
|
||||
```
|
||||
|
||||
Conditions WHERE:
|
||||
|
||||
- **=** : `WHERE sexe ="m"`
|
||||
- **>=** ; **>** : `WHERE naissance >= "1998-1-1"`
|
||||
- **<=** ; **<** : `WHERE naissance < "2000-1-1"`
|
||||
- **AND** : `WHERE naissance >= "1998-1-1" AND naissance < "2000-1-1"`
|
||||
- **OR** : `WHERE naissance >= "2000-1-1" OR naissance < "1998-1-1"`
|
||||
-
|
||||
|
||||
|
||||
|
||||
#### Afficher un résultat unique:
|
||||
|
||||
`SELECT DISTINCT col1 FROM table;`
|
||||
|
||||
```mariadb
|
||||
MariaDB [mydatabase]> SELECT DISTINCT sexe FROM animal;
|
||||
+------+
|
||||
| sexe |
|
||||
+------+
|
||||
| f |
|
||||
| m |
|
||||
+------+
|
||||
```
|
||||
|
||||
|
||||
|
||||
#### Trier les résultats:
|
||||
|
||||
`SELECT col1, col2 FROM table ORDER BY col2;`
|
||||
|
||||
`SELECT col1, col2 FROM table ORDER BY col2 DESC;`
|
||||
|
||||
```mariadb
|
||||
MariaDB [mydatabase]> SELECT nom, naissance FROM animal ORDER BY naissance DESC;
|
||||
+----------+------------+
|
||||
| nom | naissance |
|
||||
+----------+------------+
|
||||
| Puffball | 1999-03-30 |
|
||||
| Slim | 1996-03-30 |
|
||||
| Chirpy | 1996-03-10 |
|
||||
| Bowser | 1989-08-31 |
|
||||
| Fluffy | 1984-11-10 |
|
||||
+----------+------------+
|
||||
|
||||
```
|
||||
|
||||
!!! Tri sur plusieurs colonnes
|
||||
|
||||
|
||||
|
||||
#### Calcul sur les dates:
|
||||
|
||||
Alias: `AS`
|
||||
|
||||
```mariadb
|
||||
MariaDB [mydatabase]> SELECT nom, naissance, CURRENT_DATE,(YEAR(CURRENT_DATE)-YEAR(naissance)) - (RIGHT(CURRENT_DATE,5)<RIGHT(naissance,5)) AS age FROM animal ORDER BY nom;
|
||||
+----------+------------+--------------+------+
|
||||
| nom | naissance | CURRENT_DATE | age |
|
||||
+----------+------------+--------------+------+
|
||||
| Bowser | 1989-08-31 | 2021-09-04 | 32 |
|
||||
| Chirpy | 1996-03-10 | 2021-09-04 | 25 |
|
||||
| Fluffy | 1984-11-10 | 2021-09-04 | 36 |
|
||||
| Puffball | 1999-03-30 | 2021-09-04 | 22 |
|
||||
| Slim | 1996-03-30 | 2021-09-04 | 25 |
|
||||
+----------+------------+--------------+------+
|
||||
```
|
||||
|
||||
```mariadb
|
||||
MariaDB [mydatabase]> SELECT nom, naissance, MONTH(naissance) AS 'mois de naissance' FROM animal;
|
||||
+----------+------------+-------------------+
|
||||
| nom | naissance | mois de naissance |
|
||||
+----------+------------+-------------------+
|
||||
| Puffball | 1999-03-30 | 3 |
|
||||
| Slim | 1996-03-30 | 3 |
|
||||
| Chirpy | 1996-03-10 | 3 |
|
||||
| Fluffy | 1984-11-10 | 11 |
|
||||
| Bowser | 1989-08-31 | 8 |
|
||||
+----------+------------+-------------------+
|
||||
```
|
||||
|
||||
|
||||
|
||||
#### Valeur NULL:
|
||||
|
||||
Conceptuellement, NULL représente une valeur qui manque, ou une valeur inconnue. Utilisez les opérateurs IS NULL et IS NOT NULL.
|
||||
|
||||
Avec MySQL, 0 et NULL représentent le booléen faux, et tout le reste représente le booléen vrai. La valeur par défaut du booléen vrai issue d'une comparaison est 1.
|
||||
|
||||
Lorsque vous utilisez la clause ORDER BY, les valeurs NULL sont toujours triées en premier, même si vous utilisez l'attribut DESC.
|
||||
|
||||
Chercher les animaux morts:
|
||||
|
||||
```mariadb
|
||||
MariaDB [mydatabase]> SELECT nom, naissance, mort FROM animal WHERE mort IS NOT NULL;
|
||||
+--------+------------+------------+
|
||||
| nom | naissance | mort |
|
||||
+--------+------------+------------+
|
||||
| Bowser | 1989-08-31 | 1995-07-29 |
|
||||
+--------+------------+------------+
|
||||
```
|
||||
|
||||
|
||||
|
||||
#### Recherche de modèles:
|
||||
|
||||
`LIKE '_a'`: _ remplace n'importe quel caractère
|
||||
|
||||
`LIKE '___'`: trouve les noms de 3 caractères
|
||||
|
||||
`LIKE '%y'`: se termine par 'y'
|
||||
|
||||
`LIKE 'f%'`: commence par 'f'
|
||||
|
||||
`LIKE '%b%'`: contenant un 'b'
|
||||
|
||||
`NOT LIKE '%y'`: ne se termine pas par 'y'
|
||||
|
||||
```mariadb
|
||||
MariaDB [mydatabase]> SELECT * FROM animal WHERE nom LIKE "%y";
|
||||
+--------+--------+--------+------+------------+------+
|
||||
| nom | maitre | espece | sexe | naissance | mort |
|
||||
+--------+--------+--------+------+------------+------+
|
||||
| Chirpy | Gwen | oiseau | m | 1996-03-10 | NULL |
|
||||
| Fluffy | Harold | chat | m | 1984-11-10 | NULL |
|
||||
+--------+--------+--------+------+------------+------+
|
||||
```
|
||||
|
||||
```mariadb
|
||||
MariaDB [mydatabase]> SELECT * FROM animal WHERE nom LIKE "f%";
|
||||
+--------+--------+--------+------+------------+------+
|
||||
| nom | maitre | espece | sexe | naissance | mort |
|
||||
+--------+--------+--------+------+------------+------+
|
||||
| Fluffy | Harold | chat | m | 1984-11-10 | NULL |
|
||||
+--------+--------+--------+------+------------+------+
|
||||
```
|
||||
|
||||
```mariadb
|
||||
MariaDB [mydatabase]> SELECT * FROM animal WHERE nom LIKE "%b%";
|
||||
+----------+--------+---------+------+------------+------------+
|
||||
| nom | maitre | espece | sexe | naissance | mort |
|
||||
+----------+--------+---------+------+------------+------------+
|
||||
| Puffball | Diane | hamster | f | 1999-03-30 | NULL |
|
||||
| Bowser | Diane | chien | m | 1989-08-31 | 1995-07-29 |
|
||||
+----------+--------+---------+------+------------+------------+
|
||||
```
|
||||
|
||||
```mariadb
|
||||
MariaDB [mydatabase]> SELECT * FROM animal WHERE nom LIKE "______";
|
||||
+--------+--------+--------+------+------------+------------+
|
||||
| nom | maitre | espece | sexe | naissance | mort |
|
||||
+--------+--------+--------+------+------------+------------+
|
||||
| Chirpy | Gwen | oiseau | m | 1996-03-10 | NULL |
|
||||
| Fluffy | Harold | chat | m | 1984-11-10 | NULL |
|
||||
| Bowser | Diane | chien | m | 1989-08-31 | 1995-07-29 |
|
||||
+--------+--------+--------+------+------------+------------+
|
||||
```
|
||||
|
||||
|
||||
|
||||
#### Regex:
|
||||
|
||||
`REGEXP`
|
||||
|
||||
`NOT REGEXP`
|
||||
|
||||
Quelques caractéristiques des expressions régulières étendues sont :
|
||||
|
||||
- Le caractère ‘.’ trouve n'importe quel caractère.
|
||||
- Une classe de caractères ‘[...]’ trouve n'importe quel caractère contenu entre les crochets. Par exemple, la classe de caractères ‘[abc]’ trouve le caractère ‘a’, ‘b’, ou ‘c’. Pour définir un intervalle de caractères, utilisez un trait d'union. La classe de caractères ‘[a-z]’ trouvera n'importe quel caractère minuscule, tout comme la classe ‘[0-9]’ trouvera n'importe quel nombre.
|
||||
- Le caractère ‘*’ trouvera aucune ou plus d'instances du caractère qui le précède. Par exemple, ‘x*’ trouvera n'importe quel nombre de fois le caractère ‘x’, ‘[0-9]*’ trouvera n'importe quel nombre et ‘.*’ trouvera n'importe quel nombre de fois n'importe quel caractère.
|
||||
- Le modèle est trouvé s'il se produit n'importe où dans la valeur testée. (Les modèles SQL ne sont trouvés que s'ils sont présents en valeur entière.)
|
||||
- Pour ancrer un modèle de sorte qu'il soit trouvé au début ou à la fin de valeur testée, utilisez ‘^’ au début ou bien ‘$’ à la fin du modèle.
|
||||
|
||||
```mariadb
|
||||
MariaDB [mydatabase]> SELECT * FROM animal WHERE nom REGEXP "y$";
|
||||
+--------+--------+--------+------+------------+------+
|
||||
| nom | maitre | espece | sexe | naissance | mort |
|
||||
+--------+--------+--------+------+------------+------+
|
||||
| Chirpy | Gwen | oiseau | m | 1996-03-10 | NULL |
|
||||
| Fluffy | Harold | chat | m | 1984-11-10 | NULL |
|
||||
+--------+--------+--------+------+------------+------+
|
||||
|
||||
```
|
||||
|
||||
```mariadb
|
||||
MariaDB [mydatabase]> SELECT * FROM animal WHERE nom REGEXP "^f";
|
||||
+--------+--------+--------+------+------------+------+
|
||||
| nom | maitre | espece | sexe | naissance | mort |
|
||||
+--------+--------+--------+------+------------+------+
|
||||
| Fluffy | Harold | chat | m | 1984-11-10 | NULL |
|
||||
+--------+--------+--------+------+------------+------+
|
||||
```
|
||||
|
||||
```mariadb
|
||||
MariaDB [mydatabase]> SELECT * FROM animal WHERE nom REGEXP "b";
|
||||
+----------+--------+---------+------+------------+------------+
|
||||
| nom | maitre | espece | sexe | naissance | mort |
|
||||
+----------+--------+---------+------+------------+------------+
|
||||
| Puffball | Diane | hamster | f | 1999-03-30 | NULL |
|
||||
| Bowser | Diane | chien | m | 1989-08-31 | 1995-07-29 |
|
||||
+----------+--------+---------+------+------------+------------+
|
||||
```
|
||||
|
||||
```mariadb
|
||||
MariaDB [mydatabase]> SELECT * FROM animal WHERE nom REGEXP "^......$";
|
||||
+--------+--------+--------+------+------------+------------+
|
||||
| nom | maitre | espece | sexe | naissance | mort |
|
||||
+--------+--------+--------+------+------------+------------+
|
||||
| Chirpy | Gwen | oiseau | m | 1996-03-10 | NULL |
|
||||
| Fluffy | Harold | chat | m | 1984-11-10 | NULL |
|
||||
| Bowser | Diane | chien | m | 1989-08-31 | 1995-07-29 |
|
||||
+--------+--------+--------+------+------------+------------+
|
||||
```
|
||||
|
||||
|
||||
|
||||
#### Compter les lignes:
|
||||
|
||||
`COUNT()`
|
||||
|
||||
La fonction COUNT() compte le nombre de résultats non NULL
|
||||
|
||||
```mariadb
|
||||
MariaDB [mydatabase]> SELECT COUNT(*) FROM animal;
|
||||
+----------+
|
||||
| COUNT(*) |
|
||||
+----------+
|
||||
| 5 |
|
||||
+----------+
|
||||
```
|
||||
|
||||
```mariadb
|
||||
MariaDB [mydatabase]> SELECT maitre, COUNT(*) FROM animal GROUP BY maitre;
|
||||
+--------+----------+
|
||||
| maitre | COUNT(*) |
|
||||
+--------+----------+
|
||||
| Benny | 1 |
|
||||
| Diane | 2 |
|
||||
| Gwen | 1 |
|
||||
| Harold | 1 |
|
||||
+--------+----------+
|
||||
```
|
||||
|
||||
|
||||
|
||||
#### Modifier une donnée:
|
||||
|
||||
`UPDATE table SET colonne1="x" WHERE colonne2="y";`
|
||||
|
||||
```mariadb
|
||||
MariaDB [mydatabase]> UPDATE animal SET naissance = "1989-08-31" WHERE nom = "Bowser";
|
||||
Query OK, 1 row affected (0.006 sec)
|
||||
Rows matched: 1 Changed: 1 Warnings: 0
|
||||
```
|
||||
|
||||
173
docs/MySQL/tables.md
Normal file
173
docs/MySQL/tables.md
Normal file
@@ -0,0 +1,173 @@
|
||||
# Tables:
|
||||
|
||||
|
||||
|
||||
### Voir les tables de la base courante:
|
||||
|
||||
`mysql [database]> SHOW TABLES;`
|
||||
|
||||
```mysql
|
||||
MariaDB [wordpress]> SHOW TABLES;
|
||||
+-----------------------+
|
||||
| Tables_in_wordpress |
|
||||
+-----------------------+
|
||||
| wp_commentmeta |
|
||||
| wp_comments |
|
||||
| wp_links |
|
||||
| wp_options |
|
||||
| wp_postmeta |
|
||||
| wp_posts |
|
||||
| wp_term_relationships |
|
||||
| wp_term_taxonomy |
|
||||
| wp_termmeta |
|
||||
| wp_terms |
|
||||
| wp_usermeta |
|
||||
| wp_users |
|
||||
+-----------------------+
|
||||
12 rows in set (0.003 sec)
|
||||
```
|
||||
|
||||
|
||||
|
||||
### Structure d’une table:
|
||||
|
||||
`mysql [database]> DESCRIBE table;`
|
||||
|
||||
```mysql
|
||||
MariaDB [wordpress]> DESCRIBE wp_terms;
|
||||
+------------+---------------------+------+-----+---------+----------------+
|
||||
| Field | Type | Null | Key | Default | Extra |
|
||||
+------------+---------------------+------+-----+---------+----------------+
|
||||
| term_id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
|
||||
| name | varchar(200) | NO | MUL | | |
|
||||
| slug | varchar(200) | NO | MUL | | |
|
||||
| term_group | bigint(10) | NO | | 0 | |
|
||||
+------------+---------------------+------+-----+---------+----------------+
|
||||
4 rows in set (0.006 sec)
|
||||
```
|
||||
|
||||
|
||||
|
||||
### Renommer une table:
|
||||
|
||||
`mysql [database]> ALTER TABLE table AS new_table;`
|
||||
|
||||
```mysql
|
||||
MariaDB [zenphoto]> ALTER TABLE _tags RENAME AS _new-tags;
|
||||
```
|
||||
|
||||
|
||||
|
||||
### Créer une table:
|
||||
|
||||
`mysql [database]> CREATE TABLE table;`
|
||||
|
||||
```mariadb
|
||||
MariaDB [mydatabase]> CREATE TABLE animal (nom VARCHAR(20), maitre VARCHAR(20), espece VARCHAR(20), sexe CHAR(1), naissance DATE, mort DATE);
|
||||
Query OK, 0 rows affected (0.138 sec)
|
||||
```
|
||||
|
||||
|
||||
|
||||
### Charger des données dans une table:
|
||||
|
||||
`mysql [database]> INSERT INTO table VALUES ('','');`
|
||||
|
||||
```mariadb
|
||||
MariaDB [mydatabase]> INSERT INTO animal VALUES ('Puffball','Diane','hamster','f','1999-03-30',NULL);
|
||||
Query OK, 1 row affected (0.012 sec)
|
||||
```
|
||||
|
||||
`mysql [database]> LOAD DATA LOCAL INFILE "file.txt" INTO TABLE table;`
|
||||
|
||||
```mariadb
|
||||
MariaDB [mydatabase]> LOAD DATA LOCAL INFILE "evenements.txt" INTO TABLE evenement;
|
||||
Query OK, 11 rows affected, 3 warnings (0.017 sec)
|
||||
Records: 11 Deleted: 0 Skipped: 0 Warnings: 3
|
||||
```
|
||||
|
||||
Le fichier .txt doit être dans le dossier qui contient les tables:
|
||||
|
||||
```bash
|
||||
/opt/homebrew/var/mysql/mydatabase master 13:26:16
|
||||
❯ ls -la
|
||||
total 208
|
||||
drwx------ 8 bruno admin 256 sep 6 13:26 .
|
||||
drwxr-xr-x 18 bruno admin 576 sep 4 08:00 ..
|
||||
-rw-rw---- 1 bruno admin 741 sep 4 09:17 animal.frm
|
||||
-rw-rw---- 1 bruno admin 98304 sep 4 09:17 animal.ibd
|
||||
-rw-rw---- 1 bruno admin 67 sep 4 08:00 db.opt
|
||||
-rw-rw---- 1 bruno admin 1377 sep 6 11:18 evenement.frm
|
||||
-rw-rw---- 1 bruno admin 98304 sep 6 11:18 evenement.ibd
|
||||
-rw-r--r-- 1 bruno admin 529 sep 6 13:26 evenements.txt
|
||||
|
||||
```
|
||||
|
||||
|
||||
|
||||
### Vérification des tables :
|
||||
|
||||
<u>2 solutions:</u>
|
||||
|
||||
**1) CHECK TABLE table;**
|
||||
|
||||
```mysql
|
||||
MariaDB [wordpress]> CHECK TABLE wp_terms;
|
||||
+--------------------+-------+----------+----------+
|
||||
| Table | Op | Msg_type | Msg_text |
|
||||
+--------------------+-------+----------+----------+
|
||||
| wordpress.wp_terms | check | status | OK |
|
||||
+--------------------+-------+----------+----------+
|
||||
1 row in set (0.008 sec)
|
||||
```
|
||||
|
||||
|
||||
|
||||
**2) [mysqlcheck](https://mariadb.com/kb/en/library/mysqlcheck/)**
|
||||
|
||||
mysqlcheck verrouille chaque table en lecture seule (la base est alors inaccessible pour les autres processus pendant ce temps) pour vérification ou réparation.
|
||||
|
||||
```bash
|
||||
❯ mysqlcheck -u root -ppassword wordpress
|
||||
wordpress.wp_commentmeta OK
|
||||
wordpress.wp_comments OK
|
||||
wordpress.wp_links OK
|
||||
wordpress.wp_options OK
|
||||
wordpress.wp_postmeta OK
|
||||
wordpress.wp_posts OK
|
||||
wordpress.wp_term_relationships OK
|
||||
wordpress.wp_term_taxonomy OK
|
||||
wordpress.wp_termmeta OK
|
||||
wordpress.wp_terms OK
|
||||
wordpress.wp_usermeta OK
|
||||
wordpress.wp_users OK
|
||||
```
|
||||
|
||||
|
||||
|
||||
### Optimiser:
|
||||
|
||||
Les bases InnoDB ne supportent pas l’option OPTIMIZE.
|
||||
|
||||
A la place,MySQL crée une nouvelle table, y copie toutes les lignes, efface l’ancienne table, renomme la nouvelle et lance ANALYSE
|
||||
|
||||
|
||||
|
||||
```bash
|
||||
❯ mysqlcheck -u root -psncfp1p2 -o --all-databases
|
||||
mysql.column_stats Table is already up to date
|
||||
mysql.columns_priv Table is already up to date
|
||||
mysql.db OK
|
||||
mysql.event Table is already up to date
|
||||
...
|
||||
wordpress.wp_commentmeta
|
||||
note : Table does not support optimize, doing recreate + analyze instead
|
||||
status : OK
|
||||
wordpress.wp_comments
|
||||
note : Table does not support optimize, doing recreate + analyze instead
|
||||
status : OK
|
||||
wordpress.wp_links
|
||||
note : Table does not support optimize, doing recreate + analyze instead
|
||||
status : OK
|
||||
```
|
||||
|
||||
@@ -1,4 +1,8 @@
|
||||
# Reset Expired root Password for MySQL 5.7 on Mac OS X
|
||||
# Trucs
|
||||
|
||||
|
||||
|
||||
### Reset Expired root Password for MySQL 5.7 on Mac OS X
|
||||
|
||||
1. Arrêter MySQL (PrefPane)
|
||||
|
||||
@@ -29,3 +33,13 @@ mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass';
|
||||
$ mysql -u root -p Enter password:
|
||||
```
|
||||
|
||||
|
||||
|
||||
### Fix the 2002 MySQL Socket error (OSX)
|
||||
|
||||
```bash
|
||||
$ sudo mkdir /var/mysql
|
||||
|
||||
$ sudo ln -s /tmp/mysql.sock /var/mysql/mysql.sock
|
||||
```
|
||||
|
||||
93
docs/MySQL/type_bases.md
Normal file
93
docs/MySQL/type_bases.md
Normal file
@@ -0,0 +1,93 @@
|
||||
# Types de bases (moteur de stockage):
|
||||
|
||||
|
||||
|
||||
### [InnoDB](https://dev.mysql.com/doc/refman/5.5/en/innodb-storage-engine.html)
|
||||
|
||||
Conseil: enable innodb_file_per_table = 1 option to put indexes and data for individual tables into distinct files.
|
||||
|
||||
Requête pour trouver les tables InnoDB parmi toutes les bases:
|
||||
|
||||
```mysql
|
||||
mysql> SELECT table_schema, table_name FROM INFORMATION_SCHEMA.TABLES WHERE engine = 'innodb';
|
||||
```
|
||||
|
||||
Constitution de Mysql (par défaut):
|
||||
|
||||
```bash
|
||||
bruno@silverbook:/usr/local/var/mysql$
|
||||
-rw-r----- 1 bruno admin 15114 28 nov 08:08 ib_buffer_pool
|
||||
-rw-rw---- 1 bruno admin 50331648 3 déc 10:35 ib_logfile0
|
||||
-rw-rw---- 1 bruno admin 50331648 24 oct 12:51 ib_logfile1
|
||||
-rw-rw---- 1 bruno admin 79691776 3 déc 10:35 ibdata1
|
||||
-rw-rw---- 1 bruno admin 12582912 28 nov 16:53 ibtmp1
|
||||
-rw-rw---- 1 bruno admin 1272770 3 déc 10:35 silverbook.home.err
|
||||
-rw-rw---- 1 bruno admin 5 28 nov 16:53 silverbook.home.pid
|
||||
```
|
||||
|
||||
- ibdata1: fichier de 10Mo extensible. InnoDB y stocke tout (bases,tables, index...) pour éviter la fragmentation.
|
||||
- ib_logfile0, ib_logfile1: 2 fichiers log de 5Mo.
|
||||
- ibtmp1
|
||||
- ib_buffer_pool:
|
||||
- silverbook.home.err: fichier log des erreurs
|
||||
|
||||
http://forum.wgpower.net/technique/innodb-fichier-ibdata1-trop-37009_1.html
|
||||
|
||||
https://vdachev.net/2007/02/22/mysql-reducing-ibdata1/
|
||||
|
||||
https://stackoverflow.com/questions/3456159/how-to-shrink-purge-ibdata1-file-in-mysql
|
||||
|
||||
|
||||
|
||||
### [MyISAM](https://dev.mysql.com/doc/refman/5.5/en/myisam-storage-engine.html)
|
||||
|
||||
|
||||
|
||||
### Connaitre le type de bases par défaut:
|
||||
|
||||
`mysql> SHOW ENGINES;`
|
||||
|
||||
```mysql
|
||||
MariaDB [(none)]> SHOW ENGINES;
|
||||
+--------------------+---------+-------------------------------------------------------------------------------------------------+--------------+------+------------+
|
||||
| Engine | Support | Comment | Transactions | XA | Savepoints |
|
||||
+--------------------+---------+-------------------------------------------------------------------------------------------------+--------------+------+------------+
|
||||
| CSV | YES | Stores tables as CSV files | NO | NO | NO |
|
||||
| MRG_MyISAM | YES | Collection of identical MyISAM tables | NO | NO | NO |
|
||||
| MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO |
|
||||
| Aria | YES | Crash-safe tables with MyISAM heritage. Used for internal temporary tables and privilege tables | NO | NO | NO |
|
||||
| MyISAM | YES | Non-transactional engine with good performance and small data footprint | NO | NO | NO |
|
||||
| SEQUENCE | YES | Generated tables filled with sequential values | YES | NO | YES |
|
||||
| InnoDB | DEFAULT | Supports transactions, row-level locking, foreign keys and encryption for tables | YES | YES | YES |
|
||||
| PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO |
|
||||
|
||||
```
|
||||
|
||||
<u>InnoDB est le moteur par défaut.</u>
|
||||
|
||||
|
||||
|
||||
### ENGINE utilisé pour une table:
|
||||
|
||||
`mysql> SELECT TABLE_NAME, ENGINE FROM information_schema.TABLES where TABLE_SCHEMA = 'database';`
|
||||
|
||||
```mysql
|
||||
MariaDB [(none)]> SELECT TABLE_NAME, ENGINE FROM information_schema.TABLES where TABLE_SCHEMA = 'wordpress';
|
||||
+-----------------------+--------+
|
||||
| TABLE_NAME | ENGINE |
|
||||
+-----------------------+--------+
|
||||
| wp_terms | InnoDB |
|
||||
| wp_commentmeta | InnoDB |
|
||||
| wp_term_taxonomy | InnoDB |
|
||||
| wp_usermeta | InnoDB |
|
||||
| wp_options | InnoDB |
|
||||
| wp_users | InnoDB |
|
||||
| wp_term_relationships | InnoDB |
|
||||
| wp_links | InnoDB |
|
||||
| wp_postmeta | InnoDB |
|
||||
| wp_termmeta | InnoDB |
|
||||
| wp_comments | InnoDB |
|
||||
| wp_posts | InnoDB |
|
||||
+-----------------------+--------+
|
||||
```
|
||||
|
||||
71
docs/MySQL/users.md
Normal file
71
docs/MySQL/users.md
Normal file
@@ -0,0 +1,71 @@
|
||||
# Utilisateurs
|
||||
|
||||
|
||||
|
||||
### Voir tous les utilisateurs MySQL:
|
||||
|
||||
ainsi que les hosts auxquels ils ont le droit de se connecter.
|
||||
|
||||
```mysql
|
||||
MariaDB [(none)]> SELECT user,host from mysql.user;
|
||||
+-----------------+-----------+
|
||||
| User | Host |
|
||||
+-----------------+-----------+
|
||||
| adm_wp | localhost |
|
||||
| bruno | localhost |
|
||||
| mariadb.sys | localhost |
|
||||
| mysqlbackupuser | localhost |
|
||||
| newuser | localhost |
|
||||
| root | localhost |
|
||||
+-----------------+-----------+
|
||||
6 rows in set (0.001 sec)
|
||||
```
|
||||
|
||||
on affiche également les mots-de-passe.
|
||||
|
||||
```mysql
|
||||
MariaDB [(none)]> SELECT host,user,password FROM mysql.user;
|
||||
|
||||
# MySQL 5.7+
|
||||
MariaDB [(none)]> SELECT host,user,authentication_string FROM mysql.user;
|
||||
```
|
||||
|
||||
|
||||
|
||||
### Créer un utilisateur:
|
||||
|
||||
`mysql> CREATE USER theuser;`
|
||||
|
||||
```mysql
|
||||
MariaDB [(none)]> CREATE USER 'theuser'@'localhost' IDENTIFIED BY 'the_password';
|
||||
```
|
||||
|
||||
Lui donner tous les droits:
|
||||
|
||||
```mysql
|
||||
MariaDB [(none)]> GRANT ALL PRIVILEGES ON * . * TO 'theuser'@'localhost';
|
||||
MariaDB [(none)]> FLUSH PRIVILEGES
|
||||
```
|
||||
|
||||
|
||||
|
||||
### Supprimer un utilisateur:
|
||||
|
||||
`mysql> DROP USER theuser;`
|
||||
|
||||
```mysql
|
||||
MariaDB [(none)]> DROP USER 'theuser'@'localhost';
|
||||
Query OK, 0 rows affected (0.023 sec)
|
||||
```
|
||||
|
||||
|
||||
|
||||
### Renommer un utilisateur:
|
||||
|
||||
`mysql> RENAME USER theuser;`
|
||||
|
||||
```mysql
|
||||
MariaDB [(none)]> RENAME USER 'theuser'@'localhost' TO 'the_user'@'localhost';
|
||||
Query OK, 0 rows affected (0.038 sec)
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user