Summary of database backup and recovery commands in MySQL

Source: Internet
Author: User
In mysql, you only need to use the mysqldump command to back up and restore database data tables. you can use mysqldump in both windows and linux to back up and restore databases.

In mysql, you only need to use the mysqldump command to back up and restore database data tables. you can use mysqldump in both windows and linux to back up and restore databases.

Let's talk about using mysqldump for backup.


SQL to back up the MyISAM table:

The code is as follows:

/Usr/local/mysql/bin/mysqldump-uyejr-pyejr -- default-character-set = utf8 -- opt -- extended-insert = false


-- Triggers-R -- hex-blob-x db_name> db_name. SQL

Use the following SQL to back up the Innodb table:

The code is as follows:

/Usr/local/mysql/bin/mysqldump-uyejr-pyejr -- default-character-set = utf8 -- opt -- extended-insert = false


-- Triggers-R -- hex-blob -- single-transaction db_name> db_name. SQL


Next, let's talk about how to use mysqldump to restore the backup data.


The file backed up by mysqldump is an SQL script that can be directly imported. There are two ways to import data.

• Directly use the mysql client
For example:

The code is as follows:

/Usr/local/mysql/bin/mysql-uyejr-pyejr db_name <db_name. SQL

• Use SOURCE syntax
In fact, this is not a standard SQL syntax, but a function provided by the mysql client, for example:

The code is as follows:

SOURCE/tmp/db_name. SQL;

Here, you must specify the absolute path of the file and the file must be read by the mysqld running user (such as nobody.


You may also know a command named mysqlhotcopy, which I will briefly describe.

Mysqlhotcopy supports copying multiple databases at a time and regular expression. The following are examples:

The code is as follows:
Root #/usr/local/mysql/bin/mysqlhotcopy-h = localhost-u = yejr-p = yejr db_name/tmp

(Set the Database Directory db_name

Copy to/tmp)

The code is as follows:
Root #/usr/local/mysql/bin/mysqlhotcopy-h = localhost-u = yejr-p = yejr db_name_1... db_name_n/tmp
Root #/usr/local/mysql/bin/mysqlhotcopy-h = localhost-u = yejr-p = yejr db_name./regex/tmp

For more detailed usage, refer to the manual or call the following command to view the help of mysqlhotcopy:

The code is as follows:
Perldoc/usr/local/mysql/bin/mysqlhotcopy

Note: To use mysqlhotcopy, you must have the SELECT and RELOAD permissions (to execute flush tables) and must be able to read

Datadir/db_name directory permission.

Restore mysqlhotcopy

Mysqlhotcopy backs up the entire database directory, which can be directly copied to the datadir specified by mysqld (here is

In the/usr/local/mysql/data/) directory, pay attention to permission issues, as shown in the following example:

The code is as follows:
Root # cp-rf db_name/usr/local/mysql/data/
Root # chown-R nobody: nobody/usr/local/mysql/data/

(Change the owner of the db_name directory to mysqld)

Well, other SQL statements are not described here. you can search Baidu.


Use shell commands in linux

The code is as follows:
Shell> mysqldump -- quick db_name | gzip> db_name.contents.gz

(The file created in this example is in the compressed format ).

The command for restoring/transferring to another instance is as follows:

The code is as follows:
Shell> gunzip <db_name.contents.gz | mysql db_name

The preceding command is applicable to * nix operating system machines.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.