Wondershare Recoverit

Hard Drive Recovery

  • Recovers deleted or lost files from hard drive effectively, safely and completely.
  • Supports data recovery from 500+ data loss scenarios, including computer crash, partition loss, accidental human error, etc.
  • Supports 1000+ file formats recovery with a high success rate and without any quality loss.
Free Download Free Download Free Download Learn More >
hard drive recovery

How to Backup and Restore MySQL Database

Learn the ins and outs of backing up and restoring your MySQL database with this comprehensive guide. Explore various methods, tools, and best practices to safeguard data.
Amy Dennis
Amy Dennis Originally published Jun 27, 24, updated Dec 16, 24
My MySQL database crashed, and I lost all my important data. What do I do now?

If this question seems like something you've heard before, you're not the only one. Losing data is a big worry for people who manage and create databases. It can happen because of mistakes people make, problems with the computer hardware, or attacks from hackers. Losing important data in MySQL database can cause a lot of trouble for any company.

So, knowing how to get your MySQL database back from a saved copy can save the day.

In this complete guide, we'll show you everything about how to backup and restore MySQL database , typing commands into the computer to using easy-to-understand screenshots.

In this article
    1. Way 1: Back up MySQL Database Using mysqldump
    2. Way 2: Back up MySQL Database Using phpMyAdmin
    3. Way 3: Back up MySQL Database with Workbench
    4. Way 4: Back up MySQL Database via File System Snapshot
    5. Way 5: Back up MySQL Database via Binary Log
    1. Method 1: Restore All Databases in MySQL
    2. Method 2: Restore MySQL Database from Files Using phpMyAdmin
    3. Method 3: Recover MySQL Database from Files with Workbench
    4. Method 4: Recover MySQL Database via Binary Log

How to Back Up MySQL Database?

In this section of our article, we have listed a handful of methods to backup MySQL database files.

If you...You should use...
Prefer command-line tools and want a flexible, scriptable solution Way 1: mysqldump
Want a user-friendly interface and are comfortable with web-based tools Way 2: phpMyAdmin
Prefer a desktop application with a graphical interface and additional database management features Way 3: MySQL Workbench
Need to back up very large databases or multiple databases simultaneously with minimal downtime Way 4: File System Snapshot
Want to enable point-in-time recovery and capture all database changes Way 5: Binary Log

Way 1: Back up MySQL Database Using mysqldump

The mysqldump utility is a tried-and-true method for backing up your MySQL database. It's a command-line tool that comes bundled with every MySQL installation, making it a convenient choice for database administrators and developers.

You should use mysqldump if you prefer command-line tools and want a flexible, scriptable solution. This method is highly versatile and can be easily automated, making it ideal for scheduled backups. It comes pre-installed with MySQL and generates SQL files that are human-readable and portable.

To back up your entire MySQL database using mysqldump, follow these steps:

Step 1: Open your terminal or command prompt. Navigate to the MySQL bin directory (e.g., cd /usr/local/mysql/bin).

Step 2: Run the following command, replacing [database_name], [user], and [password] with your actual database name, MySQL user, and password, respectively:

mysqldump -u [user] -p[password] [database_name] > [backup_file_name].sql

This command will create an SQL file containing all the data and structure of your MySQL database.

mysql backup and recovery

Way 2: Back up MySQL Database Using phpMyAdmin

For those who prefer a graphical user interface (GUI), phpMyAdmin is a popular choice. This web-based tool provides an intuitive way to manage and back up your MySQL databases.

It's particularly helpful for users who want a visual representation of their database structure and prefer point-and-click operations. The intuitive interface makes it easy to export databases or specific tables, and users can customize export settings without needing to remember complex command-line syntax.

This method is ideal for small to medium-sized databases and for users who value ease of use over advanced scripting capabilities.

Here's how to back up MySQL database using phpMyAdmin:

Step 1: Open your web browser and navigate to the phpMyAdmin interface. Log in with your MySQL credentials.

Step 2: Select the database you want to back up from the left-hand panel. Click on the "Export" tab at the top of the screen.

export backup mysql php

Step 3: Choose the export options you prefer. For a complete backup, you can leave the default settings. Scroll down and click the "Go" button to initiate the backup process.

phpMyAdmin will generate an SQL file containing your database backup, which you can save to your local machine.

Way 3: Back up MySQL Database with Workbench

