Backing up and restoring MySQL databases using dump files

Introduction

In this article, I will write about database dump files, what they are, and how you can leverage them in the application development cycle. I will focus on creating dump files for MySQL databases here but the process is alike for other relational database systems, just a few syntax differences. In the end, you will know how to confidently generate and use dump files.

Prerequisites

  • Familiarity with MySQL

  • A MySQL database and a user with full access to the database

  • A little familiarity with working with cmd-like interfaces

  • All of the examples included here are written using a non-root user on Ubuntu22.04. (This is not really important.)

What is a dump file?

A dump file, also known as a backup file, is a file that contains a database's schema, data and other related information. It is usually used to replicate or restore a database. Say the database server crashed and data was lost in the process, an updated dump file can be used to restore the database and its data. Another practical scenario is when you need to set up several database servers for an application, ideally, these databases will need to have the same table structure. With the help of a dump file, you can easily set them all up in no time. I believe you can already begin to see areas where you could make use of dump files to hasten the development process.

mysqldump

MySQL comes with a command-line utility called mysqldump . It's an executable that creates a backup of a MySQL database by dumping its data, schema and other related information to a file. Its output is all the SQL commands needed to replicate the database.

The location of this executable varies depending on the operating system but provided you do not have issues with your MySQL installation and setup, its path would already be added to the operating system's PATH variable. Otherwise, you simply need to add the path to the directory where MySQL was installed to the operating system's PATH variable and all would go well.

Creating a dump file

To create a dump file, we simply use the mysqldump command-line utility.

mysqldump -u username -p --databases --add-drop-database dbname > db_dump.sql

Let me break down what each part of this command means:

  • we are calling the mysqldump command and passing it some parameters (remember it's an executable)

  • the u flag is used to specify the user we are using to access the MySQL server, this defaults to the username of the current user.

  • the databases and add-drop-database flags are optional but are used here because I will like the generated SQL commands to include a statement to create the database if it doesn't exist. That's what the add-drop-database flag does but it doesn't seem to work without the databases flag. The databases flag is actually used to tell mysqldump that we want to dump more than one database, without that flag, mysqldump treats everyother name after the first db_name as table names. Although it looks like we don't need the databases flag, but the add-drop-database flag seems not to work without it.

  • the p flag makes mysqldump prompts us to enter the password for the intended user.

  • this is followed by the name of the database we want to create a dump file for.

  • then we direct the output of that command to a file called db_dump.sql. This can also be a path including the file name.

And that's all, it's that simple. When you press enter, mysqldump populates the file db_dump.sql with the necessary SQL commands needed to replicate the database.

If you're using window's powershell to run mysqldump, you shouldn't use > to write the output to a file because it will be encoded in utf-16 which is not permitted as a connection character set, so the dump file cannot be loaded correctly. You should use the result-file flag instead. The command then looks like this:

mysqldump -u username -p --databases --add-drop-database dbname --result-file=db_dump.sql

Generate a database from a dump file

To restore a database from the dump file, you simply run

mysql -u username -p < db_dump.sql

And that's all. Once that is completed, you can log into mysql and use the show databases; command to confirm that the database does exist and you can query it to confirm that the tables are also updated.

I will like to add here that if we created the dump file without the --databases and --add-drop-database flag, all would still go well, we'll only need to specify the name of the database we are dumping the tables and data from the dump file into. The command would then look like this:

mysql -u username -p db_name < db_dump.sql

Common errors and how to solve them

  1. Can't connect to local MySQL server through ...

    This means the mysql server is not running yet. You simply need to start the server. I am currently running Ubuntu 22.04 on WSL as a non-root user, so to start my MySQL server I simply run sudo service mysql start. Whichever platform you're running, just make sure the mysql server is running and you'll no longer see that error.

  2. Access denied; you need (at least one of) the PROCESS privilege(s)...

    This occurs with newer version of MySQL, there are few ways to resolve this:

    • Instead of using the intended user, you can use the root user, depending on how you setup the root user, you might not need the p flag anymore and will have to use sudo instead. In that case, the command looks something like this:

        sudo mysqldump --databases --add-drop-database db_name > dump_file.sql
      
    • Another option is to grant the PROCESS privilege to the intended user. To grant the PROCESS privilege, log in as an administrator user(or root) and run the following query: GRANT PROCESS ON *.* TOusername@localhost; The PROCESS privilege is a global one, so you can't grant it on individual databases.

      Mind you, this has its caveat, security-wise.

    • Another option is to use the --no-tablespaces flag.

        mysqldump -u username -p --no-tablespaces --databases --add-drop-database dbname > dump_file.sql
      

      If you want to know about tablespaces, you can find more info about it here.

Thanks for reading up to this point. I am sure you can now use database dump files confidently. If you have questions, please do share them in the comment section.