technique:mysql

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Les deux révisions précédentes Révision précédente
Prochaine révision
Révision précédente
Dernière révisionLes deux révisions suivantes
technique:mysql [2018/04/06 23:10] francoisatechnique:mysql [2024/02/13 17:05] – [Restauration MySql] guillaumev
Ligne 2: Ligne 2:
  
 ===== Connexion ===== ===== Connexion =====
- 
-mysql ‐u nomutilisateur ‐p SHOW DATABASES; USE nomdelabdd; SHOW TABLES; DESCRIBE nomdelatable; 
- 
-===== Actions sur bases ===== 
- 
-CREATE DATABASE nomdelabdd; DROP DATABASE nomdelabdd; 
- 
-CREATE TABLE nomdelatable ( nomcolonne1 INT, nomcolonne2 FLOAT, nomcolonne3 VARCHAR(50), nomcolonne3 CHAR(50) ); 
- 
-DROP TABLE nomdelatable; 
- 
-''DELETE FROM nom_de_la_table WHERE nom_de_l'entree="valeur de l'entre"'' 
  
 Se connecter à MySQL en ligne de commande sous Linux : Se connecter à MySQL en ligne de commande sous Linux :
  
-<code>mysql ‐u nomutilisateur ‐p+<code> 
 +mysql ‐u nomutilisateur ‐p
  
 </code> </code>
Ligne 23: Ligne 12:
 Afficher les bases de données MySQL existantes : Afficher les bases de données MySQL existantes :
  
-<code><font inherit/inherit;;#204a87 font-weight: bold;;inherit>SHOW</font> <font inherit/inherit;;#000000;;inherit>DATABASES</font><font inherit/inherit;;#000000 font-weight: bold;;inherit>;</font>+<code> 
 +SHOW DATABASES;
  
 </code> </code>
Ligne 30: Ligne 20:
  
 <code> <code>
-USE<font inherit/inherit;;#f8f8f8 text-decoration: underline;;inherit></font>nomdelabdd;+USE nomdelabdd;
  
 </code> </code>
Ligne 37: Ligne 27:
  
 <code> <code>
-<font inherit/inherit;;#204a87 font-weight: bold;;inherit>SHOW</font> <font inherit/inherit;;#000000;;inherit>TABLES</font><font inherit/inherit;;#000000 font-weight: bold;;inherit>;</font>+SHOW TABLES;
  
 </code> </code>
Ligne 44: Ligne 34:
  
 <code> <code>
-<font inherit/inherit;;#204a87 font-weight: bold;;inherit>DESCRIBE</font> <font inherit/inherit;;#000000;;inherit>nomdelatable</font><font inherit/inherit;;#000000 font-weight: bold;;inherit>;</font>+DESCRIBE nomdelatable;
  
 </code> </code>
  
-=== Manipulation d'une base de données MySQL ===+Supprimer une table 
 + 
 +<code> 
 +DROP TABLE nomdelatable; 
 + 
 +</code> 
 + 
 +===== Manipulation d'une base de données MySQL =====
  
 Création de la base de données : Création de la base de données :
  
 <code> <code>
-<font inherit/inherit;;#204a87 font-weight: bold;;inherit>CREATE</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>DATABASE</font> <font inherit/inherit;;#000000;;inherit>nomdelabdd</font><font inherit/inherit;;#000000 font-weight: bold;;inherit>;</font>+CREATE DATABASE nomdelabdd;
  
 </code> </code>
Ligne 60: Ligne 57:
  
 <code> <code>
-<font inherit/inherit;;#204a87 font-weight: bold;;inherit>DROP</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>DATABASE</font> <font inherit/inherit;;#000000;;inherit>nomdelabdd</font><font inherit/inherit;;#000000 font-weight: bold;;inherit>;</font>+DROP DATABASE nomdelabdd;
  
 </code> </code>
  
