Databases are essential tools to collect and store mountains of data. All complex applications use them to store information. Show
Managing and backing up servers and databases can all be done in MySQL. In this guide, we are going to walk you through what mysqldump is, how to use it, identify some common errors, and provide some clear examples along the way so that you will be able to use mysqldump effectively.
Table of ContentsWhat is mysqldump?MySQL is a database system that has been around for years and continues to be one of the most popular choices for websites. It's open-source and agile. Developers can use these databases to store anything a website may need. The information stored in online databases can range from consumer info and simple text to picture galleries to network information. Mysqldump is part of the relational database package for MySQL. It is used to back up all the data in a database into a single text file. These files or "dumps" can be generated for individual databases or a collection of them. The text in the file is displayed as a set of SQL statements that can later be reconstructed into its original state. The purpose of this tool is to export a database to a backup file or to move the database to another web host. Other delimited text formats like XML and CSV can also be generated with mysqldump. These simple queries make the backup process easier. The importance of backing up dataCompanies that hope to run smoothly need pristine copies of their data at different points in time. Without a backup strategy, there is nothing to protect them in the case of a disaster. The ease in which the data can be lost forever is too much to cope with as data can easily become corrupted or get lost over time. Malicious intent and natural disasters are not a requirement for worst-case scenarios to transpire. Having backups at periodic intervals gives the company the ability to rewind the clock by reloading the previous database. If something breaks or fails, this acts as a lifeline for the system. The company also has data versioning available. Different versions of the database and product are available to go back to. Critical changes that later prove to break the system can be undone, then you can restore the old versions without the problem. By backing up everything, migrations to new servers or development environments transpire without the fear that data will be lost. Don't want to maintain you backup scripts? Try SimpleBackups Now → How to use mysqldumpBy using mysqldump, a developer can get a hold of the .sql file that serves as a back-up for the entire database. To use the tool, the developer needs access to the server running the instance of MySQL. The required privileges have to be given to export anything. The user credentials for the database will also be needed, including the username and password. The three ways that mysqldump is used to export data includes:
Export MySQL tables via mysqldumpMake sure you are on a machine that has MySQL installed. You will also need a valid database user with -at minimum- full read access privileges. This should do for basic options, but more advanced commands may require additional privileges. With that in order, launch a terminal where you will send the command to back up the tables. Match your inputs to conform to the following mysqldump command structure:
For the live command, replace
You will then provide the password for the database user
because it is not passed along with the Exporting a MySQL databaseThe steps for exporting a database are very close to those for exporting a table. There is just a small change in the format of the command. You will need the same server access and credentials. Match your inputs to conform to the following mysqldump command structure:
The database you will export comes after the Exporting a MySQL serverThe command is very similar for entire servers as well. Match your inputs to conform to the following mysqldump command structure:
The command itself is pretty basic, with Developers using PowerShell on Windows will need to include -result-file as an option. This will specify the file name and make sure that the output is in ASCII format so that it will load correctly later. Other common options include adding Importing a mysqldumpImporting a .sql file is straight forward. The only kink is to make sure the target server has a blank database before importing anything (check our mini guide on how to import SQL files). Match your inputs to conform to the following mysqldump command structure:
The mysqlimport command will also work on databases you want to restore that already exists on the target machine:
You also have the option to import all databases by running a command that looks like this:
What does the --quick flag do?Mysqldump can operate in one of two ways.
The second method is important when dealing with large tables. By using the Dump without locking tables and the --skip-lock-tables flagUsing Generally it is
recommended to use So should I use
What does the --single-transaction flag do?In short, the
Note: MyISAM tables will not benefit from this flag and should be locked if you want to preserve their dump integrity. How to dump large tables?To dump large tables, you could combine the following two flags,
Note: This is ideal for InnoDB tables. Since it will use less RAM and also produce consistent dumps without locking tables. Need to manage long execution backups, timeout, retries, streaming...? Try SimpleBackups Now → How to ignore tables using mysqldump?Using the Here is an example that will just allow you to ignore one table:
As you seen, the format is as following: To ignore all tables in a database (or a whole database when dumping all your databases), you have to repeat the argument to include all the tables you want to ignore.
How to dump binary BLOB data?Sometimes you may face issue with the resulting dump if it has binary data. For this reason, you could use the following mysqldump flag Under the hood, it dumps the binary strings it finds (BINARY, VARBINARY, BLOB) in a hexadecimal format which represents these data structure in a reliable way. Here is a mysqldump example to dump binary data correctly:
Does the "where" clause work with mysqldump?Yes, this clause works with the command line. This makes it easy to set conditions on the data you need to dump from the database. If there is a large enterprise that has been in business for decades that wants to pull the information after April 27, 2017, then this clause allows that to happen. The where clause passes a string for the condition and grabs the specific records requested.
Troubleshooting common errorsAlong the way you may face some MySQL common errors that are -to some degree- easy to mitigate. We will share below some of these errors and how to solve them. Error 2013: lost connection to mysql server during query when dumping tableTo fix this issue, you need to go into the MySQL configuration file and increase some values. When those are added, save and close the file, then restart MySQL for the changes to take effect. The values you need to adjust are:
The adjustments to the file will be under the
Error 2020: got packet bigger than 'max_allowed_packet' bytes when dumping tableIf the database you need to back up is large, and the file size ends up bigger thant the maximum allowed packet size, this error pops up. This error can be fixed by going into the MySQL configuration file and increasing max_allowed_packet value in the The changes will look like this:
Table does not exist (1146), couldn't execute 'show create table x'There may be times when you delete a table during backing up. If this is the case, you can restrict
certain tables from the mysqldump command with the
By listing the option multiple times, you can ignore multiple tables:
Selecting the database returns 'unknown database'This error happens most often when you use the The correct input would look like this:
Error 1044 when selecting the databaseIf the user trying to do the dump doesn't have the privileges necessary to access the database, this error occurs. Logging into MySQL and assigning those privileges to the user will fix the issue. Enter command: Then enter the correct password, and proceed to grant privileges to the selected user.
After that, flush the privileges and exit from MySQL by entering the command: Access denied for user when trying to connect to mysqldumpThis error has several possible causes. Here's three of the most common causes of the issue.
If you are using the wrong command, then this error will appear. The command may be mostly correct but it's missing a critical ingredient in the mysqldump format. The basic command will look like this:
If you fail to specify a username or password then it will spit back the following message:
This error comes up if the backup is trying to be done on a remote server. The configurations for MySQL are set to disallow external connections. Here, the localhost is the only one allowed to make a backup. This is a security measure, so it's a good measure to have but if you need to change this, go to configurations and change MySQL to allow connections from a remote host.
If you try to use the wrong username and password combination while connecting to the database, this error happens. MySQL can't verify that the request is authentic and returns the error. You'll have to make the request again with proper credentials, make sure there aren't any typos in your original command as that is the easiest mistake to make. ConclusionMysqldump is a useful tool to help back up databases with minimal commands. One command allows the entire database to be spit out into a single text file. The tool is versatile enough to back up the parts of the database that is needed and comes with a variety of options to change the data you need to save. Automated MySQL Backups as a serviceSimpleBackups is a database and website backup automation tool that offloads all the backups tasks. → Set up your first MySQL backup for free How do I run a MySQL dump file?From the normal command line, you can import the dump file with the following command: mysql -u username -p new_database < data-dump. sql.
How do I run a dump file in MySQL workbench?Load a MySQL dump from MySQL Workbench
Connect to your MySQL database. Click Server on the main tool bar. Select Data Import. You should see a link to the default dump folder, typically your Documents folder in a subfolder titled dumps .
How do I dump a DB file?To dump a database into a file, you use the . dump command. The . dump command converts the entire structure and data of an SQLite database into a single text file.
How does SQL dump work?A database dump contains a record of the table structure and/or the data from a database and is usually in the form of a list of SQL statements ("SQL dump"). A database dump is most often used for backing up a database so that its contents can be restored in the event of data loss.
|