Outils pour utilisateurs

Outils du site


linux:mysql (lu 65548 fois)

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
Prochaine révision Les deux révisions suivantes
linux:mysql [06-08-2019 11:14]
edmc73
linux:mysql [09-10-2019 10:55]
edmc73 [Sauvegarder]
Ligne 69: Ligne 69:
    
 /^DROP TABLE IF EXISTS `$table`/ .. /^UNLOCK TABLES;$/ and print; /^DROP TABLE IF EXISTS `$table`/ .. /^UNLOCK TABLES;$/ and print;
 +</code>
 +
 +Pour automatiser la connexion a mysql et ne pas avoir à taper de mot de passe, créer un fichier **~/.my.cnf**
 +<code>
 +[client]
 +user=root
 +password=somepassword
 </code> </code>
 ===== Logguer les requêtes en temps réel ponctuellement ===== ===== Logguer les requêtes en temps réel ponctuellement =====
Ligne 260: Ligne 267:
  
 Ça remarche ! et même à chaud sans redémarrer mysql ! J'ai même l'impression que ça a été plus rapide de faire cette manip que d'attendre la fin de la requête d'optimisation... Ça remarche ! et même à chaud sans redémarrer mysql ! J'ai même l'impression que ça a été plus rapide de faire cette manip que d'attendre la fin de la requête d'optimisation...
 +
 +
 +Autre cas un peu plus complexe ou il a fallu rajouté un --force pour que ça passe. 30 minutes environ de traitement et ça remarche.
 +
 +<code>
 +# myisamchk statistiques
 +Checking MyISAM file: statistiques
 +Data records: 43197952   Deleted blocks:       0
 +myisamchk: warning: Table is marked as crashed and last repair failed
 +myisamchk: warning: 1 client is using or hasn't closed the table properly
 +- check file-size
 +myisamchk: warning: Size of indexfile is: 2134406144      Should be: 1024
 +- check record delete-chain
 +- check key delete-chain
 +- check index reference
 +- check data record references index: 1
 +myisamchk: Unknown error 126
 +myisamchk: error: Can't read indexpage from filepos: -1
 +- check record links
 +myisamchk: error: Wrong bytesec: 0-0-0 at linkstart: 10578706084
 +MyISAM-table 'statistiques' is corrupted
 +Fix it using switch "-r" or "-o"
 +
 +
 +# myisamchk -r -v statistiques
 +- recovering (with sort) MyISAM-table 'statistiques'
 +Data records: 43197952
 +myisamchk: error: Can't create new tempfile: 'statistiques.TMD'
 +MyISAM-table 'statistiques' is not fixed because of errors
 +Try fixing it by using the --safe-recover (-o), the --force (-f) option or by not using the --quick (-q) flag
 +
 +# myisamchk -f -r -v statistiques
 +- recovering (with sort) MyISAM-table 'statistiques'
 +Data records: 43197952
 +- Fixing index 1
 +  - Searching for keys, allocating buffer for 102782 keys
 +Wrong bytesec:   0-  0-  0 at 10578706084; Skipped
 +Wrong bytesec:   0-  0-  0 at 10578706104; Skipped
 +Wrong bytesec:   0-  0-  0 at 10578706124; Skipped
 +Wrong bytesec:   0-  0-  0 at 10578706144; Skipped
 +Wrong bytesec:   0-  0-  0 at 10578706164; Skipped
 +Wrong bytesec:   0-  0-  0 at 10578706184; Skipped
 +Wrong bytesec:   0-  0-  0 at 10578706204; Skipped
 +Wrong bytesec:   0-  0-  0 at 10578706224; Skipped
 +Wrong bytesec:   0-  0-  0 at 10578706244; Skipped
 +Wrong bytesec:   0-  0-  0 at 10578706264; Skipped
 +Wrong bytesec:   0-  0-  0 at 10578706284; Skipped
 +Wrong bytesec:   0-  0-  0 at 10578706304; Skipped
 +Wrong bytesec:   0-  0-  0 at 10578706324; Skipped
 +Wrong bytesec:   0-  0-  0 at 10578706344; Skipped
 +Wrong bytesec:   0-  0-  0 at 10578706364; Skipped
 +Wrong bytesec:   0-  0-  0 at 10578706384; Skipped
 +Wrong bytesec:   0-  0-  0 at 10578706404; Skipped
 +Wrong bytesec:   0-  0-  0 at 10578706424; Skipped
 +  - Merging 43197952 keys
 +  - Last merge and dumping keys
 +- Fixing index 2
 +  - Searching for keys, allocating buffer for 94325 keys
 +  - Merging 43197952 keys
 +  - Last merge and dumping keys
 +- Fixing index 3
 +  - Searching for keys, allocating buffer for 16873 keys
 +  - Merging 43197952 keys
 +  - Last merge and dumping keys
 +- Fixing index 4
 +  - Searching for keys, allocating buffer for 115510 keys
 +  - Merging 43197952 keys
 +  - Last merge and dumping keys
 +- Fixing index 5
 +  - Searching for keys, allocating buffer for 66644 keys
 +  - Merging 43197952 keys
 +  - Last merge and dumping keys
 +
 +</code>
 +
 +
 +===== Strict Mode =====
 +
 +Bon à savoir si jamais vous copiez des dates en 0000-00-00 et que vous avez une erreur
 +
 +  SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect date value: '0000-00-00' for column...
 +
 +Strict mode affects whether the server permits '0000-00-00' as a valid date: If strict mode is not enabled, '0000-00-00' is permitted and inserts produce no warning. If strict mode is enabled, '0000-00-00' is not permitted and inserts produce an error, unless IGNORE is given as well. For INSERT IGNORE and UPDATE IGNORE, '0000-00-00' is permitted and inserts produce a warning.
 +
 +===== Log binaire =====
 +
 +Utiliser des log binaire pour mettre à jour une base de données distante après avoir fait un mysqldump
 +
 +Vérifier si les log binaires sont activé
 +
 +<code>
 +MariaDB [(none)]> SHOW BINARY LOGS;
 ++------------------+-----------+
 +| Log_name         | File_size |
 ++------------------+-----------+
 +| mysql-bin.000001 |       328 |
 ++------------------+-----------+
 +1 row in set (0.000 sec)
 +
 +
 +MariaDB [(none)]> show variables like 'log_bin%';
 ++---------------------------------+--------------------------------+
 +| Variable_name                   | Value                          |
 ++---------------------------------+--------------------------------+
 +| log_bin                         | ON                             |
 +| log_bin_basename                | /var/log/mysql/mysql-bin       |
 +| log_bin_compress                | OFF                            |
 +| log_bin_compress_min_len        | 256                            |
 +| log_bin_index                   | /var/log/mysql/mysql-bin.index |
 +| log_bin_trust_function_creators | OFF                            |
 ++---------------------------------+--------------------------------+
 +6 rows in set (0.001 sec)
 +
 +</code>
 +
 +Si non
 +<code>
 +mysql> SHOW BINARY LOGS;
 +ERROR 1381 (HY000): You are not using binary logging
 +</code>
 +
 +Pour activer dans le cas de mariaDB
 +  vi /etc/mysql/mariadb.conf.d/50-server.cnf
 +Décommenter la ligne
 +  log_bin                = /var/log/mysql/mysql-bin.log
 +et restarter mariaDB
 +  systemctl restart mariadb.service
 +
 +
 +Vos fichiers de log binaires sont maintenant stocké dans /var/log/mysql/
 +
 +**mysqlbinlog** permet de lire les fichiers de log binaire et de générer une sortie sql
 +<code>
 +# mysqlbinlog mysql-bin.000001 
 +mysqlbinlog: unknown variable 'default-character-set=utf8mb4'
 +
 +---------- Si vous avez ce message d'erreur, utilisez l'option suivante
 +
 +# mysqlbinlog --no-defaults mysql-bin.000001 
 +</code>
 +
 +Filtre possible:
 +  * ''--database=ma_base''  ou ''-d ma_base''
 +  * ''--start-datetime="2019-09-09 12:15:00"''  supérieur ou égal à cette date
 +  * ''--stop-datetime="2019-12-24 13:15:00"''   inférieur à cette date
 +  * ''--start-position=N''   commence à la position N
 +  * ''--stop-position=N''  s'arrête à la position N
 +
 +example de restauration
 +  mysqlbinlog /var/log/mysql/mysql-bin.[0-9]* | mysql -u root -p
 +
 +  mysqlbinlog --no-defaults --start-datetime="2019-09-09 12:15:00" /var/log/mysql/mysql-bin.[0-9]* | mysql -u root -p
 +
 +Par défaut, mysql s'arrête dès la 1ère erreur (par ex: duplicate entry), pour continuer après une erreur, ajouté l'option -f
 +
 +  mysqlbinlog --no-defaults --start-datetime="2019-09-09 12:15:00" /var/log/mysql/mysql-bin.[0-9]* | mysql -u root -p -f
  
linux/mysql.txt · Dernière modification: 14-02-2023 11:39 de edmc73