-=== Manipulation des tables d'une base de données MySQL ===+===== Gestion des users ===== 
 + 
 +Création d'un utilisateur 
 + 
 +<code> 
 +create USER 'nomuser'@'localhost' IDENTIFIED BY 'motdepassesecret'; 
 + 
 +</code> 
 + 
 +Attribution des privilèges complets sur une base : 
 + 
 +<code> 
 +GRANT ALL PRIVILEGES ON nombase.* TO 'nomuser'@'localhost'; 
 +flush privileges; 
 + 
 +</code> 
 + 
 +Attribution des privilèges en consultation : 
 + 
 +<code> 
 +GRANT SELECT ON nombase.* TO 'nomuser'@'localhost'; 
 +flush privileges; 
 + 
 +</code> 
 + 
 +Suppression d'un user : 
 + 
 +<code> 
 +DROP USER 'nomuser@localhost'; 
 + 
 +</code> 
 + 
 +===== Manipulation des tables d'une base de données MySQL =====
  
 Création d'une table : Création d'une table :
  
 <code> <code>
-<font inherit/inherit;;#204a87 font-weight: bold;;inherit>CREATE</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>TABLE</font> <font inherit/inherit;;#000000;;inherit>nomdelatable</font> +CREATE TABLE nomdelatable ( nomcolonne1 INT , nomcolonne2 FLOAT , nomcolonne3 VARCHAR ( 50 ), nomcolonne3 CHAR ( 50 ) );
-<font inherit/inherit;;#000000 font-weight: bold;;inherit>(</font> +
-<font inherit/inherit;;#000000;;inherit>nomcolonne1</font> <font inherit/inherit;;#204a87;;inherit>INT</font><font inherit/inherit;;#000000 font-weight: bold;;inherit>,</font> +
-<font inherit/inherit;;#000000;;inherit>nomcolonne2</font> <font inherit/inherit;;#204a87;;inherit>FLOAT</font><font inherit/inherit;;#000000 font-weight: bold;;inherit>,</font> +
-<font inherit/inherit;;#000000;;inherit>nomcolonne3</font> <font inherit/inherit;;#204a87;;inherit>VARCHAR</font><font inherit/inherit;;#000000 font-weight: bold;;inherit>(</font><font inherit/inherit;;#0000cf font-weight: bold;;inherit>50</font><font inherit/inherit;;#000000 font-weight: bold;;inherit>),</font> +
-<font inherit/inherit;;#000000;;inherit>nomcolonne3</font> <font inherit/inherit;;#204a87;;inherit>CHAR</font><font inherit/inherit;;#000000 font-weight: bold;;inherit>(</font><font inherit/inherit;;#0000cf font-weight: bold;;inherit>50</font><font inherit/inherit;;#000000 font-weight: bold;;inherit>)</font> +
-<font inherit/inherit;;#000000 font-weight: bold;;inherit>);</font>+
  
 </code> </code>
Ligne 82: Ligne 105:
  
 <code> <code>
-<font inherit/inherit;;#204a87 font-weight: bold;;inherit>DROP</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>TABLE</font> <font inherit/inherit;;#000000;;inherit>nomdelatable</font><font inherit/inherit;;#000000 font-weight: bold;;inherit>;</font>+DROP TABLE nomdelatable ;
  
 </code> </code>
  
-== Modification d'une table ==+==== Modification d'une table ====
  
 Ajouter une colonne à une table : Ajouter une colonne à une table :
  
 <code> <code>
-<font inherit/inherit;;#204a87 font-weight: bold;;inherit>ALTER</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>TABLE</font> <font inherit/inherit;;#000000;;inherit>nomdelatable</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>ADD</font> <font inherit/inherit;;#000000;;inherit>nomdecolonne</font> <font inherit/inherit;;#204a87;;inherit>INT</font><font inherit/inherit;;#000000 font-weight: bold;;inherit>;</font> +ALTER TABLE nomdelatable ADD nomdecolonne INT ; 
-<font inherit/inherit;;#204a87 font-weight: bold;;inherit>ALTER</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>TABLE</font> <font inherit/inherit;;#000000;;inherit>nomdelatable</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>ADD</font> <font inherit/inherit;;#000000;;inherit>nomdecolonne1</font> <font inherit/inherit;;#204a87;;inherit>VARCHAR</font><font inherit/inherit;;#000000 font-weight: bold;;inherit>(</font><font inherit/inherit;;#0000cf font-weight: bold;;inherit>50</font><font inherit/inherit;;#000000 font-weight: bold;;inherit>);</font>+ALTER TABLE nomdelatable ADD nomdecolonne1 VARCHAR ( 50 );
  
 </code> </code>
  
 <code> <code>
