The backup artifact that comes with mysql--mysqldump

Preface

In many cases, we will need to perform operations such as backup transfer to mysql. There are many ways to back up, one of which is mysql's own, that is mysqldump.

Mysqldump command

The mysqldump tool is a client application that comes with mysql, in the /bin directory of the mysql installation directory. mysqldump is used for logical backup. It can generate a set of SQL statements. Executing these statements can regenerate the original database object definitions and table data. mysqldump is often used to dump one or more MySQL databases for backup or transfer to another SQL server. The mysqldump command can also generate output in CSV, other delimited text, or XML format.

mysqldump needs at least the SELECT permission of the dump table, the SHOW VIEW permission of the dump view, and the trigger permission of the dump trigger. If the --single-transaction option is not used, the table is locked. If the --no-tablespaces option is not used, then ( Starting from MySQL 5.7.31) process permissions. Some options may require other privileges mentioned in the option description.

The advantages of mysqldump include the convenience and flexibility of viewing and even editing the output before recovery. You can clone the database for development and DBA work, or make slight changes to the existing database for testing. But it is not a fast or scalable solution for backing up large amounts of data. For big data, even if the backup step takes a reasonable amount of time, restoring the data will be very slow, because replaying SQL statements involves disk I/O for inserting, creating indexes, etc.

For large-scale backup and recovery, physical backup is more suitable. Physical backup can copy data files into an original format that can be quickly restored.

Call syntax

There are usually three ways to use mysqldump-dump a group or multiple tables, dump a group or multiple complete databases, or dump a complete MySQL server-as follows:

shell> mysqldump [options] db_name [tbl_name ...]
shell> mysqldump [options] --databases db_name ...
shell> mysqldump [options] --all-databases

To view the list of options supported by your version of mysqldump, use the command mysqldump --help to view

The above options are generally the user and password of the database. There are generally two ways

Method 1: No need to manually write the password in the command, use -u username -p, this way will execute the command and let us enter the password

grammar:

mysqldump -u 用户名 -p 数据库名 > (目录)导出文件名

Examples:

[root@localhost mysql-5.7.24]# mysqldump -uroot -p  --all-databases > dump.sql

Method 2: Enter the password directly in the command, -u followed by the user name, -p followed by a space and then enter the password

grammar:

mysqldump -u用户 -p 密码 数据库名 > (目录)导出文件名

Instance

[root@localhost bin]# mysqldump -uroot -proot --all-databases > dbdump.sql

Note: There can be no spaces after -p. You need to write the password directly. After executing the command, the following prompt will appear, which means that using the password on the command line interface is not safe.

mysqldump: [Warning] Using a password on the command line interface can be insecure.

Use mysqldump for backup

The following describes how to use mysqldump to generate a dump file and how to reload the dump file. The dump file can be used in several ways:

1、作为备份,以便在数据丢失的情况下恢复数据。

2、作为设置副本的数据源。

3、作为实验数据的来源:

 - 制作可在不更改原始数据的情况下使用的数据库副本。
 - 测试潜在的升级不兼容性。

Use mysqldump to dump data in SQL format

The following describes how to use mysqldump to create a dump file in SQL format.

By default, mysqldump writes information to standard output as SQL statements. You can save the output to a file:

shell> mysqldump [arguments] > file_name

To dump all databases, call mysqldump–all-databases with the following options:

shell> mysqldump --all-databases > dump.sql

To dump only specific databases, name them on the command line and use the --databases option:

shell> mysqldump --databases db1 db2 db3 > dump.sql

The –databases option causes all names on the command line to be treated as database names. Without this option, mysqldump uses the first name as the database name and the following names as the table name.

When using --all-databases or --databases, mysqldump writes CREATE DATABASE and USE statements before the dump output of each database. This ensures that when the dump file is reloaded, if the dump file does not exist, it will create each database and set it as the default database so that the database contents can be loaded into the same database they came from. If you want the dump file to forcefully delete each database before creating each database, please also use the --add-drop-database option.

To dump a single database, name it on the command line:

shell> mysqldump --databases test > dump.sql

In the case of a single database, the following --databases option can be ignored:

shell> mysqldump test > dump.sql

The difference between the previous two commands is that if there is no-databases, the dump output does not contain CREATE DATABASE or USE statements. This has several implications:

1、重新加载转储文件时,必须指定默认数据库名称,以便服务器知道要重新加载哪个数据库。

2、对于重新加载,您可以指定与原始名称不同的数据库名称,这使您能够将数据重新加载到不同的数据库中。

3、如果要重新加载的数据库不存在,则必须先创建它。

4、因为输出不包含CREATE DATABASE语句,所以——add-drop-database选项不起作用。如果您使用它,它不会产生DROP DATABASE语句。

To dump only a specific table in the database, name it in the command line after the database name:

shell> mysqldump test t1 t3 t7 > dump.sql

Dump table definition and content separately

The -no-data option tells mysqldump not to dump table data, which causes the dump file to contain only table creation statements. Instead, the –no-create-info option tells mysqldump not to output CREATE statements, so that the dump file contains only table data.

For example, to dump the table definition and data of the test database separately, use the following command:

shell> mysqldump --no-data test > dump-defs.sql
shell> mysqldump --no-create-info test > dump-data.sql

For a defined dump, adding --routines and --events options can also include stored procedures and event definitions:

shell> mysqldump --no-data --routines --events test > dump-defs.sql

Reload the backup in SQL format

To reload a dump file containing SQL statements written by mysqldump, use it as input to the mysql client. If the dump file was created by mysqldump with the --all-databases or --databases option, it contains CREATE DATABASE and USE statements, and there is no need to specify a default database to load data into it:

Example:

shell> mysql < dump.sql

Or use the source command in mysql:

mysql> source dump.sql

If the file is a single database dump that does not contain CREATE DATABASE and USE statements, create the database first:

shell> mysqladmin create db1

Then specify the database name when loading the dump file:

shell> mysql db1 < dump.sql

Copy the database from one server to another

On server 1:

shell> mysqldump --databases db1 > dump.sql

Copy the dump file from server 1 to server 2.

On server 2:

shell> mysql < dump.sql

Guess you like

Origin blog.csdn.net/qq_36551991/article/details/111409411