How To Backup and Restore MySQL Database Using Command Line

Backup and Restore MySQL Database Using Command Line

This tutorial guide will show you two easy ways to Backup and Restore MySQL Database Using Command Line – mysqldump. You can also use this process to move your data to a new web server. 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.

The parameters of the said command as follows.

  • [uname] Your database username.
  • [passwd] The password for your database (note there is no space between -p and the password).
  • [dbname] The name of your database.
  • [backupdb.sql] The filename for your database backup.

Backup and Restore MySQL Database Using Command Line

Backup MySQL database

First, you can check MySQL databases from your server:

#mysql -h localhost -u root -p
#mysql> show databases;

The following command will dump all databases to an sql file. Replace pass with your root database password and filename with the name of the file you wish to create such as backupdb.sql

Back up multiple databases in MySQL

 #mysqldump –u[uname] –p[passwd] [database name 1] [database name 2] > backup.sql

Example:

 #mysqldump –u root –pidroidus chedelics radiks > backup.sql

Backup all databases in MySQL

 #mysqldump –u [uname] –p[passwd] –all-databases > backup.sql

Example:

 #mysqldump –u root –pidroidus –all-databases > backup.sql

Back up your MySQL Database with Compress

 #mysqldump -u root -p[passwd] --databases [dbname] | gzip > backup.sql.gz

Example:

 #mysqldump -u root -pidroidus --databases  | gzip > backup.sql.gz

Restore MySQL database from a backup file

Above we backup the Tutorials database into backupdb.sql file. To re-create the Tutorials database you should follow two steps:

  • Create an appropriately named database on the target machine
  • Load the file using the mysql command:
 #mysqladmin -u root -p create [dbname]
 #gzip -d backupdb.sql.gz #mysql -uroot -p[passwd] [dbname] < backupdb.sql

Example:

#mysqladmin -u root -p create chedelics
#gzip -d backupdb.sql.gz
#backupdb.sql
#mysql -uroot -pidroidus chedelics < backupdb.sql

How To Backup and Restore MySQL Database Using Command Line

Backup and Restore MySQL Database

Many of the world’s largest and fastest-growing organizations including Facebook, Google, Adobe, Alcatel Lucent and Zappos rely on MySQL to save time and money powering their high-volume Web sites, business-critical systems and packaged software.

In this tutorial we will guide you through two easy ways to backup and restore the data in your MySQL database using mysqldump. You can also use this process to move your data to a new web server. We assume that you already have MySQL installed on Linux system with administrative privileges and we assume that you already have badic knowledge on MySQL and command line or terminal.

The parameters of the said command as follows.

  • [uname] Your database username.
  • [passwd] The password for your database (note there is no space between -p and the password).
  • [dbname] The name of your database.
  • [backupdb.sql] The filename for your database backup.

Backup and Restore MySQL Database Using Command Line

Backup MySQL database

First, you can check MySQL databases from your server:

#mysql -h localhost -u root -p
#mysql> show databases;

The following command will dump all databases to an sql file. Replace pass with your root database password and filename with the name of the file you wish to create such as backupdb.sql

Back up multiple databases in MySQL

 #mysqldump –u[uname] –p[passwd] [database name 1] [database name 2] > backup.sql

Example:

 #mysqldump –u root –pidroidus chedelics radiks > backup.sql

Backup all databases in MySQL

 #mysqldump –u [uname] –p[passwd] –all-databases > backup.sql

Example:

 #mysqldump –u root –pidroidus –all-databases > backup.sql

Back up your MySQL Database with Compress

 #mysqldump -u root -p[passwd] --databases [dbname] | gzip > backup.sql.gz

Example:

 #mysqldump -u root -pidroidus --databases  | gzip > backup.sql.gz

Restore MySQL database from a backup file

Above we backup the Tutorials database into backupdb.sql file. To re-create the Tutorials database you should follow two steps:

  • Create an appropriately named database on the target machine
  • Load the file using the mysql command:
 #mysqladmin -u root -p create [dbname]
 #gzip -d backupdb.sql.gz #mysql -uroot -p[passwd] [dbname] < backupdb.sql

Example:

#mysqladmin -u root -p create chedelics
#gzip -d backupdb.sql.gz
#backupdb.sql
#mysql -uroot -pidroidus chedelics < backupdb.sql

You Might Also Like: How To Reset Root Password on MySQL Server