-<font inherit/inherit;;#204a87 font-weight: bold;;inherit>ALTER</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>TABLE</font> <font inherit/inherit;;#000000;;inherit>nomdelatable</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>ADD</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>COLUMN</font> <font inherit/inherit;;#000000;;inherit>nomdelacolonne</font> <font inherit/inherit;;#204a87;;inherit>INT</font><font inherit/inherit;;#000000 font-weight: bold;;inherit>;</font>+ALTER TABLE nomdelatable ADD COLUMN nomdelacolonne INT ;
  
 </code> </code>
Ligne 104: Ligne 127:
  
 <code> <code>
-<font inherit/inherit;;#204a87 font-weight: bold;;inherit>ALTER</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>TABLE</font> <font inherit/inherit;;#000000;;inherit>nomdelatable</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>DROP</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>COLUMN</font> <font inherit/inherit;;#000000;;inherit>nomdecolonne1</font><font inherit/inherit;;#000000 font-weight: bold;;inherit>;</font>+ALTER TABLE nomdelatable DROP COLUMN nomdecolonne1 ;
  
 </code> </code>
Ligne 111: Ligne 134:
  
 <code> <code>
-<font inherit/inherit;;#204a87 font-weight: bold;;inherit>ALTER</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>TABLE</font> <font inherit/inherit;;#000000;;inherit>nomdelatable</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>RENAME</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>AS</font> <font inherit/inherit;;#000000;;inherit>lenouveaunomdelatable</font>+ALTER TABLE nomdelatable RENAME AS lenouveaunomdelatable
  
 </code> </code>
  
 <code> <code>
-<font inherit/inherit;;#204a87 font-weight: bold;;inherit>ALTER</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>TABLE</font> <font inherit/inherit;;#000000;;inherit>nomancientable</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>RENAME</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>TO</font> <font inherit/inherit;;#000000;;inherit>nomnouveautable</font><font inherit/inherit;;#000000 font-weight: bold;;inherit>;</font>+ALTER TABLE nomancientable RENAME TO nomnouveautable ;
  
 </code> </code>
  
 <code> <code>
-<font inherit/inherit;;#204a87 font-weight: bold;;inherit>RENAME</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>TABLE</font> <font inherit/inherit;;#000000;;inherit>nomancientable</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>TO</font> <font inherit/inherit;;#000000;;inherit>nomnouveautable</font><font inherit/inherit;;#000000 font-weight: bold;;inherit>;</font>+RENAME TABLE nomancientable TO nomnouveautable ;
  
 </code> </code>
Ligne 128: Ligne 151:
  
 <code> <code>
-<font inherit/inherit;;#204a87 font-weight: bold;;inherit>ALTER</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>TABLE</font> <font inherit/inherit;;#000000;;inherit>nomdelatable</font> <font inherit/inherit;;#000000;;inherit>CHANGE</font> <font inherit/inherit;;#000000;;inherit>nomdelacolonne</font> <font inherit/inherit;;#000000;;inherit>nouveaunomdelacolonne</font> <font inherit/inherit;;#204a87;;inherit>INT</font><font inherit/inherit;;#000000 font-weight: bold;;inherit>;</font>+ALTER TABLE nomdelatable CHANGE nomdelacolonne nouveaunomdelacolonne INT ;
  
 </code> </code>
Ligne 135: Ligne 158:
  
 <code> <code>
-<font inherit/inherit;;#204a87 font-weight: bold;;inherit>ALTER</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>TABLE</font> <font inherit/inherit;;#000000;;inherit>nomdelatable</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>ADD</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>PRIMARY</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>KEY</font> <font inherit/inherit;;#000000 font-weight: bold;;inherit>(</font><font inherit/inherit;;#000000;;inherit>nomcolonne</font><font inherit/inherit;;#000000 font-weight: bold;;inherit>);</font>+ALTER TABLE nomdelatable ADD PRIMARY KEY ( nomcolonne );
  
 </code> </code>
  
 <code> <code>
