Database: detailed explanation of mysqldump usage

Table of contents

1. Detailed command format

1.1 Syntax format

1.2 Common parameters

1.3 Complete parameters

2. Several common errors of mysqldump

2.1. Prompt command not found -bash: mysqldump: command not found

2.2、 the command line interface can be insecure

2.3、Gotpacket bigger than ‘max_allowed_packet‘ bytes


1. Detailed command format

1.1 Syntax format

mysqldump [OPTIONS] database [tables]  

1.2 Common parameters

--all-databases, -A: 备份所有数据库
--databases, -B: 用于备份多个数据库,如果没有该选项,mysqldump把第一个名字参数作为数据库名,后面的作为表名。使用该选项,mysqldum把每个名字都当作为数据库名。

--force, -f:即使发现sql错误,忽略错误继续备份
--host=host_name, -h host_name:备份主机名,默认为localhost
--no-data, -d:只导出表结构
--password[=password], -p[password]:密码
--port=port_num, -P port_num:制定TCP/IP连接时的端口号
--quick, -q:快速导出
--tables:覆盖 --databases or -B选项,后面所跟参数被视作表名
--user=user_name, -u user_name:用户名
--xml, -X:导出为xml文件

1.3 Complete parameters

--all-databases  , -A

Export all databases.

mysqldump  -uroot -p --all-databases

--all-tablespaces  , -Y

Export all tablespaces.

mysqldump  -uroot -p --all-databases --all-tablespaces

--no-tablespaces  , -y

No tablespace information is exported.

mysqldump  -uroot -p --all-databases --no-tablespaces

--add-drop-database

Add the drop database statement before each database is created.

mysqldump  -uroot -p --all-databases --add-drop-database

--add-drop-table

Add a drop data table statement before each data table is created. (The default is on, use --skip-add-drop-table to cancel the option)

mysqldump -uroot -p --all-databases (add drop statement by default)

mysqldump -uroot -p --all-databases --skip-add-drop-table (cancel drop statement)

--add-locks

Increment LOCK TABLES before each table export and UNLOCK TABLE after. (default is on, use --skip-add-locks to cancel the option)

mysqldump -uroot -p --all-databases (add LOCK statement by default)

mysqldump -uroot -p --all-databases --skip-add-locks (cancel LOCK statement)

--allow-keywords

Allows creation of column names that are keywords. This is done by prefixing the table name to each column name.

mysqldump  -uroot -p --all-databases --allow-keywords

--apply-slave-statements

Add 'STOP SLAVE' before 'CHANGE MASTER' and 'START SLAVE' at the end of the export.

mysqldump  -uroot -p --all-databases --apply-slave-statements

--character-sets-dir

directory for charset files

mysqldump  -uroot -p --all-databases  --character-sets-dir=/usr/local/mysql/share/mysql/charsets

--comments

Additional note information. It is enabled by default and can be canceled with --skip-comments

mysqldump -uroot -p --all-databases (default records comments)

mysqldump -uroot -p --all-databases --skip-comments (uncomment)

--compatible

Exported data will be compatible with other databases or older versions of MySQL. Values ​​can be ansi, mysql323, mysql40, postgresql, oracle, mssql, db2, maxdb, no_key_options, no_tables_options, no_field_options, etc.,

To use several values, separate them with commas. It does not guarantee full compatibility, but try to be as compatible as possible.

mysqldump  -uroot -p --all-databases --compatible=ansi

--compact

Export less output (useful for debugging). Remove structures such as comments and headers and tails. Options can be used: --skip-add-drop-table --skip-add-locks --skip-comments --skip-disable-keys

mysqldump  -uroot -p --all-databases --compact

--complete-insert,  -c

Use the full insert statement (including column names). Doing so can improve the insertion efficiency, but it may be affected by the max_allowed_packet parameter and cause the insertion to fail.

mysqldump  -uroot -p --all-databases --complete-insert

--compress, -C

