MySQL Backup and Restore Commands for Database Administration

This article shows you several practical examples on how to perform various backup operations of MySQL/MariaDB databases using the mysqldump command and also we will see how to restore them with the help of mysql and mysqlimport command in Linux.

mysqldump is a command-line client program, it is used to dump local or remote MySQL databases or collections of databases for backup into a single flat file.

We assume that you already have MySQL installed on the Linux system with administrative privileges and we believe that you already have a small amount of knowledge of MySQL.

If you don’t have MySQL installed or don’t have any exposure to MySQL then read our articles below.

How to Backup MySQL Database in Linux?

To take a backup of MySQL databases or databases, the database must exist in the database server and you must have access to it. The format of the command would be.

# mysqldump -u [username] –p[password] [database_name] > [dump_file.sql]

The parameters of the said command are as follows.

  • [username] : A valid MySQL username.
  • [password] : A valid MySQL password for the user.
  • [database_name] : A valid Database name you want to take backup.
  • [dump_file.sql]: The name of the backup dump file you want to generate.

How to Backup a Single MySQL Database?

To take a backup of a single database, use the command as follows. The command will dump the database [rsyslog] structure with data onto a single dump file called rsyslog.sql.

# mysqldump -u root -ptecmint rsyslog > rsyslog.sql

How to Backup Multiple MySQL Databases?

If you want to take backup of multiple databases, run the following command. The following example command takes a backup of databases [rsyslog, syslog] structure and data into a single file called rsyslog_syslog.sql.

# mysqldump -u root -ptecmint --databases rsyslog syslog > rsyslog_syslog.sql

How to Backup All MySQL Databases?

If you want to take a backup of all databases, then use the following command with the option –all-database. The following command takes the backup of all databases with their structure and data into a file called all-databases.sql.

# mysqldump -u root -ptecmint --all-databases > all-databases.sql

How to Backup MySQL Database Structure Only?

If you only want the backup of the database structure without data, then use the option –no-data in the command. The below command exports database [rsyslog] Structure into a file rsyslog_structure.sql.

# mysqldump -u root -ptecmint -–no-data rsyslog > rsyslog_structure.sql

How to Backup MySQL Database Data Only?

To backup database data without structure, use the option –no-create-info with the command. This command takes the database [rsyslog] Data into a file rsyslog_data.sql.

# mysqldump -u root -ptecmint --no-create-db --no-create-info rsyslog > rsyslog_data.sql

How to Backup a Single Table of Database?

With the below command you can take a backup of a single table or specific tables of your database. For example, the following command only takes a backup of the wp_posts table from the database wordpress.

# mysqldump -u root -ptecmint wordpress wp_posts > wordpress_posts.sql

How to Backup Multiple Tables of Database?

If you want to take a backup of multiple or certain tables from the database, then separate each table with space.

# mysqldump -u root -ptecmint wordpress wp_posts wp_comments > wordpress_posts_comments.sql

How to Backup Remote MySQL Database

The below command takes the backup of the remote server [] database [gallery] into a local server.

# mysqldump -h -u root -ptecmint gallery > gallery.sql

How to Restore MySQL Database?

In the above tutorial, we have seen how to take the backup of databases, tables, structures, and data only, now we will see how to restore them using the following format.

# # mysql -u [username] –p[password] [database_name] < [dump_file.sql]

How to Restore Single MySQL Database

To restore a database, you must create an empty database on the target machine and restore the database using msyql command. For example, the following command will restore the rsyslog.sql file to the rsyslog database.

# mysql -u root -ptecmint rsyslog < rsyslog.sql

If you want to restore a database that already exists on the targeted machine, then you will need to use the mysqlimport command.

# mysqlimport -u root -ptecmint rsyslog < rsyslog.sql

In the same way, you can also restore database tables, structures, and data. If you liked this article, then do share it with your friends.

Tutorial Feedback...
Was this article helpful? If you don't find this article helpful or found some outdated info, issue or a typo, do post your valuable feedback or suggestions in the comments to help improve this article...

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.

49 thoughts on “MySQL Backup and Restore Commands for Database Administration”

    • First dump all your databases into one file using:

      # mysqldump -u username -p –all-databases > dump.sql 

      And then restore them all using:

      # mysql -u username -p < dump.sql
  1. I just had upgraded/installed my mysql version from 5.1 to 5.5 redhat in a way that i had to remove the existing version using the command ‘rpm -e’ then installed the new version. I successfully installed the new version which is the mysql 5.5 but the problem is when i was going to restore my database using innobackupex it fails. It could not copy the file or database to its targeted folder when i use the command ‘innobackupex –copy-back /home/backup’. I did not use the mysqldump during the installation or upgrade. I need your help on this sir..thank you.

  2. hi
    thank you for these articles
    I want to back up all tables form a database , except only one table (for example or two tables)
    what do i do ?

  3. Thanks for this. I have not yet tried it. But sure this will help me to backup and restore my databases faster than the usual way. Specially those databases larger than 1GB.

  4. Thanks for such a nice and consolidated information. please tell how to restore a single table, will it not overwrite the existing one? if so can it be prevented?

      • I’ve attempted to restore…or update my database on my development machine with a MySQL dump file I made on my production machine.

        when logged into my dev machine, used your restore command, and it just didn’t do anything….lots of info explaining how MySQl import works, but it did NOT import the database and overwrite the residing db.

        The ONLY difference in my code was that I wait for the machine to ask me for my password, and I have a full path to the db file I want to import. I changed the name of the file to just the name of the db.sql.

        Have any suggestions as to why it should not work?

        mysqlimport -u root -p dbname < /home/web/pathtofile/dbname.sql is the command I used.

        can you help me?

        • No you can’t overwrite database on existing database, the only way to restore database is, first remove the existing database and create new database with same name and then try the restore command it will work.


Leave a Reply to Ninad Cancel reply

Have a question or suggestion? Please leave a comment to start the discussion. Please keep in mind that all comments are moderated and your email address will NOT be published.