-<font inherit/inherit;;#204a87 font-weight: bold;;inherit>ALTER</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>TABLE</font> <font inherit/inherit;;#000000;;inherit>nomdelatable</font> +ALTER TABLE nomdelatable ADD CONSTRAINT pk_nomdelacontrainte PRIMARY KEY ( nomcolonne );
-<font inherit/inherit;;#204a87 font-weight: bold;;inherit>ADD</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>CONSTRAINT</font> <font inherit/inherit;;#000000;;inherit>pk_nomdelacontrainte</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>PRIMARY</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>KEY</font> <font inherit/inherit;;#000000 font-weight: bold;;inherit>(</font><font inherit/inherit;;#000000;;inherit>nomcolonne</font><font inherit/inherit;;#000000 font-weight: bold;;inherit>);</font>+
  
 </code> </code>
Ligne 148: Ligne 170:
  
 <code> <code>
-<font inherit/inherit;;#204a87 font-weight: bold;;inherit>ALTER</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>TABLE</font> <font inherit/inherit;;#000000;;inherit>nomdelatable</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>DROP</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>PRIMARY</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>KEY</font><font inherit/inherit;;#000000 font-weight: bold;;inherit>;</font>+ALTER TABLE nomdelatable DROP PRIMARY KEY ;
  
 </code> </code>
Ligne 155: Ligne 177:
  
 <code> <code>
-<font inherit/inherit;;#204a87 font-weight: bold;;inherit>ALTER</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>TABLE</font> <font inherit/inherit;;#000000;;inherit>nomdelatable</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>ADD</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>CONSTRAINT</font> <font inherit/inherit;;#000000;;inherit>fk_nomdelacontrainte</font> +ALTER TABLE nomdelatable ADD CONSTRAINT fk_nomdelacontrainte FOREIGN KEY ( nomcolonne ) REFERENCES tabledereference ( nomcolonne );
-<font inherit/inherit;;#204a87 font-weight: bold;;inherit>FOREIGN</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>KEY</font> <font inherit/inherit;;#000000 font-weight: bold;;inherit>(</font><font inherit/inherit;;#000000;;inherit>nomcolonne</font><font inherit/inherit;;#000000 font-weight: bold;;inherit>)</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>REFERENCES</font> <font inherit/inherit;;#000000;;inherit>tabledereference</font><font inherit/inherit;;#000000 font-weight: bold;;inherit>(</font><font inherit/inherit;;#000000;;inherit>nomcolonne</font><font inherit/inherit;;#000000 font-weight: bold;;inherit>);</font>+
  
 </code> </code>
Ligne 163: Ligne 184:
  
 <code> <code>
-<font inherit/inherit;;#204a87 font-weight: bold;;inherit>ALTER</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>TABLE</font> <font inherit/inherit;;#000000;;inherit>nomdelatable</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>DROP</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>CONSTRAINT</font> <font inherit/inherit;;#000000;;inherit>fk_nomdelacontrainte</font><font inherit/inherit;;#000000 font-weight: bold;;inherit>;</font>+ALTER TABLE nomdelatable DROP CONSTRAINT fk_nomdelacontrainte ;
  
 </code> </code>
  
-=== Requêtes SQL de base ===+==== Requêtes SQL de base ====
  
 Afficher / sélectionner des enregistrements d'une table SQL : Afficher / sélectionner des enregistrements d'une table SQL :
  
 <code> <code>
-<font inherit/inherit;;#204a87 font-weight: bold;;inherit>SELECT</font> <font inherit/inherit;;#000000;;inherit>nomcolonne</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>FROM</font> <font inherit/inherit;;#000000;;inherit>nomtable</font><font inherit/inherit;;#000000 font-weight: bold;;inherit>;</font>+SELECT nomcolonne FROM nomtable ;
  
 </code> </code>
Ligne 179: Ligne 200:
  
 <code> <code>
-<font inherit/inherit;;#204a87 font-weight: bold;;inherit>SELECT</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>DISTINCT</font> <font inherit/inherit;;#000000;;inherit>nomcolonne</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>FROM</font> <font inherit/inherit;;#000000;;inherit>nomtable</font><font inherit/inherit;;#000000 font-weight: bold;;inherit>;</font>+SELECT DISTINCT nomcolonne FROM nomtable ;
  
 </code> </code>
Ligne 186: Ligne 207:
  
 <code> <code>
