DEV Community

loading...
Cover image for The Complete mysqldump Guide (with examples)
SimpleBackups

The Complete mysqldump Guide (with examples)

zoxta profile image zOxta Originally published at simplebackups.io Updated on ・8 min read

Databases are essential tools to collect and store mountains of data. All complex applications use them to store information.
There are many database solutions out there, but MySQL is one of the most popular. Using MySQL is vital to keeping these databases secure and running smoothly.

Managing and backing up servers and databases can all be done in MySQL.
To help these complex processes run smoothly, utilities have been developed. Mysqldump is one of those tools meant to make the lives of developers easier.

In this guide, we are going to walk you through what utility 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.

What 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 data

Companies 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.

How to use mysqldump

By 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:

  1. Exporting specific tables in a MySQL database
  2. Exporting single databases
  3. Exporting the entire MySQL server

Exporting MySQL tables

Make 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 command structure:

mysqldump [options] db_name [table_name ...]
Enter fullscreen mode Exit fullscreen mode

For the live command, replace [options] with the valid option names or flags. These will most likely include -u and -p, which stands for user and password. When using more than one option, be careful of the order they are listed in because they will be processed in order from first to last. Here, [table_name…] will be replaced by the name of the table you are exporting. Different tables must be separated by spaces. The following example is for backing up tables called mystery and cash, and the name of the database is db_cooper.

mysqldump -u username -p db_cooper mystery cash > file_name.sql
Enter fullscreen mode Exit fullscreen mode

You will then provide the password for the database user because it is not passed along with the -p flag. The > character indicates the output redirection that will create the dump file. Here, file_name is what the final file will be called.

Exporting a MySQL database

The 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 command structure:

mysqldump -u username -p --databases db_larry db_curly db_moe > file_name.sql
Enter fullscreen mode Exit fullscreen mode

The database you will export comes after the --databases option. The space character separates multiple databases.

Exporting a MySQL server

The command is very similar for entire servers as well.

Match your inputs to conform to the following command structure:

mysqldump -u username -p --all-databases
Enter fullscreen mode Exit fullscreen mode

The command itself is pretty basic, with --all-databases indicating that everything on the server should be dumped. If there are specific requirements, that is where the options come in for the command. Adding -compatible will make the file that gets exported compatible with older MySQL servers or database systems.

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 --no-data will only back up the database structure, using --no-create-info backs up the database without any structure.

Importing a mysqldump

Importing a .sql file is straight forward. The only kink is to make sure the target server has a blank database before importing anything.

Match your inputs to conform to the following command structure:

mysql -u root -pmypassword wpdb < db_backup.sql
Enter fullscreen mode Exit fullscreen mode

The mysqlimport command will also work on databases you want to restore that already exists on the target machine:

mysqlimport -u root -pmypassword wpdb < wpdb_backup.sql
Enter fullscreen mode Exit fullscreen mode

You also have the option to import all databases by running a command that looks like this:

mysql -u root -pmypassword < alldb_backup.sql
Enter fullscreen mode Exit fullscreen mode

What does the --quick flag do?

Mysqldump can operate in one of two ways.

  1. The tool can go grab all data at once, buffer it in memory, and then dump it.
  2. It dumps the tables row by row.

The second method is important when dealing with large tables. By using the --quick flag, mysqldump reads large databases without needing large amounts of RAM to fit the full table into the memory.
This ensures that the databases will be read and copied correctly on systems with small amounts of RAM and large data sets.

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.

mysqldump -u root -pmypassword wpdb --tables thetable --where="date_created > '2017-04-27'" > wpdb_myrecord.sql
Enter fullscreen mode Exit fullscreen mode

Troubleshooting common errors

Along 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 table

To 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:

  • max_allowed_packet
  • net_write_timeout
  • net_read_timeout
  • innodb_buffer_pool_size

The adjustments to the file will be under the [mysqld] and [mysqldump] sections and will look like this:

[mysqld]
innodb_buffer_pool_size=100M
max_allowed_packet=1024M
Enter fullscreen mode Exit fullscreen mode
[mysqldump]
max_allowed_packet=1024M
net_read_timeout=3600
net_write_timeout=3600
Enter fullscreen mode Exit fullscreen mode

Error 2020: got packet bigger than 'max_allowed_packet' bytes when dumping table

If 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 [mysqld] and [mysqldump] sections. Save and close the file when finished, then restart MySQL for the changes to take effect.

The changes will look like this:

[mysqld]
max_allowed_packet=desired-value
Enter fullscreen mode Exit fullscreen mode
[mysqldump]
max_allowed_packet=desired-value
Enter fullscreen mode Exit fullscreen mode

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 --ignore-table option. To identify the table, you will have to state both the database and table names.

mysqldump -u root -pmypassword example_db --ignore-table=name_of_table > db_backup.sql
Enter fullscreen mode Exit fullscreen mode

By listing the option multiple times, you can ignore multiple tables:

mysqldump -u root -pmypassword example_db --ignore-table=table --ignore-table=tableaux > db_backup.sql
Enter fullscreen mode Exit fullscreen mode

Selecting the database returns 'unknown database'

This error happens most often when you use the -p flag in the command line with the password and there is a space in between -p and mypassword. If this happens when using "root" as the user with a password of "base-face", there will be an error stating "Unknown database base-face."

The correct input would look like this:

mysqldump -u root -pbase-face wpdb > wpdb_backup.sql
Enter fullscreen mode Exit fullscreen mode

Error 1044 when selecting the database

If 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:

mysql -u root -p
Enter fullscreen mode Exit fullscreen mode

Then enter the correct password, and proceed to grant privileges to the selected user.

GRANT ALL PRIVILEGES ON wpdb.* TO 'not_a_hacker'@'inconspicuous_host';
Enter fullscreen mode Exit fullscreen mode

After that, flush the privileges and exit from MySQL by entering the command:

FLUSH PRIVILEGES;

EXIT;
Enter fullscreen mode Exit fullscreen mode

Access denied for user when trying to connect to mysqldump

This error has several possible causes. Here's three of the most common causes of the issue.

Wrong mysqldump command

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:

mysqldump -u user -pmypasword database > database.sql
Enter fullscreen mode Exit fullscreen mode

If you fail to specify a username or password then it will spit back the following message:

mysqldump: Got error: 1045: "Access denied for user 'user' @ 'localhost' (using password: NO)" when trying to connect
Enter fullscreen mode Exit fullscreen mode

Remote host not allowed to connect to database

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.

Wrong user credentials

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.

Conclusion

Mysqldump 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 using SimpleBackups

SimpleBackups.io is a database and website backup automation tool. It automates MySQL backups using mysqldump to securely dump your MySQL databases and send them offsite to the cloud for storage. When you need to ensure your MySQL backups are secure, you can trust SimpleBackups to take care of it for you.

Discussion (0)

pic
Editor guide