Easy way to backup and restore MySQL database

Backups have two goals. The main purpose is to recover lost data (deleted or corrupted due) when the data. The second goal is to restore data to a point in the past. Because of its function, the backup process requires the user to copy data, which ultimately consumes the capacity of the storage medium. This encourages found simplify backup data storage technologies such as deduplication and compression.

In this article, we will discuss how you can easily backup and restore databases, in particular through the database CLI (Command Line Interface) with the MySQL system.

If the user name requires a password, you need to enter a password to check in after this command is executed; if the database user name without a password, do not add "-p" argument, the same time import. Note that the user name needs to have operating authority corresponding to the database, or can not export data. Because it is for system maintenance and export all databases, we usually use the root and other superuser privileges.

How to back up and restore MySQL database

MySQL database backup and restore steps as follows:

How to back up the database using the CLI

Use mysqldump tool provided by MySQL, MySQL database backup or export is very easy.

mysqldump -u[uname] -p[pass] [dbname] > [backupfile].sql

  • [Uname] - is the name of the user has access to the database;
  • [Pass] - the user password;
  • Name of the database is to be backed up or exported - [dbname];
  • [Backupfile] - is the name of the backup file.

Note that where: -u and -p back directly with a user name and password, without spaces.

How to restore a database using the CLI

Another way to restore or import a MySQL database backup files is to use our previous mysql as back up or export the database as easy.

mysql -u[uname] -p[pass] [dbname] < [backupfile].sql

  • [Uname] - is the user name has access to the database;
  • [Pass] - the user password;
  • [Dbname] - the name of the database is to be restored or imported;
  • [Backupfile] - is the name of the file to restore.

We will give a database restore example, as follows:

Easy way to backup and restore MySQL database

Other Reference:

Command to back up a remote MySQL database
mysqldump -hhostname -uusername -ppassword databasename> backupfile.sql
 
backup MySQL database backup MySQL database format with deleting table with drop table format that allows the backup to overwrite the existing database without the need to manually delete the original database.
mysqldump --- add-drop-table -uusername -ppassword databasename> backupfile.sql
 
directly compressed backup MySQL database
mysqldump -hhostname -uusername -ppassword databasename | gzip> backupfile.sql.gz

Backup a MySQL database (s) table
mysqldump -hhostname -uusername -ppassword databasename specific_table1 specific_table2> backupfile.sql
 
back up multiple MySQL database
databasename1 databasename2 databasename3> multibackupfile.sql mysqldump -hhostname -uusername -ppassword --databases
 
only back up the database structure
mysqldump --no-data --databases databasename1 databasename2 databasename3 > structurebackupfile.sql
 
all database backup server
mysqldump --all-databases allbackupfile.sql
 
restore command MySQL database
mysql -hhostname -uusername -ppassword databasename <backupfile.sql
 
reduction compression MySQL database
gunzip <backupfile.sql.gz | mysql -uusername -ppassword databasename

Good luck, I hope this helps newcomers to Linux.

Guess you like

Origin www.linuxidc.com/Linux/2020-03/162491.htm