-<font inherit/inherit;;#204a87 font-weight: bold;;inherit>SELECT</font> <font inherit/inherit;;#000000;;inherit>nomcolonne</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>FROM</font> <font inherit/inherit;;#000000;;inherit>nomtable</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>WHERE</font> <font inherit/inherit;;#000000;;inherit>nomcolonne</font> <font inherit/inherit;;#ce5c00 font-weight: bold;;inherit>=</font> <font inherit/inherit;;#4e9a06;;inherit>'1'</font><font inherit/inherit;;#000000 font-weight: bold;;inherit>;</font> +SELECT nomcolonne FROM nomtable WHERE nomcolonne = '1'
-<font inherit/inherit;;#204a87 font-weight: bold;;inherit>SELECT</font> <font inherit/inherit;;#000000;;inherit>nomcolonne</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>FROM</font> <font inherit/inherit;;#000000;;inherit>nomtable</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>WHERE</font> <font inherit/inherit;;#000000;;inherit>nomcolonne</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>LIKE</font> <font inherit/inherit;;#4e9a06;;inherit>'%.com'</font><font inherit/inherit;;#000000 font-weight: bold;;inherit>;</font>+SELECT nomcolonne FROM nomtable WHERE nomcolonne LIKE '%.com' ;
  
 </code> </code>
Ligne 194: Ligne 215:
  
 <code> <code>
-<font inherit/inherit;;#204a87 font-weight: bold;;inherit>SELECT</font> <font inherit/inherit;;#000000;;inherit>colonne1</font><font inherit/inherit;;#000000 font-weight: bold;;inherit>,</font> <font inherit/inherit;;#000000;;inherit>colonne2</font><font inherit/inherit;;#000000 font-weight: bold;;inherit>,</font> <font inherit/inherit;;#000000;;inherit>colonne3</font> +SELECT colonne1 , colonne2 , colonne3  FROM table1 INNER JOIN table2 ON table1 . colonne1 = table2 . colonne3 WHERE colonne1 = 'cequejecherche'
-<font inherit/inherit;;#204a87 font-weight: bold;;inherit>FROM</font> <font inherit/inherit;;#000000;;inherit>table1</font> +
-<font inherit/inherit;;#204a87 font-weight: bold;;inherit>INNER</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>JOIN</font> <font inherit/inherit;;#000000;;inherit>table2</font> +
-<font inherit/inherit;;#204a87 font-weight: bold;;inherit>ON</font> <font inherit/inherit;;#000000;;inherit>table1</font><font inherit/inherit;;#000000 font-weight: bold;;inherit>.</font><font inherit/inherit;;#000000;;inherit>colonne1</font><font inherit/inherit;;#ce5c00 font-weight: bold;;inherit>=</font><font inherit/inherit;;#000000;;inherit>table2</font><font inherit/inherit;;#000000 font-weight: bold;;inherit>.</font><font inherit/inherit;;#000000;;inherit>colonne3</font> +
-<font inherit/inherit;;#204a87 font-weight: bold;;inherit>WHERE</font> <font inherit/inherit;;#000000;;inherit>colonne1</font> <font inherit/inherit;;#ce5c00 font-weight: bold;;inherit>=</font> <font inherit/inherit;;#4e9a06;;inherit>'cequejecherche'</font>+
  
 </code> </code>
Ligne 205: Ligne 222:
  
 <code> <code>
-<font inherit/inherit;;#204a87 font-weight: bold;;inherit>UPDATE</font> <font inherit/inherit;;#000000;;inherit>nomtable</font> +UPDATE nomtable SET nomcolonne1 = 'mavaleur' , nomcolonne2 = 'mavaleur2'
-<font inherit/inherit;;#204a87 font-weight: bold;;inherit>SET</font> <font inherit/inherit;;#000000;;inherit>nomcolonne1</font><font inherit/inherit;;#ce5c00 font-weight: bold;;inherit>=</font><font inherit/inherit;;#4e9a06;;inherit>'mavaleur'</font><font inherit/inherit;;#000000 font-weight: bold;;inherit>,</font><font inherit/inherit;;#000000;;inherit>nomcolonne2</font><font inherit/inherit;;#ce5c00 font-weight: bold;;inherit>=</font><font inherit/inherit;;#4e9a06;;inherit>'mavaleur2'</font>+
  
 </code> </code>
Ligne 213: Ligne 229:
  
 <code> <code>
