mysqldump, ursprünglich von Igor Romanenko geschrieben, ist seit sehr langer Zeit das klassische logische Backup-Tool für MySQL. Es erzeugt eine Reihe von SQL-Anweisungen, die Ihre Daten neu erstellen; standardmäßig überspringt es die information_schema-Tabellen und dumpt performance_schema nie. Seine strukturelle Schwäche ist, dass es sowohl für Backup als auch Restore einen einzigen Thread nutzt – selbst wenn Ihr Server 64 brachliegende Kerne hat.
MySQL hat das im Shell-Client mit einer neuen Generation von Dump-Utilities adressiert. Dieser Beitrag behandelt, was sie sind und wie man sie nutzt.
Die MySQL Shell im Überblick
Die MySQL Shell ist ein fortgeschrittener Client und Code-Editor für MySQL Server. Version 8.0.21 führte Utilities zum Erstellen eines logischen Dumps und zum logischen Restore einer ganzen Instanz samt Benutzerkonten ein. Version 8.0.22 erweiterte dies auf ausgewählte Tabellen und Views.
Die Utilities
util.dumpInstance()– eine ganze Instanz samt Benutzern dumpenutil.dumpSchemas()– einen gewählten Satz von Schemata dumpenutil.loadDump()– einen Dump in eine Zieldatenbank ladenutil.dumpTables()– spezifische Tabellen und Views dumpen
util.dumpInstance()
Dies dumpt jede Datenbank im MySQL-Datenverzeichnis und schließt information_schema, mysql, ndbinfo, performance_schema und sys aus.
Syntax
util.dumpInstance(outputUrl[, options])
Die Ausgabe geht auf das lokale Dateisystem; outputUrl ist der Pfad zum Zielverzeichnis, absolut oder relativ zum aktuellen Arbeitsverzeichnis.
Ein Dry-Run-Modus erlaubt es, Schemata zu inspizieren und Kompatibilitätsprobleme aufzudecken, bevor man sich festlegt, sodass Sie die richtigen Kompatibilitätsoptionen anwenden und einen sauberen Dump erzeugen können.
Optionen
ocimds : [True | False]
Mit aktiviertem ocimds prüft das Utility das Data Dictionary, das Index Dictionary und die Verschlüsselungsoptionen in CREATE TABLE-Anweisungen und kommentiert sie im DDL aus, sodass alle Tabellen im MySQL-Datenverzeichnis liegen und die Standard-Schema-Verschlüsselung nutzen. Es sucht außerdem in CREATE TABLE-Anweisungen nach anderen Storage Engines als InnoDB, nach der Vergabe ungeeigneter Berechtigungen an Benutzer oder Rollen und nach anderen Kompatibilitätsproblemen. Jede nicht konforme Anweisung löst eine Ausnahme aus und stoppt den Dump.
Der praktische Ansatz ist, zuerst mit dryRun zu laufen, um jedes Problem aufzuzählen, und dann mit der Option compatibility erneut zu laufen, um sie automatisch in der Dump-Ausgabe zu beheben.
Hinweis: Diese Option gilt für die Instanz- und Schema-Dump-Utilities.
Beispiel 1 — das Problem aufdecken
MySQL localhost:3306 ssl cart JS > util.dumpInstance("/home/vagrant/production_backup", {ocimds: true,compatibility: ["strip_restricted_grants"]}) Acquiring global read lock Global read lock acquired All transactions have been started Locking instance for backup NOTE: Backup lock is not supported in MySQL 5.7 and DDL changes will not be blocked. The dump may fail with an error or not be completely consistent if schema changes are made while dumping. Global read lock has been released Checking for compatibility with MySQL Database Service 8.0.22 NOTE: MySQL Server 5.7 detected, please consider upgrading to 8.0 first. You can check for potential upgrade issues using util.checkForServerUpgrade(). NOTE: User 'backupuser'@'localhost' had restricted privileges (RELOAD, SUPER, CREATE TABLESPACE) removed NOTE: User 'root'@'127.0.0.1' had restricted privileges (RELOAD, SHUTDOWN, FILE, SUPER, CREATE TABLESPACE) removed NOTE: User 'root'@'::1' had restricted privileges (RELOAD, SHUTDOWN, FILE, SUPER, CREATE TABLESPACE) removed NOTE: User 'root'@'localhost' had restricted privileges (RELOAD, SHUTDOWN, FILE, SUPER, CREATE TABLESPACE, PROXY) removed ERROR: Table 'cart'.'sales' uses unsupported storage engine MyISAM (fix this with 'force_innodb' compatibility option) Compatibility issues with MySQL Database Service 8.0.22 were found. Please use the 'compatibility' option to apply compatibility adaptations to the dumped DDL. Util.dumpInstance: Compatibility issues were found (RuntimeError)
Eine MyISAM-Tabelle in der Datenbank cart stoppt den Lauf, und der Dry Run sagt es unmissverständlich.
Beispiel 2 — es automatisch beheben
Fügen Sie force_innodb zur compatibility-Liste hinzu:
MySQL localhost:3306 ssl cart JS > util.dumpInstance("/home/vagrant/production_backup", {dryRun: true ,ocimds: true,compatibility: ["strip_restricted_grants","force_innodb"]}) Acquiring global read lock Global read lock acquired All transactions have been started Locking instance for backup NOTE: Backup lock is not supported in MySQL 5.7 and DDL changes will not be blocked. The dump may fail with an error or not be completely consistent if schema changes are made while dumping. Global read lock has been released Checking for compatibility with MySQL Database Service 8.0.22 NOTE: MySQL Server 5.7 detected, please consider upgrading to 8.0 first. You can check for potential upgrade issues using util.checkForServerUpgrade(). NOTE: User 'backupuser'@'localhost' had restricted privileges (RELOAD, SUPER, CREATE TABLESPACE) removed NOTE: User 'root'@'127.0.0.1' had restricted privileges (RELOAD, SHUTDOWN, FILE, SUPER, CREATE TABLESPACE) removed NOTE: User 'root'@'::1' had restricted privileges (RELOAD, SHUTDOWN, FILE, SUPER, CREATE TABLESPACE) removed NOTE: User 'root'@'localhost' had restricted privileges (RELOAD, SHUTDOWN, FILE, SUPER, CREATE TABLESPACE, PROXY) removed NOTE: Table 'cart'.'sales' had unsupported engine MyISAM changed to InnoDB Compatibility issues with MySQL Database Service 8.0.22 were found and repaired. Please review the changes made before loading them. Writing global DDL files Writing users DDL Writing DDL for schema `cart` Writing DDL for table `cart`.`salaries` Writing DDL for table `cart`.`sales` Writing DDL for table `cart`.`t1` Preparing data dump for table `cart`.`salaries` Data dump for table `cart`.`salaries` will be chunked using column `id` Preparing data dump for table `cart`.`sales` Data dump for table `cart`.`sales` will be chunked using column `id` Preparing data dump for table `cart`.`t1` NOTE: Could not select a column to be used as an index for table `cart`.`t1`. Chunking has been disabled for this table, data will be dumped to a single file.
Sauberer Dry Run, keine Ausnahmen. Zeit für die echten Dumps.
Beispiel 3 — der tatsächliche Dump
MySQL localhost:3306 ssl cart JS > util.dumpInstance("/home/vagrant/production_backup", {compatibility: ["strip_restricted_grants","force_innodb"],threads : 12}) Acquiring global read lock Global read lock acquired All transactions have been started Locking instance for backup Global read lock has been released Writing global DDL files Writing users DDL Writing DDL for schema `cart` Writing DDL for view `cart`.`price` Writing DDL for table `cart`.`dummy` Writing DDL for table `cart`.`salaries` Writing DDL for schema `sbtest` Writing DDL for table `sbtest`.`sbtest1` Writing DDL for table `sbtest`.`sbtest10` . . . 1 thds dumping - 99% (624.55K rows / ~625.40K rows), 896.15K rows/s, 10.13 MB/s uncompressed, 3.73 MB/s compressed Duration: 00:00:00s Schemas dumped: 2 Tables dumped: 18 Uncompressed data size: 7.14 MB Compressed data size: 2.79 MB Compression ratio: 2.6 Rows written: 624550 Bytes written: 2.79 MB Average uncompressed throughput: 7.14 MB/s Average compressed throughput: 2.79 MB/s
Da die compatibility-Option angewendet wurde, werden MyISAM-Tabellen beim Hinausdoppen in InnoDB umgewandelt. Das Zielverzeichnis muss vor dem Exportbeginn leer sein; existiert es unter seinem übergeordneten Verzeichnis nicht, legt das Utility es an.
Wie das DDL aussieht
[example@sqldat.com production_backup]$ cat example@sqldat.com -- MySQLShell dump 1.0.1 Distrib Ver 8.0.22 for Linux on x86_64 - for MySQL 8.0.22 (MySQL Community Server (GPL)), for Linux (x86_64) -- -- Host: localhost Database: cart Table: sales -- ------------------------------------------------------ -- Server version 5.7.32 -- -- Table structure for table `sales` -- /*!40101 SET @saved_cs_client = @@character_set_client */; /*!50503 SET character_set_client = utf8mb4 */; CREATE TABLE IF NOT EXISTS `sales` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(30) DEFAULT NULL, `address` varchar(30) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1; /*!40101 SET character_set_client = @saved_cs_client */;
Wo mysqldump eine Datei schreibt, schreibt die Shell viele:
[example@sqldat.com production_backup]$ ls -lrth total 52K -rw-r-----. 1 vagrant vagrant 707 Nov 6 02:36 @.json -rw-r-----. 1 vagrant vagrant 287 Nov 6 02:36 cart.json -rw-r-----. 1 vagrant vagrant 240 Nov 6 02:36 @.sql -rw-r-----. 1 vagrant vagrant 240 Nov 6 02:36 @.post.sql -rw-r-----. 1 vagrant vagrant 2.6K Nov 6 02:36 @.users.sql -rw-r-----. 1 vagrant vagrant 733 Nov 6 02:36 example@sqldat.com -rw-r-----. 1 vagrant vagrant 486 Nov 6 02:36 cart.sql -rw-r-----. 1 vagrant vagrant 575 Nov 6 02:36 example@sqldat.com -rw-r-----. 1 vagrant vagrant 8 Nov 6 02:36 example@sqldat.com@0.tsv.zst.idx -rw-r-----. 1 vagrant vagrant 8 Nov 6 02:36 example@sqldat.com@@1.tsv.zst.idx -rw-r-----. 1 vagrant vagrant 47 Nov 6 02:36 example@sqldat.com@0.tsv.zst -rw-r-----. 1 vagrant vagrant 24 Nov 6 02:36 example@sqldat.com@@1.tsv.zst -rw-r-----. 1 vagrant vagrant 252 Nov 6 02:36 @.done.json
@.jsonhält Serverdetails plus die Liste der Benutzer, Datenbanknamen und ihrer Zeichensätze.cart.jsonhält View-, Stored-Procedure- und Funktionsnamen sowie die Tabellenliste.@.sqlund@.post.sqlprotokollieren Details zur MySQL-Serverversion.@.users.sqlenthält die Datenbankbenutzer.- Die Datei
example@sqldat.comenthält die Tabellenstruktur. cart.sqlenthält die Datenbank-Anweisung.- Eine weitere Datei
example@sqldat.comhält Spaltennamen und Zeichensätze. - Die Dateien
*.tsv.zst.idxsind binär und speichern Tabellen-Indexstatistiken. - Die Dateien
*.tsv.zstsind binär und speichern die Daten selbst. @.done.jsonprotokolliert die Backup-Endzeit und die Dateigrößen in KB.
util.dumpSchemas()
Dumpt nur die von Ihnen benannten Schemata.
Syntax
util.dumpSchemas(schemas, outputUrl[, options])
Beispiel
MySQL localhost:3306 ssl cart JS > util.dumpSchemas(["cart"], "/home/vagrant/production_backup",{compatibility: ["strip_restricted_grants","force_innodb"],threads :12}) Acquiring global read lock Global read lock acquired All transactions have been started Locking instance for backup NOTE: Backup lock is not supported in MySQL 5.7 and DDL changes will not be blocked. The dump may fail with an error or not be completely consistent if schema changes are made while dumping. Global read lock has been released Writing global DDL files Writing DDL for table `cart`.`price_tag` Writing DDL for schema `cart` Writing DDL for table `cart`.`salaries` Writing DDL for table `cart`.`sales` NOTE: Table 'cart'.'sales' had unsupported engine MyISAM changed to InnoDB Preparing data dump for table `cart`.`price_tag` Data dump for table `cart`.`price_tag` will be chunked using column `id` Data dump for table `cart`.`price_tag` will be written to 1 file Preparing data dump for table `cart`.`salaries` Data dump for table `cart`.`salaries` will be chunked using column `id` Data dump for table `cart`.`salaries` will be written to 2 files Preparing data dump for table `cart`.`sales` Data dump for table `cart`.`sales` will be chunked using column `id` Running data dump using 12 threads. NOTE: Progress information uses estimated values and may not be accurate. Data dump for table `cart`.`sales` will be written to 1 file 1 thds dumping - 150% (3 rows / ~2 rows), 0.00 rows/s, 0.00 B/s uncompressed, 0.00 B/s compressed Duration: 00:00:00s Schemas dumped: 1 Tables dumped: 3 Uncompressed data size: 53 bytes Compressed data size: 0 bytes Compression ratio: 53.0 Rows written: 3 Bytes written: 0 bytes Average uncompressed throughput: 53.00 B/s Average compressed throughput: 0.00 B/s
util.dumpTables
Für ausgewählte Tabellen – nützlich, wenn mysqldump bei einigen großen zu lange dauern würde.
Syntax
util.dumpTables(schema, tables, outputUrl[, options])
Beispiel
util.dumpTables("sbtest", [ "sbtest14", "sbtest16" ], "/home/vagrant/specific_table",{dryRun: true}) MySQL localhost:33060+ ssl sbtest JS > util.dumpTables("sbtest", [ "sbtest14", "sbtest16" ], "/home/vagrant/specific_table",{threads: 12}) Acquiring global read lock Global read lock acquired All transactions have been started Locking instance for backup Global read lock has been released Writing global DDL files Writing DDL for table `sbtest`.`sbtest16` Writing DDL for table `sbtest`.`sbtest14` Preparing data dump for table `sbtest`.`sbtest16` Data dump for table `sbtest`.`sbtest16` will be chunked using column `id` Preparing data dump for table `sbtest`.`sbtest14` Data dump for table `sbtest`.`sbtest14` will be chunked using column `id` Running data dump using 12 threads. NOTE: Progress information uses estimated values and may not be accurate. Data dump for table `sbtest`.`sbtest16` will be written to 1 file Data dump for table `sbtest`.`sbtest14` will be written to 1 file 1 thds dumping - 99% (78.07K rows / ~78.08K rows), 0.00 rows/s, 0.00 B/s uncompressed, 0.00 B/s compressed Duration: 00:00:00s Schemas dumped: 1 Tables dumped: 2 Uncompressed data size: 892.39 KB Compressed data size: 348.91 KB Compression ratio: 2.6 Rows written: 78068 Bytes written: 348.91 KB Average uncompressed throughput: 892.39 KB/s Average compressed throughput: 348.91 KB/s
Das Dump-Lade-Utility
Das Laden unterstützt Daten-Streaming zu entferntem Speicher, paralleles Laden von Tabellen oder Tabellen-Chunks, Fortschrittsverfolgung, Resume und Reset sowie sogar gleichzeitiges Laden, während der Dump noch geschrieben wird.
Hinweis: Das Lade-Utility nutzt LOAD DATA LOCAL INFILE, daher muss local_infile auf dem Ziel vor dem Import global aktiviert sein.
Ist die Systemvariable sql_require_primary_key auf ON gesetzt, gibt der Loader für jede Tabelle im Dump, der ein Primärschlüssel fehlt, einen Fehler zurück.
Syntax
util.loadDump(url[, options])
Beispiel
MySQL localhost:3306 ssl sbtest JS > util.loadDump("/home/vagrant/specific_table", {progressFile :"/home/vagrant/specific_table/log.json",threads :12}) Loading DDL and Data from '/home/vagrant/specific_table' using 12 threads. Opening dump... Target is MySQL 8.0.22. Dump was produced from MySQL 8.0.22 Checking for pre-existing objects... Executing common preamble SQL [Worker006] Executing DDL script for `sbtest`.`sbtest1` [Worker004] Executing DDL script for `sbtest`.`sbtest12` 2 thds loading \ 100% (892.39 KB / 892.39 KB), 0.00 B/s, 0 / 2 tables done[Worker001] example@sqldat.com@@0.tsv.zst: Records: 39034 Deleted: 0 Skipped: 0 Warnings: 0 [Worker005] example@sqldat.com@@0.tsv.zst: Records: 39034 Deleted: 0 Skipped: 0 Warnings: 0 Executing common postamble SQL 2 chunks (78.07K rows, 892.39 KB) for 2 tables in 1 schemas were loaded in 1 sec (avg throughput 892.39 KB/s) 0 warnings were reported during the load.
Standardmäßig werden FULLTEXT-Indizes erst gebaut, nachdem eine Tabelle fertig geladen ist, was den Import erheblich beschleunigt. Sie können auch die Erstellung aller Indizes bis nach dem Datenimport aufschieben. Der Loader importiert über mehrere Threads, um die Parallelität zu maximieren, dekomprimiert transparent Dumps, die von den eigenen Dump-Utilities der Shell erzeugt wurden, lässt Sie einzelne Tabellen und Schemata ein- oder ausschließen und kann das Binary Logging auf dem Ziel mit SET sql_log_bin=0 unterdrücken.
Fazit
Dies gehören zu den leistungsfähigsten Utilities, die in MySQL 8.0 hinzugefügt wurden. Sie können von MySQL 5.6 dumpen und in 5.7 oder 8.0 laden – allerdings werden Benutzerkonten nicht gedumpt, wenn die Quelle 5.6 ist. Im nächsten Beitrag unterlegen wir mit harten Zahlen, wie die Shell-Utilities im Vergleich zu mysqldump bei Backup und Restore abschneiden.