CentOSLinuxTutorialsUbuntu

How To Backup and Restore MySQL Database Using Command Line

How To Backup and Restore MySQL Database Using Command Line

In this tutorial, we will show you how to backup and restore the MySQL database using Command-Line. For those of you who didn’t know, Backing up and restoring a MySQL database is an important aspect of database management. Backups ensure that your data is safe and can be recovered in case of data loss due to hardware failure, software bugs, or human error.

This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo‘ to the commands to get root privileges. I will show you the step-by-step installation of backup and restore MySQL on a Linux server.

Prerequisites

  • A server running one of the following operating systems: Ubuntu and any other Debian-based distribution or CentOS Linux.
  • An active internet connection.
  • SSH access to the server (or just open Terminal if you’re on a desktop).
  • A non-root sudo user or access to the root user. We recommend acting as a non-root sudo user, however, as you can harm your system if you’re not careful when acting as the root.

The parameters of the said command are 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.

Backing Up a MySQL Database using the Command Line

The mysqldump the utility is used to create a backup of a MySQL database. This tool is included in the MySQL server package and can be used to back up the entire database or individual tables.

  • Backup MySQL/MariaDB 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 into 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

Restoring a MySQL Database using the Command Line

  • Restore MySQL/MariaDB 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

Conclusion

Backing up and restoring a MySQL database is a critical aspect of database management. Using the command line is a powerful and efficient way to perform these tasks. Using the command line is a great way to do this, as it gives you complete control over the backup and restores the process. By following the steps outlined in this tutorial, you should now be able to easily back up and restore your MySQL databases using the command line on your Linux system.

Thanks for using this tutorial to back up and restore MySQL/MariaDB database using command line. For additional help or useful information, we recommend you check the official MySQL website.

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!

Save

r00t

r00t is a seasoned Linux system administrator with a wealth of experience in the field. Known for his contributions to idroot.us, r00t has authored numerous tutorials and guides, helping users navigate the complexities of Linux systems. His expertise spans across various Linux distributions, including Ubuntu, CentOS, and Debian. r00t's work is characterized by his ability to simplify complex concepts, making Linux more accessible to users of all skill levels. His dedication to the Linux community and his commitment to sharing knowledge makes him a respected figure in the field.
Back to top button