MySQL Backup and Restore Commands for Database Administration

This article shows you several practical examples on how to perform various backup operations of MySQL databases using 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 database or collection of databases for backup into a single flat file.

How to backup mysql database

How to Backup and Restore MySQL Database

We assume that you already have MySQL installed on Linux system with administrative privileges and we assume that you already have a small amount of knowledge on MySQL. If you don’t have MySQL installed or don’t have any exposure to MySQL then read our articles below.

  1. Install MySQL Server on RHEL/CentOS 6-5, Fedora 17-12
  2. 20 MySQL Commands for Database Administration

How to Backup MySQL Database?

To take a backup of MySQL database 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 as follows.

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

How to Backup a Single MySQL Database?

To take a backup of single database, use the command as follows. The command will dump database [rsyslog] structure with data on to 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 in to 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 backup of all databases, then use the following command with 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 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 only without structure, then 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 Single Table of Database?

With the below command you can take backup of single table or certain tables of your database. For example, the following command only take backup of 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 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 remote server [172.16.25.126] database [gallery] into a local server.

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

How to Restore MySQL Database?

In the above tutorial we have seen the how to take the backup of databases, tables, structures and data only, now we will see how to restore them using 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 exist on 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.

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.

Ravi Saive

I am Ravi Saive, creator of TecMint. A Computer Geek and Linux Guru who loves to share tricks and tips on Internet. Most Of My Servers runs on Open Source Platform called Linux. Follow Me: Twitter, Facebook and Google+

Your name can also be listed here. Got a tip? Submit it here to become an TecMint author.

RedHat RHCE and RHCSA Certification Book
Linux Foundation LFCS and LFCE Certification Preparation Guide

You may also like...

47 Responses

  1. Bile Bakshi says:

    I would like to share somethings after go through this web site. I crack Linux system administrator interview on first attempt. And I got the job. And I have crack the second interview on second attempt. And I have offer letter from second one. I m very happy.

  2. Venkat says:

    Thanks for this very beautiful and clear article.

  3. siva says:

    Hi Ravi, backup and restore commands are really useful.

    I was looking for the command to know the backup status. can you please help on that.

    • Ravi Saive says:

      @Siva,

      There isn’t any command that shows backup status, you need to check manually after taking backup manually or through script…or you can set a ‘mail to’ in script to get email notification once backup is complete..

  4. Bashir Deen says:

    This is such a helpful article with regarding to mysql backups.
    thanks.

  5. Tad Osborn says:

    Your last item regarding mysqlimport is not accurate. Two problems:
    1) < character is not used
    2) mysqlimport is for importing text files (csv) and the file name needs to be the table name. The program will automatically use the string in front of the period as the table name…so it errors out because it can't find a table named rsyslog in the database rsyslog.

    • Ravi Saive says:

      @Tad,

      Thanks for pointing out, yes it should be mysql or mysqladmin for restoring databases. I will make the changes as suggested..

  6. Cyrille Tarla says:

    Thanks this was really good and it did help me alot

  7. Gibran says:

    How to repair single database and all databases

  8. Abmysql http://www.abmysql.com is a free web service,
    with abmysql you can run automatically some backup when you want on a secure server (provide by abmysql) and to another FTP server, google drive, Dropbox,etc.

    Try now http://www.abmysql.com, it’s free !

Got something to say? Join the discussion.

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.