Ci-dessous, les différences entre deux révisions de la page.
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 [23-04-2019 17:34] edmc73 [Erreur] |
linux:mysql [09-02-2020 21:14] edmc73 [Sauvegarder] |
||
---|---|---|---|
Ligne 27: | Ligne 27: | ||
mysql> show variables; | mysql> show variables; | ||
mysql> show global variables like ' | mysql> show global variables like ' | ||
+ | |||
+ | Pour modifier une variable global | ||
+ | mysql> set global max_connections=500; | ||
Pour voir les status | Pour voir les status | ||
Ligne 50: | Ligne 53: | ||
Sauvegarder toutes les tables avec un prefix | Sauvegarder toutes les tables avec un prefix | ||
mysqldump -h hostname -u user -p $(mysql nom_de_la_base -h hostname -u user -p -Bse 'show tables like" | mysqldump -h hostname -u user -p $(mysql nom_de_la_base -h hostname -u user -p -Bse 'show tables like" | ||
+ | |||
+ | La bonne pratique en prod est d' | ||
+ | |||
+ | mysqldump -u USER -p --single-transaction --quick --lock-tables=false --all-databases (or) DATABASE | gzip > OUTPUT.sql.gz | ||
+ | |||
+ | * --quick : This option is useful for dumping large tables. It forces mysqldump to retrieve rows for a table from the server a row at a time rather than retrieving the entire row set and buffering it in memory before writing it out | ||
+ | |||
+ | * --single-transaction : This option sets the transaction isolation mode to REPEATABLE READ and sends a START TRANSACTION SQL statement to the server before dumping data. It is useful only with transactional tables such as InnoDB, because then it dumps the consistent state of the database at the time when START TRANSACTION was issued without blocking any applications. | ||
+ | |||
+ | * --lock-tables=false : option stops MyISAM tables (if they exsit) being locked during the backup | ||
Restaurer | Restaurer | ||
Ligne 66: | Ligne 79: | ||
/^DROP TABLE IF EXISTS `$table`/ .. /^UNLOCK TABLES;$/ and print; | /^DROP TABLE IF EXISTS `$table`/ .. /^UNLOCK TABLES;$/ and print; | ||
+ | </ | ||
+ | |||
+ | Pour automatiser la connexion a mysql et ne pas avoir à taper de mot de passe, créer un fichier **~/ | ||
+ | < | ||
+ | [client] | ||
+ | user=root | ||
+ | password=somepassword | ||
</ | </ | ||
===== Logguer les requêtes en temps réel ponctuellement ===== | ===== Logguer les requêtes en temps réel ponctuellement ===== | ||
Ligne 130: | Ligne 150: | ||
Plein d' | Plein d' | ||
+ | |||
+ | Un autre script dans les paquets **automysqlbackup** https:// | ||
+ | |||
===== Percona XtraBackup ===== | ===== Percona XtraBackup ===== | ||
Ligne 207: | Ligne 230: | ||
ERROR 144 (HY000) at line 1: Table ' | ERROR 144 (HY000) at line 1: Table ' | ||
</ | </ | ||
+ | |||
+ | Se placer dans le bon répertoire | ||
+ | |||
+ | cd / | ||
< | < | ||
- | # myisamchk | + | # myisamchk |
- | Checking MyISAM file: dashboard_log | + | Checking MyISAM file: MA_TABLE |
Data records: | Data records: | ||
myisamchk: warning: Table is marked as crashed and last repair failed | myisamchk: warning: Table is marked as crashed and last repair failed | ||
Ligne 226: | Ligne 253: | ||
myisamchk: warning: Found 1581466 deleted blocks | myisamchk: warning: Found 1581466 deleted blocks | ||
myisamchk: warning: Found 8799790 key parts. Should be: 654724 | myisamchk: warning: Found 8799790 key parts. Should be: 654724 | ||
- | MyISAM-table 'dashboard_log' is corrupted | + | MyISAM-table 'MA_TABLE' is corrupted |
Fix it using switch " | Fix it using switch " | ||
</ | </ | ||
< | < | ||
- | # myisamchk -r -v dashboard_log | + | # myisamchk -r -v MA_TABLE |
- | - recovering (with sort) MyISAM-table 'dashboard_log' | + | - recovering (with sort) MyISAM-table 'MA_TABLE' |
Data records: 654724 | Data records: 654724 | ||
- Fixing index 1 | - Fixing index 1 | ||
Ligne 251: | Ligne 278: | ||
Ça remarche ! et même à chaud sans redémarrer mysql ! J'ai même l' | Ça remarche ! et même à chaud sans redémarrer mysql ! J'ai même l' | ||
+ | |||
+ | 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. | ||
+ | |||
+ | < | ||
+ | # myisamchk statistiques | ||
+ | Checking MyISAM file: statistiques | ||
+ | Data records: 43197952 | ||
+ | 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 | ||
+ | - 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 ' | ||
+ | Fix it using switch " | ||
+ | |||
+ | |||
+ | # myisamchk -r -v statistiques | ||
+ | - recovering (with sort) MyISAM-table ' | ||
+ | Data records: 43197952 | ||
+ | myisamchk: error: Can't create new tempfile: ' | ||
+ | MyISAM-table ' | ||
+ | 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 ' | ||
+ | Data records: 43197952 | ||
+ | - Fixing index 1 | ||
+ | - Searching for keys, allocating buffer for 102782 keys | ||
+ | Wrong bytesec: | ||
+ | Wrong bytesec: | ||
+ | Wrong bytesec: | ||
+ | Wrong bytesec: | ||
+ | Wrong bytesec: | ||
+ | Wrong bytesec: | ||
+ | Wrong bytesec: | ||
+ | Wrong bytesec: | ||
+ | Wrong bytesec: | ||
+ | Wrong bytesec: | ||
+ | Wrong bytesec: | ||
+ | Wrong bytesec: | ||
+ | Wrong bytesec: | ||
+ | Wrong bytesec: | ||
+ | Wrong bytesec: | ||
+ | Wrong bytesec: | ||
+ | Wrong bytesec: | ||
+ | Wrong bytesec: | ||
+ | - 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 | ||
+ | |||
+ | </ | ||
+ | |||
+ | |||
+ | ===== Strict Mode ===== | ||
+ | |||
+ | Bon à savoir si jamais vous copiez des dates en 0000-00-00 et que vous avez une erreur | ||
+ | |||
+ | SQLSTATE[22007]: | ||
+ | |||
+ | Strict mode affects whether the server permits ' | ||
+ | |||
+ | ===== 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é | ||
+ | |||
+ | < | ||
+ | MariaDB [(none)]> | ||
+ | +------------------+-----------+ | ||
+ | | Log_name | ||
+ | +------------------+-----------+ | ||
+ | | mysql-bin.000001 | 328 | | ||
+ | +------------------+-----------+ | ||
+ | 1 row in set (0.000 sec) | ||
+ | |||
+ | |||
+ | MariaDB [(none)]> | ||
+ | +---------------------------------+--------------------------------+ | ||
+ | | Variable_name | ||
+ | +---------------------------------+--------------------------------+ | ||
+ | | log_bin | ||
+ | | log_bin_basename | ||
+ | | log_bin_compress | ||
+ | | log_bin_compress_min_len | ||
+ | | log_bin_index | ||
+ | | log_bin_trust_function_creators | OFF | | ||
+ | +---------------------------------+--------------------------------+ | ||
+ | 6 rows in set (0.001 sec) | ||
+ | |||
+ | </ | ||
+ | |||
+ | Si non | ||
+ | < | ||
+ | mysql> SHOW BINARY LOGS; | ||
+ | ERROR 1381 (HY000): You are not using binary logging | ||
+ | </ | ||
+ | |||
+ | Pour activer dans le cas de mariaDB | ||
+ | vi / | ||
+ | Décommenter la ligne | ||
+ | log_bin | ||
+ | et restarter mariaDB | ||
+ | systemctl restart mariadb.service | ||
+ | |||
+ | |||
+ | Vos fichiers de log binaires sont maintenant stocké dans / | ||
+ | |||
+ | **mysqlbinlog** permet de lire les fichiers de log binaire et de générer une sortie sql | ||
+ | < | ||
+ | # mysqlbinlog mysql-bin.000001 | ||
+ | mysqlbinlog: | ||
+ | |||
+ | ---------- Si vous avez ce message d' | ||
+ | |||
+ | # mysqlbinlog --no-defaults mysql-bin.000001 | ||
+ | </ | ||
+ | |||
+ | Filtre possible: | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | |||
+ | example de restauration | ||
+ | mysqlbinlog / | ||
+ | |||
+ | mysqlbinlog --no-defaults --start-datetime=" | ||
+ | |||
+ | Par défaut, mysql s' | ||
+ | |||
+ | mysqlbinlog --no-defaults --start-datetime=" | ||
+ | |||
+ | ===== Auto increment ===== | ||
+ | |||
+ | On peut réinitialiser la valeur de l'auto increment d'un id avec la requete | ||
+ | <code sql> | ||
+ | ALTER TABLE `users` AUTO_INCREMENT = 1; | ||
+ | </ | ||
+ | Mysql prendra automatiquement le max(id) + 1 | ||
+ | |||
+ | Pour réinitialiser complètement une colonne d'id | ||
+ | <code sql> | ||
+ | ALTER TABLE `users` DROP `id`; | ||
+ | ALTER TABLE `users` AUTO_INCREMENT = 1; | ||
+ | ALTER TABLE `users` ADD `id` int UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST; | ||
+ | </ | ||
+ | |||
+ | Si votre id est utilisé dans d' | ||
+ | |||
+ | <code sql> | ||
+ | SET @count = 0; | ||
+ | UPDATE `users` SET `users`.`id` = @count:= @count + 1; | ||
+ | ALTER TABLE `users` AUTO_INCREMENT = 1; | ||
+ | </ |