-<font inherit/inherit;;#204a87 font-weight: bold;;inherit>UPDATE</font> <font inherit/inherit;;#000000;;inherit>nomtable</font> +UPDATE nomtable SET nomcolonne1 = 'mavaleur' , nomcolonne2 = 'mavaleur2' WHERE nomcolonne3 = '0'
-<font inherit/inherit;;#204a87 font-weight: bold;;inherit>SET</font> <font inherit/inherit;;#000000;;inherit>nomcolonne1</font><font inherit/inherit;;#ce5c00 font-weight: bold;;inherit>=</font><font inherit/inherit;;#4e9a06;;inherit>'mavaleur'</font><font inherit/inherit;;#000000 font-weight: bold;;inherit>,</font><font inherit/inherit;;#000000;;inherit>nomcolonne2</font><font inherit/inherit;;#ce5c00 font-weight: bold;;inherit>=</font><font inherit/inherit;;#4e9a06;;inherit>'mavaleur2'</font> +
-<font inherit/inherit;;#204a87 font-weight: bold;;inherit>WHERE</font> <font inherit/inherit;;#000000;;inherit>nomcolonne3</font> <font inherit/inherit;;#ce5c00 font-weight: bold;;inherit>=</font><font inherit/inherit;;#4e9a06;;inherit>'0'</font>+
  
 </code> </code>
Ligne 222: Ligne 236:
  
 <code> <code>
-<font inherit/inherit;;#204a87 font-weight: bold;;inherit>INSERT</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>INTO</font> <font inherit/inherit;;#000000;;inherit>nomtable</font> <font inherit/inherit;;#000000 font-weight: bold;;inherit>(</font><font inherit/inherit;;#000000;;inherit>colonne1</font><font inherit/inherit;;#000000 font-weight: bold;;inherit>,</font><font inherit/inherit;;#000000;;inherit>colonne2</font><font inherit/inherit;;#000000 font-weight: bold;;inherit>,</font><font inherit/inherit;;#000000;;inherit>colonne3</font><font inherit/inherit;;#000000 font-weight: bold;;inherit>)</font> +INSERT INTO nomtable ( colonne1 , colonne2 , colonne3 ) VALUES ( valeu1 , valeur2 , valeur3 );
-<font inherit/inherit;;#204a87 font-weight: bold;;inherit>VALUES</font> <font inherit/inherit;;#000000 font-weight: bold;;inherit>(</font><font inherit/inherit;;#000000;;inherit>valeu1</font><font inherit/inherit;;#000000 font-weight: bold;;inherit>,</font><font inherit/inherit;;#000000;;inherit>valeur2</font><font inherit/inherit;;#000000 font-weight: bold;;inherit>,</font><font inherit/inherit;;#000000;;inherit>valeur3</font><font inherit/inherit;;#000000 font-weight: bold;;inherit>);</font>+
  
 </code> </code>
Ligne 230: Ligne 243:
  
 <code> <code>