MySQL Workbench is a visual database design and management tool developed by Oracle. It offers a user-friendly interface for backing up your MySQL databases.

You should use MySQL Workbench if you prefer a desktop application with a graphical interface and additional database management features. This comprehensive tool, developed by Oracle, provides a rich set of features for database design, development, and administration. It offers a user-friendly interface for backing up databases.

To back up MySQL database with Workbench, follow these steps:

Step 1: Launch MySQL Workbench and connect to your MySQL server. In the left sidebar, find the "Management" section and click on "Data Export". From the list of databases, select the one you want to back up.

backup database workbench

Step 2: Choose where you want to save the backup file. You can either save it in a project folder or as a single self-contained file.

Step 3: Customize the backup options if needed, such as choosing a complete or partial backup, enabling compression, and adjusting other settings.

Step 4: Review the backup settings you've configured. If everything looks good, click the "Start Export" button to begin the backup process.

data export process

Step 5: Workbench will show you the progress of the backup. Once it's done, you'll get a confirmation that the backup is complete.

Way 4: Back up MySQL Database via File System Snapshot

If you have access to your server's file system, you can create a backup by taking a snapshot of the MySQL data directory. This method is particularly useful if you need to back up multiple databases or large databases that would be cumbersome to back up using SQL dump methods.

File System Snapshot is ideal for environments where minimizing downtime is crucial, as it can create a consistent backup quickly without significantly impacting database performance. It's especially valuable in enterprise environments with high-volume databases that require rapid backup and restore capabilities.

Here's how to back up MySQL database via a file system snapshot:

Step 1: First, find out where your MySQL data directory is located. This is the folder where MySQL stores all its database files. The default location varies depending on your operating system:

  • On Linux, it's usually `/var/lib/mysql/`
  • On Windows, it's typically `C:\Program Files\MySQL\MySQL Server X.X\data\` (replace X.X with your MySQL version)

Step 2: Before taking the snapshot, we need to pause MySQL temporarily to ensure that all data is in a consistent state. In the MySQL prompt, run this command:

FLUSH TABLES WITH READ LOCK;

This command flushes all tables and stops MySQL from making any further changes.

file system snapshot

Step 3: Now it's time to create the actual snapshot of the MySQL data directory. The exact command depends on your file system or storage solution. For example, if you're using LVM (Logical Volume Manager) on Linux, you can run:

lvcreate --snapshot --name mysqldata_snapshot --size 1G /dev/mapper/mysqlvg-mysqldata

This command creates a 1GB snapshot of your MySQL data volume.

Or, if you are using NetApp storage systems (using ONTAP CLI), you can run the following command:

snapshot create -server vs_name -volume vol_name -snapshot snap_name

Step 4: So, after creating the snapshot, we need to unlock MySQL so that it can start processing changes again. Run this command:

UNLOCK TABLES;

Step 5: Finally, you can back up the snapshot you just created by copying or moving it to another location, such as a different server, an external hard drive, or cloud storage. The snapshot contains all your MySQL database files at the moment you created it.

Remember, the specific commands for creating the snapshot may vary depending on your file system or storage solution, but the general process remains the same: pause MySQL, create the snapshot, unlock MySQL, and back up the snapshot.

Way 5: Back up MySQL Database via Binary Log

MySQL's binary log is a set of files that record all the changes made to your databases. While primarily used for replication and point-in-time recovery, you can also use the binary log to create a backup of your MySQL database.

Binary logging is perfect for situations where you need to record every database change and possibly redo them to restore data. This technique is commonly combined with complete backups to create a thorough backup plan. It's particularly useful when you need to restore data to a certain point in time or in setups where you need to match data across several servers.

To back up MySQL database via the binary log, follow these steps:

Step 1: Ensure that binary logging is enabled on your MySQL server. Create a full backup of your database using one of the methods described earlier (e.g., mysqldump, phpMyAdmin, or MySQL Workbench).

Step 2: Flush the binary logs to create a new log file for your backup period:

FLUSH LOGS;
binary logs

Step 3: Note the current binary log Image name and position using the following command:

SHOW VARIABLES LIKE 'log_bin%';

Step 4: Copy the binary log files (e.g., mysql-bin.000001, mysql-bin.000002, etc.) from the MySQL data directory to a safe location.\ Store the binary log files alongside your full database backup for future restoration.

How to Restore MySQL Database

