
mysqldump is a built-in backup and recovery utility for MySQL. It exports a database’s schema and data to a text-based file that can be used to restore the database later. The exported file typically contains SQL statements such as CREATE, INSERT, and DROP, but it can also support non-SQL formats for further processing.
In this article, you will use mysqldump to export a single database or multiple databases to an .sql file or a non-SQL format such as .csv or .txt, and then restore the database using the MySQL client.
Before you begin, you need to:
mysqldump is part of the MySQL client package and allows you to export MySQL databases using the following syntax:
-h [host]: The MySQL database host. Optional when connecting to a local database server.-u [user]: A MySQL user with SELECT, LOCK, VIEW, and TRIGGER privileges on the target database.-P [port]: The MySQL server port. Optional for local servers.-p: Prompts for the MySQL user password.[database]: The database to export.> [export-file]: Writes the exported database to the specified file.You can export MySQL data to various formats using mysqldump, including:
.sql: Plain SQL file with CREATE, INSERT, and SELECT statements..sql.gz: Gzip-compressed SQL dump..sql.zip: ZIP-compressed SQL dump..sql.tar: Tar-archived SQL dump..bak: Generic backup file..csv: Comma-separated values (CSV) file containing table data..txt: Plain text file..xml: XML-formatted dump..dump: Full schema and data dump, similar to .sql.Before exporting a database with mysqldump, log in to the MySQL database server, verify that the database exists, and prepare it for export if it is in use with existing applications. The following steps demonstrate how to access the MySQL database server and prepare the target database for export.
Log in to the MySQL database server as a user with sufficient privileges, such as root, with SELECT, LOCK, VIEW, and TRIGGER privileges on the target database.
When prompted, enter the mysql root user password.
List the databases the user can access.
Identify the target database, for example exampledb, in the output:
Switch to the target database.
List all tables in the database.
Verify that the user has SELECT privileges by listing the records in any table.
mysqldumpYou can export one or more databases with mysqldump if your MySQL user has the required privileges to the target database. mysqldump connects to the database, dumps a copy of the database, and redirects the output to your target file. In the following steps, export a MySQL database to an SQL file with mysqldump.
Create a directory named database-backups to store your backup files.
Navigate to the directory.
Export a single database like exampledb to a .sql file using mysqldump.
To prevent changes during export (useful if the database is active), add the --lock-all-tables option:
Run the above command again for each database, specifying a unique filename.
Export all databases to a single .sql file using mysqldump.
The above command exports all databases the user can access on the MySQL database server.
List the files in your working directory and verify that the .sql file is available.
Output:
The mysqldump tool only produces SQL-formatted output, even if the file extension is .csv. To generate an actual CSV file, use a SQL query with the INTO OUTFILE clause from within the mysql shell.
Log in to the MySQL server.
Run a SELECT statement with INTO OUTFILE to export the table.
This creates a CSV file at /tmp/example_table.csv on the database server. You may need to adjust file permissions or use sudo to copy it.
Exit the MySQL console.
Copy the file to your current directory.
Create the script file.
Add the following content to the file:
You can modify the script to customize delimiters, escape characters, or output locations. Make sure the mysql-files directory has proper write permissions for MySQL and copy access for your user.
Make the script executable.
Run the script.
After exporting a MySQL database using mysqldump, verify that the backup file is intact before restoring. This section walks you through testing the dump using a temporary database, checking table integrity, and then restoring into the intended target database.
List the backup file and confirm it is not empty.
Log in to the MySQL database server.
Create a test database such as restore_db.
Exit the MySQL console.
Restore the backup to the restore_db database.
Log in to the MySQL database server with the restore_db database.
List the tables in the database and verify that they match the original database.
Check the integrity of the table data for corruption or restoration errors.
Drop the restore_db test database after verifying a successful restoration.
Exit the MySQL console.
Back up the current production database.
Restore the SQL file into the target database.
If the database does not exist, create it first.
Log in to verify the restored database.
Check the integrity of the restored tables.
Exit the MySQL console.
In this article, you used mysqldump to export a MySQL database, verified the resulting SQL files, and tested data restoration to ensure structural and data integrity. These steps form the foundation of a reliable backup workflow in production environments. For extended options such as partial exports, triggers, or custom character sets, consult the mysqldump reference manual.
0 Comments
Be the first to comment and share your perspective with the community.