Enable compression to pass all information between client and server

mysqldump  -uroot -p --all-databases --compress

--create-options,  -a

Include all MySQL feature options in the CREATE TABLE statement. (default is on)

mysqldump  -uroot -p --all-databases

--databases,  -B

Export several databases. All name parameters following the parameter are treated as database names.

mysqldump  -uroot -p --databases test mysql

--debug

Output debug information for debugging. The default value is: d:t:o,/tmp/mysqldump.trace

mysqldump  -uroot -p --all-databases --debug

mysqldump  -uroot -p --all-databases --debug=” d:t:o,/tmp/debug.trace”

--debug-check

Check memory and open file usage instructions and exit.

mysqldump  -uroot -p --all-databases --debug-check

--debug-info

print debug info and exit

mysqldump  -uroot -p --all-databases --debug-info

--default-character-set

Set the default character set, the default value is utf8

mysqldump  -uroot -p --all-databases --default-character-set=latin1

--delayed-insert

Export data using delayed insert mode (INSERT DELAYED)

mysqldump  -uroot -p --all-databases --delayed-insert

--delete-master-logs

Delete logs after master backup. This parameter will automatically activate --master-data.

mysqldump  -uroot -p --all-databases --delete-master-logs

--disable-keys

For each table, reference the INSERT statement with /*!40000 ALTER TABLE tbl_name DISABLE KEYS */; and /*!40000 ALTER TABLE tbl_name ENABLE KEYS */; statements. This makes importing the dump file much faster, since it creates the index after inserting all the rows. This option is only suitable for MyISAM tables and is enabled by default.

mysqldump  -uroot -p --all-databases 

--dump-slave

This option will cause the master's binlog location and filename to be appended to the exported data file. When set to 1, the CHANGE MASTER command will be output to the data file; when set to 2, description information will be added before the command. This option will turn on --lock-all-tables unless --single-transaction is specified. This option automatically turns off the --lock-tables option. The default value is 0.

mysqldump  -uroot -p --all-databases --dump-slave=1

mysqldump  -uroot -p --all-databases --dump-slave=2

--events, -E

Export events.

mysqldump  -uroot -p --all-databases --events

--extended-insert,  -e

Use INSERT syntax with multiple VALUES columns. This makes the export file smaller and speeds up the import. The default is on, use --skip-extended-insert to cancel the option.

mysqldump  -uroot -p --all-databases

mysqldump -uroot -p --all-databases --skip-extended-insert (cancel option)

--fields-terminated-by

Ignore the given field in the export file. Used with the --tab option, not with the --databases and --all-databases options

mysqldump  -uroot -p test test --tab=”/home/mysql” --fields-terminated-by=”#”

--fields-enclosed-by

Each field in the output file is wrapped with the given characters. Used with the --tab option, not with the --databases and --all-databases options

mysqldump  -uroot -p test test --tab=”/home/mysql” --fields-enclosed-by=”#”

--fields-optionally-enclosed-by

Individual fields in the output file are optionally wrapped with the given characters. Used with the --tab option, not with the --databases and --all-databases options

mysqldump  -uroot -p test test --tab=”/home/mysql”  --fields-enclosed-by=”#” --fields-optionally-enclosed-by  =”#”

--fields-escaped-by

Individual fields in the output file ignore the given characters. Used with the --tab option, not with the --databases and --all-databases options

mysqldump  -uroot -p mysql user --tab=”/home/mysql” --fields-escaped-by=”#”

--flush-logs

Flush the log before starting the export.

Please note: If you export multiple databases at once (using the option --databases or --all-databases), the log will be refreshed database by database. Except using --lock-all-tables or --master-data. In this case, the log will be flushed once and correspondingly all tables will be locked at the same time. Therefore, if you plan to export and flush logs at the same time, you should use --lock-all-tables or --master-data and --flush-logs.

mysqldump  -uroot -p --all-databases --flush-logs

--flush-privileges

After exporting the mysql database, issue a FLUSH PRIVILEGES statement. For proper recovery, this option should be used whenever exporting the mysql database and relying on mysql database data.

mysqldump  -uroot -p --all-databases --flush-privileges

--force

Ignore SQL errors that occur during export.

mysqldump  -uroot -p --all-databases --force

--help

Display a help message and exit.

mysqldump  --help

--hex-blob

Export binary string fields using hexadecimal format. This option must be used if there is binary data. The affected field types are BINARY, VARBINARY, and BLOB.

mysqldump  -uroot -p --all-databases --hex-blob

--host, -h

Host information to be exported

mysqldump  -uroot -p --host=localhost --all-databases

--ignore-table

Do not export the specified table. When specifying to ignore multiple tables, it needs to be repeated multiple times, one table at a time. Each table must specify both the database and table name. For example: --ignore-table=database.table1 --ignore-table=database.table2  …

mysqldump  -uroot -p --host=localhost --all-databases --ignore-table=mysql.user

--include-master-host-port

Add 'MASTER_HOST=, MASTER_PORT=' to the 'CHANGE MASTER TO..' statement generated by --dump-slave  

mysqldump  -uroot -p --host=localhost --all-databases --include-master-host-port

--insert-ignore

Use the INSERT IGNORE statement when inserting rows.

mysqldump  -uroot -p --host=localhost --all-databases --insert-ignore

--lines-terminated-by

Each line of the output file is delimited by the given string. Used with the --tab option, not with the --databases and --all-databases options.

mysqldump  -uroot -p --host=localhost test test --tab=”/tmp/mysql”  --lines-terminated-by=”##”

--lock-all-tables,  -x

Submit requests to lock all tables in all databases to ensure data consistency. This is a global read lock and is automatically turned off by the --single-transaction and --lock-tables options.

mysqldump  -uroot -p --host=localhost --all-databases --lock-all-tables

--lock-tables,  -l

Lock all tables before starting the export. Lock the table with READ LOCAL to allow parallel inserts into MyISAM tables. For tables that support transactions such as InnoDB and BDB, --single-transaction is a better choice, because it does not require locking the table at all.

Note that when exporting multiple databases, --lock-tables locks tables separately for each database. Therefore, this option cannot guarantee the logical consistency of tables in the export file between databases. The export status of different database tables can be completely different.

mysqldump  -uroot -p --host=localhost --all-databases --lock-tables

--log-error

Append warning and error messages to the given file

mysqldump  -uroot -p --host=localhost --all-databases  --log-error=/tmp/mysqldump_error_log.err

--master-data

This option appends the location and filename of the binlog to the output file. If it is 1, the CHANGE MASTER command will be output; if it is 2, comment information will be added before the output CHANGE MASTER command. This option turns on the --lock-all-tables option, unless --single-transaction is also specified (in which case a global read lock is acquired for a short time at the start of the export; see --single below for the rest) -transaction option). This option automatically turns off the --lock-tables option.

mysqldump  -uroot -p --host=localhost --all-databases --master-data=1;

mysqldump  -uroot -p --host=localhost --all-databases --master-data=2;

--max_allowed_packet

The maximum packet length sent and received by the server.

mysqldump  -uroot -p --host=localhost --all-databases --max_allowed_packet=10240

--net_buffer_length

The buffer size for TCP/IP and socket connections.

mysqldump  -uroot -p --host=localhost --all-databases --net_buffer_length=1024

--no-autocommit

Wrap tables with autocommit/commit statements.

mysqldump  -uroot -p --host=localhost --all-databases --no-autocommit

--no-create-db,  -n

Just export the data without adding a CREATE DATABASE statement.

mysqldump  -uroot -p --host=localhost --all-databases --no-create-db

--no-create-info,  -t

Just export the data without adding a CREATE TABLE statement.

mysqldump  -uroot -p --host=localhost --all-databases --no-create-info

--no-data, -d

Do not export any data, only the database table structure.

mysqldump  -uroot -p --host=localhost --all-databases --no-data

--no-set-names,  -N

Equivalent to --skip-set-charset

mysqldump  -uroot -p --host=localhost --all-databases --no-set-names

--opt

Equivalent to --add-drop-table, --add-locks, --create-options, --quick, --extended-insert, --lock-tables, --set-charset, --disable-keys The The option is enabled by default and can be disabled with --skip-opt.

mysqldump  -uroot -p --host=localhost --all-databases --opt

--order-by-primary

Sort the records of each table if the primary key exists, or the first unique key. Works when exporting MyISAM tables to InnoDB tables, but makes the export job take a long time. 

mysqldump  -uroot -p --host=localhost --all-databases --order-by-primary

--password, -p

Password to connect to the database

--pipe (available in windows system)

Connect to mysql using named pipes

mysqldump  -uroot -p --host=localhost --all-databases --pipe

--port, -P

Connection database port number

--protocol

The connection protocol used, including: tcp, socket, pipe, memory.

mysqldump  -uroot -p --host=localhost --all-databases --protocol=tcp

--quick, -q

Do not buffer queries, export directly to standard output. The default is on, use --skip-quick to cancel this option.

mysqldump  -uroot -p --host=localhost --all-databases 

mysqldump  -uroot -p --host=localhost --all-databases --skip-quick

--quote-names,-Q

Use (`) to quote table and column names. The default is on, use --skip-quote-names to cancel this option.

mysqldump  -uroot -p --host=localhost --all-databases

mysqldump  -uroot -p --host=localhost --all-databases --skip-quote-names

--replace

Use REPLACE INTO instead of INSERT INTO.

mysqldump  -uroot -p --host=localhost --all-databases --replace

--result-file,  -r

Output directly to the specified file. This option should be used on systems that use carriage-return-line-feed pairs (\\r\\n) for line breaks (eg, DOS, Windows). This option ensures that only one line is used.

mysqldump  -uroot -p --host=localhost --all-databases --result-file=/tmp/mysqldump_result_file.txt

--routines, -R

Export stored procedures and custom functions.

mysqldump  -uroot -p --host=localhost --all-databases --routines

--set-charset

Added 'SET NAMES default_character_set' to the output file. On by default, use the --skip-set-charset option to turn it off.

mysqldump  -uroot -p --host=localhost --all-databases 

mysqldump  -uroot -p --host=localhost --all-databases --skip-set-charset

--single-transaction

This option submits a BEGIN SQL statement before exporting data. BEGIN will not block any application and can guarantee the consistent state of the database when exporting. It only works with multi-version storage engines, InnoDB only. This option and the --lock-tables option are mutually exclusive, since LOCK TABLES causes any pending transactions to be implicitly committed. To export large tables, the --quick option should be used in combination.

mysqldump  -uroot -p --host=localhost --all-databases --single-transaction

--dump-date

Add export time to output file. On by default, use the --skip-dump-date option to turn it off.

mysqldump  -uroot -p --host=localhost --all-databases

mysqldump  -uroot -p --host=localhost --all-databases --skip-dump-date

--skip-opt

Disable the --opt option.

mysqldump  -uroot -p --host=localhost --all-databases --skip-opt

--socket,-S

Specify the socket file location to connect to mysql, the default path is /tmp/mysql.sock

mysqldump  -uroot -p --host=localhost --all-databases --socket=/tmp/mysqld.sock

--tab,-T

Create tab-delimited text files for each table at the given path. Note: only for mysqldump and mysqld servers running on the same machine.

mysqldump  -uroot -p --host=localhost test test --tab="/home/mysql"

--tables

Override the --databases (-B) parameter to specify the name of the table to be exported.

mysqldump  -uroot -p --host=localhost --databases test --tables test

--triggers

Export triggers. This option is enabled by default, use --skip-triggers to disable it.

mysqldump  -uroot -p --host=localhost --all-databases --triggers

--tz-utc

Set time zone TIME_ZONE='+00:00' at the top of the export to ensure the correctness of TIMESTAMP data exported in different time zones or when data is moved to other time zones.

mysqldump  -uroot -p --host=localhost --all-databases --tz-utc

--user, -u

Specifies the username for the connection.

--verbose, --v

Output various platform information.

--version, -V

Output mysqldump version information and exit

--where, -w

Dump only the records selected by the given WHERE condition. Note that if the condition contains spaces or characters that are specific to command interpreters, be sure to quote the condition.

mysqldump  -uroot -p --host=localhost --all-databases --where=” user=’root’”

--xml, -X

Export in XML format.

mysqldump  -uroot -p --host=localhost --all-databases --xml

--plugin_dir

Directory of client plugins for compatibility with different plugin versions.

mysqldump  -uroot -p --host=localhost --all-databases --plugin_dir=”/usr/local/lib/plugin”

--default_auth

Client plugins use permissions by default.

mysqldump  -uroot -p --host=localhost --all-databases --default-auth=”/usr/local/lib/plugin/”

2. Several common errors of mysqldump

2.1. Prompt command not found -bash: mysqldump: command not found

Execute the following command

mysqldump --opt -uroot -p12345678 --databases test> /data/db_bak/test223.zip

The following errors may occur

 

Solution

Environment variables that need to be imported into mysql

Query whether the imported environment variable contains mysql

echo $PATH 

Configure the mysql system environment variables in /etc/profile, usually the folder / usr / local / mysql / bin

export PATH=$PATH:/usr/local/mysql/bin
#刷新环境变量后生效
source /etc/profile

2.2、 the command line interface can be insecure

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

Reason: If the mysql version is greater than 5.6, the added security reminder mechanism will give a warning: Warning: Using a password on the command line interface can be insecure. When using the mysqldump command to back up the MySQL database,  we use the -p parameter with the Password, although it can be quickly backed up, but the password will be visible in plain text on the command line, which is easy to be stolen. In order to back up the database more securely,  versions after MySQL 5.6 will remind you to use the encryption method in the MySQL configuration file to specify the password, which can avoid the risk of password disclosure. Of course, it is just a reminder and does not affect specific implementation.

Solution:

Modify the mysql configuration file and add a username and password

vim /etc/my.cnf

Content reference is as follows

[mysqldump]
...
user=root
password="你的mysql密码"


Then restart the mysql database, you don’t need to enter the user name and password when executing the mysqldump command

mysqldump --opt  --databases test> /data/db_bak/test223.zip

2.3、Gotpacket bigger than ‘max_allowed_packet‘ bytes

Everyone performs MySQL database backup mysqldump error Error 2020: Got packet bigger than 'max_allowed_packet' bytes

For example, execute the following command:

mysqldump --opt -uroot -p12345678 --databases test> /data/db_bak/test223.zip

The following error may occur

 

Solution: increase the max_allowed_packet parameter

mysqldump --opt -uroot -p12345678 --max_allowed_packet=512M --databases test> /data/db_bak/test223.zip

  • Several commonly used backup commands

#备份服务器所有的数据库包含数据
mysqldump -uroot -p12345678 -A >/data/db_bak/2023_mysql_all_bak.sql

#备份服务器所有的数据库仅包含表结构
mysqldump -uroot -p123456 -A-d>/data/db_bak/2023bak.sql

#备份服务器所有的数据库仅包含表数据
mysqldump -uroot -p123456 -A-t>/data/db_bak/2023DataBak.sql

#备份单个数据库的数据和结构
mysqldump -uroot-p123456 test>/data/db_bak/test_bak.sql

#备份单个数据库多个表的数据和结构
mysqldump -uroot -p123456 test tb1 tb2>f:\test_table.sql

Guess you like

Origin blog.csdn.net/xishining/article/details/131200240