Now that you've learned various ways to back up your MySQL database, let's explore how to proceed with the MySQL restore backup process in case of data loss or corruption.

Method 1: Restore All Databases in MySQL

If you need to restore all your MySQL databases from a backup file, you can use the following command for MySQL restore database.

mysql -u [user] -p[password] < [backup_file_name].sql

This command will restore all databases contained in the specified SQL backup file.

restore mysql

Method 2: Restore MySQL Database from Files Using phpMyAdmin

Restoring a MySQL database with phpMyAdmin is just as easy as backing it up. Here's how to recover MySQL database:

Step 1: Open your web browser and navigate to the phpMyAdmin interface. Log in with your MySQL credentials. Click on the "Import" tab at the top of the screen.

import php my admin

Step 2: Click the "Choose File" button and select the SQL backup file you want to restore.

Step 3: Select the options you prefer (e.g., create new database, import method, character set, etc.).

Scroll down and click the "Go" button to initiate the restoration process.

Method 3: Recover MySQL Database from Files with Workbench

MySQL Workbench also provides a straightforward way to restore your MySQL database from a backup file.

Here are the steps to restore a database using MySQL Workbench.

Step 1: Open MySQL Workbench on your computer and connect to your MySQL server. On the left side, you'll see a panel called "Navigator". Look under the "Management" section and click on "Data Import/Restore".

data import workbench

Step 2: A new window called "Data Import" will open. In this window, you need to tell Workbench where your backup file is located. If your backup is a single file, select the option "Import from Self-Contained File". If your backup is a folder, keep the default option "Dump Project Folder" selected. Click on the "..." button to browse for your backup file or folder.

Step 3: In the same "Data Import" window, you'll see some options like ""Import Options"". Here, you can choose things like which database to restore the backup to, or if you want to restore specific tables instead of the entire database. Configure these options according to your needs.

workbench import process

Step 4: Once you've selected your backup file and configured the import options, click on the "Start Import" button to begin restoring your database. Workbench will now show you the progress of the restoration process. Any errors or warnings will be displayed in a log section. Just wait for it to complete.

That's it! Workbench will restore your database from the backup file or folder you selected. The steps are pretty straightforward – connect to the server, point Workbench to your backup, configure any options, and click the "Start Import" button.

Method 4: Recover MySQL Database via Binary Log

If you have a full backup of your MySQL database and the corresponding binary log files, you can restore your data up to a specific point in time using the mysqlbinlog utility.

Here's how to recover MySQL database via the binary log:

Step 1: Stop the MySQL server to prevent further changes to your data. Restore your full database backup using one of the methods described earlier.

Step 2: Identify the binary log file and position you want to use for the point-in-time recovery. Use the mysqlbinlog utility to apply the binary log events to your restored database:

mysqlbinlog [binary_log_file] --start-position=[start_position] --stop-position=[stop_position] | mysql -u [user] -p[password] [database_name]

Step 3: Replace [binary_log_file], [start_position], [stop_position], [user], [password], and [database_name] with the appropriate values for your environment.

This command will apply the binary log events to restore MySQL database.

Bonus 1: How to Restore a Specific Table in MySQL Database

Let's say you have an SQL file (a backup file) that contains the specific table you want to restore to your MySQL database. Follow these steps to recover MySQL database from a specific table.

Step 1: To do mysql restore backup for a specific table, you can use this command:

>mysql -u your_username -p your_database_name < path/to/backup.sql

Replace the following:

  • `your_username` with your actual MySQL username
  • `your_database_name` with the name of the database you want to restore the table to
  • `path/to/backup.sql` with the full file path of your SQL backup file

Step 2: Now, if you only want to restore a single table from that backup file, instead of restoring the entire database, you can use this command:

mysql -u your_username -p your_database_name --tables table_to_restore < path/to/backup.sql

Just replace:

  • `your_username` and `your_database_name` like before
  • `table_to_restore` with the name of the specific table you want to restore
  • `path/to/backup.sql` with the path to your SQL backup file

So, in summary, the `--tables` option allows you to specify which table(s) you want to restore from the backup file, instead of restoring the entire database.

Bonus 2: How to Restore Database Files on PC

In some cases, you might need to recover your MySQL database files directly from your computer's hard drive, especially if you don't have a backup file available. This could happen due to accidental deletion, formatting, virus attacks, or other data loss scenarios.