-<font inherit/inherit;;#204a87 font-weight: bold;;inherit>DELETE</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>FROM</font> <font inherit/inherit;;#000000;;inherit>nomtable</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>WHERE</font> <font inherit/inherit;;#000000;;inherit>nomcolonne</font><font inherit/inherit;;#ce5c00 font-weight: bold;;inherit>=</font><font inherit/inherit;;#4e9a06;;inherit>'lavaleur'</font>+DELETE FROM nomtable WHERE nomcolonne = 'lavaleur'
  
 </code> </code>
 +
 +Trouver la valeur Maximal d'une colonne et ne garder que celle ligne :
 +
 +<code>
 +SELECT MAX(macolonne) FROM YourTable
 +
 +</code>
 +
 +===== Sauvegardes =====
 +
 +Pour sauvegarder toutes les bases de données on utilise la commande suivante :
 +
 +<code>
 +mysqldump --user=mon_user --password=mon_password --all-databases> fichier_destination.sql
 +
 +</code>
 +
 +Pour sauvegarder une base de données précise :
 +
 +<code>
 +mysqldump --user=mon_user --password=mon_password --databases nom_de_la_base> fichier_destination.sql
 +
 +</code>
 +
 +Pour sauvegarder plusieurs bases de données :
 +
 +<code>
 +mysqldump --user=mon_user --password=mon_password --databases nom_de_la_base_1 nom_de_la_base_2> fichier_destination.sql
 +
 +</code>
 +
 +Pour sauvegarder une table précise :
 +
 +<code>
 +mysqldump --user=mon_user --password=mon_password --databases nom_de_la_base --tables nom_de_la_table> fichier_destination.sql
 +
 +</code>
 +
 +Pour sauvegarder plusieurs tables :
 +
 +<code>
 +mysqldump --user=mon_user --password=mon_password --databases nom_de_la_base --tables nom_de_la_table_1 nom_de_la_table_2> fichier_destination.sql
 +
 +</code>
 +
 +Pour tous ces types de sauvegarde mysql les fichiers générés sont en .sql. Cela dit on peut mettre l’extension que l’on veut comme .txt par exemple. Ca n’a pas d’importance. Les fichiers sont lisibles en clair et contiennent toutes les commandes SQL pour pouvoir faire une restauration mysql.
 +
 +Cela dit les fichiers .sql sont volumineux. Pour gagner de la place, on va les compresser à la volée lors de la sauvegarde.
 +
 +==== Compresser la sauvegarde mysql ====
 +
 +Pour compresser les données, nous allons utiliser gunzip après avoir invoqué mysqldump.
 +
 +Cela donnera des commandes du type :
 +
 +<code>
 +mysqldump <commandes> | gzip> fichier_destination.sql.gz
 +
 +</code>
 +
 +Exemple avec la commande de sauvegarde complète :
 +<code>
 +
 +mysqldump --user=mon_user --password=mon_password --all-databases | gzip> fichier_destination.sql.gz
 +
 +</code>
 +
 +A noter qu’avec cette méthode, la commande de sauvegarde mysql produit un seul fichier compressé. le fichier source n’existe plus.
 +
 +===== Restauration MySql =====
 +
 +Pour la restauration mysql on n’utilise plus la commande mysqldump. On utilise la commande mysql. La commande mysql va aller lire un fichier puis le restaurer en fonction de son contenu et de ses instructions.
 +
 +La restauration mysql peut se faire pour toutes les bases de données ou pour un base de données précise.
 +
 +Exemple sans distinction de base de données :
 +
 +<code>
 +mysql --user=mon_user --password=mon_password <fichier_source.sql
 +
 +</code>
 +
 +Autre exemple pour restaurer dans une base de données précise :
 +
 +<code>
 +mysql --user=mon_user --password=mon_password nom_de_la_base <fichier_source.sql
 +
 +</code>
 +===== Restauration d'une DB dans un docker =====
 +
 +Si notre base de données tourne dans un docker, il suffit d'appeler docker exec (ou docker compose exec) avec le paramètre -T pour pouvoir restaurer un dump :
 +
 +<code>
 +docker compose exec -T [nom du docker dans le compose] mysql limesurvey --user=password --password=password <dump.sql
 +
 +docker  exec -T [nom complet du docker] mysql limesurvey --user=password --password=password <dump.sql
 +
 +</code>
 +
 +
 +===== Backup complet SQL des bases =====
 +
 +Pour toutes les bases :
 +
 +<code>
 +mysqldump --user=root --password=XXX --all-databases> alldatabases-`date +%Y%m%d-%H%M`.sql
 +
 +</code>
 +
 +Script complet de backup à adapter :
 +
 +<code>
 +#!/usr/bin/env bash
 +
 +SQL_USER="root"
 +SQL_PASS="XXX"
 +SQL_HOST="localhost"
 +
 +FILE_PREFIX="backup_db"
 +
 +FILE_DATE=`date +%Y-%m-%d-%H-%M-%S`
 +TARGET_FILE_PATH="/root/Backups/"
 +mkdir -p "$TARGET_FILE_PATH"
 +
 +DATABASES=$(mysql -h $SQL_HOST -u $SQL_USER --password=$SQL_PASS -e "SHOW DATABASES;" | tr -d " | " | grep -v -e Database -e _schema -e mysql)
 +
 +for DB_NAME in "$DATABASES"; do
 +        mysqldump -h $SQL_HOST -u $SQL_USER --password=$SQL_PASS --single-transaction --skip-lock-tables "$DB_NAME"> "$TARGET_FILE_PATH""$FILE_PREFI$
 +        gzip "$TARGET_FILE_PATH""$FILE_PREFIX"."$DB_NAME"."$FILE_DATE".sql
 +done
 +
 +</code>
 +
  
  • technique/mysql.txt
  • Dernière modification : 2024/04/02 15:07
  • de 127.0.0.1