MacBook-Bildschirm mit SQL- und PHP-Code einer Datenbankabfrage, Symbolbild für MySQL auf der Konsole

Export and Import a MySQL Database from the Command Line

You can’t always export and import MySQL databases (or MariaDB databases, which started out as a fork of MySQL) through a pretty point-and-click interface like phpMyAdmin or Adminer. For small databases under 20 MB that may still work, but with something like 3 GB it gets difficult. Tools like MySQLDumper (in German) are getting really long in the tooth (most of them aren’t actively developed anymore), even though they still work. But there’s a simpler way, as long as you have access to the console, either via SSH or directly. 

Export a specific MySQL database

mysqldump -u benutzername -p datenbankname > dump.sql

Export only the structure of a MySQL database

mysqldump -u benutzername -p --no-data datenbankname > dump.sql

Export only the data from a MySQL database

mysqldump -u benutzername -p --no-create-info datenbankname > dump.sql

Export multiple MySQL databases

mysqldump -u benutzername -p --databases datenbankname1 [datenbankname2 ...] > dump.sql

Export all MySQL databases

mysqldump -u benutzername -p --all-databases > dump.sql

Import MySQL databases

mysql -u benutzername -p datenbankname < dump.sql

In all commands, replace “benutzername” with your database user name and “datenbankname” with the name of your database.

Update 2026: On current MariaDB versions, the tools are called mariadb-dump and mariadb. The old name mysqldump still works on many systems, but according to the MariaDB documentation it has been deprecated since MariaDB 11.0 and is no longer included in the official mariadb Docker image. The options shown above stay the same.

Compress SQL dumps

Since SQL dumps can get very large and are just plain text, you can easily shrink them to a fraction of their size with a compression tool like gzip, using “gzip dump.sql“. A 2 GB SQL dump of mine came down to just under 175 MB after compressing it with gzip. With gunzip you can unpack the archive again: “gunzip dump.sql.gz”.

Once the SQL dump is successfully exported and compressed, you can get it to the target server or database via SCP, SFTP or many other ways, including putting it in a public directory. Just don’t forget the dump in that public directory, otherwise things get embarrassing (in German).

This post is also available in: Deutsch

Published by

Christopher Piontek

Technology fascinates me, and I've been writing about it on Bitpage.de since 2012. By day I'm a developer, and on the side I test software and tinker with old hardware. Here you'll find how-tos, hands-on reports and my take on things, sometimes at full nerd level, sometimes as a quick practical tip.

Leave a Reply

Your email address will not be published. Required fields are marked *