Wondershare Recoverit is a powerful data recovery tool that can help you recover lost or deleted MySQL database files from your PC or external storage devices.

Here are some key reasons to consider Wondershare Recoverit for MySQL database recovery:

  • User-Friendly Interface: Recoverit boasts an intuitive, easy-to-use interface that guides users through the recovery process step-by-step, making it accessible even for those with limited technical expertise.
  • High Success Rate: With a reported 98% success rate, Recoverit offers a robust solution for recovering lost or deleted files, including MySQL database files.
  • 20+ Years of Experience: Wondershare has been in the data recovery business for over two decades, continuously refining their software to handle various data loss scenarios.
  • Wide File Type Support: Recoverit can recover over 1000+ file types, including all common MySQL database file extensions like .frm, .ibd, .MYD, and .MYI.
  • Multiple Recovery Scenarios: Whether your files were lost due to accidental deletion, hard drive formatting, system crash, or even virus attacks, Recoverit is designed to handle diverse data loss situations.
Free Download
Free Download

Here's how to use Wondershare Recoverit to restore your MySQL database files:

Step 1: Install Wondershare Recoverit on your computer. Launch the software and select the location where your MySQL data directory was stored (e.g., the drive or partition).

wondershare recoverit interface

Step 2: Click the "Start" button to initiate the scan process. Recoverit offers two different types of scan: Quick Scan and Deep Scan. Once the scan is complete, Recoverit will display a list of recoverable files.

scan results recoverit

Step 3: Locate your MySQL database files (e.g., .frm, .ibd, or other file extensions) and select them. You can even filter the search results based on file size, and file type criteria.

filter results

Step 4: Preview the files. Click the "Recover" button to save the recovered files to a safe location on your computer.

preview and recover files

After recovering your MySQL database files, you can attempt to restore them using the appropriate methods (e.g., copying them back to the MySQL data directory or importing them into a new database).

Conclusion

This wraps our guide on how to backup and restore MySQL database. Backing up and restoring your MySQL database is a crucial aspect of data management and disaster recovery. Whether you're a seasoned database administrator or a developer working on a small project, having a reliable backup strategy in place can save you from countless headaches and potential data loss.

Throughout this guide, we've explored various methods for backing up and restoring your MySQL database, ranging from command-line tools to graphical user interfaces. We've covered mysqldump, phpMyAdmin, MySQL Workbench, file system snapshots, and binary logs for MySQL recovery. And even data recovery tools like Wondershare Recoverit.

With the knowledge and techniques you've gained from this guide, you'll be better equipped to handle any data loss scenario and keep your MySQL database safe and sound.

Free Download
Free Download

FAQ

  • How to restore MySQL database from .frm and .ibd files?
    To restore a MySQL database from .frm (table definition) and .ibd (table data) files, you can follow these steps:
    a. Stop the MySQL server.
    b. Copy the .frm and .ibd files to the appropriate database directory in the MySQL data directory.
    c. Start the MySQL server.
    d. Connect to the MySQL server and import the tables using the following command: RESTORE TABLE [table_name] FROM DISK = '[path/to/table_name].ibd';
    Replace [table_name] with the actual table name and [path/to/table_name].ibd with the full path to the .ibd file.
  • How to restore MySQL database without backup file?
    If you don't have a backup file available, restoring your MySQL database can be challenging. However, you can try the following methods:
    a. Use data recovery software like Wondershare Recoverit to recover deleted or lost MySQL database files from your hard drive or storage device.
    b. If you have enabled binary logging, you might be able to recover your database up to a specific point in time using the mysqlbinlog utility and your binary log files.
    c. If you have a file system snapshot or backup of the MySQL data directory, you can attempt to restore the database by copying the files back to the appropriate location.
  • Where are MySQL database files stored?
    The location of MySQL database files depends on your operating system and MySQL installation. By default, the MySQL data directory is:
    On Windows: C:\ProgramData\MySQL\MySQL Server 8.0\Data (or a similar path, depending on your MySQL version)
    On Linux: /var/lib/mysql
    On macOS: /usr/local/mysql/data
    Within the MySQL data directory, you'll find subdirectories for each database, containing the .frm (table definition) and .ibd (table data) files, among others. You can also check the datadir variable in your MySQL configuration file (e.g., my.cnf or my.ini) to determine the exact location of your MySQL data directory.
Amy Dennis
Amy Dennis Dec 16, 24
Share article: