mysqldump备份mysql数据库

文章详细介绍了使用mysqldump工具进行数据库的导出操作,包括导出表结构、数据以及单表信息。同时,提到了select语句导出数据的方法和注意事项,如secure_file_priv的配置。在数据导入方面,列出了LOADDATA、mysql命令及INSERTINTO等不同方式,并且讨论了mysqldump的各种参数选项,用于控制导出的详细程度和兼容性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

       数据库数据的备份是保障生产的重要一环,mysql自带的备份工具mysqldump可以满足大多数场景的数据备份,当然市场上也有很多优秀的数据库备份工具,如Xtrabackup、MyDumper等。

一、mysqldump导出数据

1、导出数据库表结构

 mysqldump  -uroot -proot  -d  test_db >  /home/test_db.sql

2、到出数据库表数据

mysqldump  -uroot -proot  -t   test_db >  /home/test_db.sql

3、导出数据库表结构和数据

 mysqldump  -uroot -proot   test_db >  /home/test_db.sql

4、导出单表结构

mysqldump -uroot -proot --set-gtid-purged=OFF -d test_db  t_user>'/home/t_user.sql'

5、导出单表数据

mysqldump  -uroot -proot  -t  test_db  t_user>/home/t_user.sql

6、导出单表结构和数据

mysqldump -uroot -proot --set-gtid-purged=OFF test_db  t_user>'/home/sys_user.sql'

、select导出数据

  1、secure_file_priv

   查看secure_file_priv是否允许把数据导出到任何目录

SHOW  VARIABLES  LIKE	  '%secure_file_priv%';

    如果为NULL,则在my.cnf配置文件中配置想要导出的的目录即可

  2、导出表数据


 SELECT  * FROM  t_user INTO OUTFILE '/var/lib/mysql-files/t_user.sql';

 导入数据

1、方式一:load data

LOAD DATA LOCAL INFILE '/var/lib/mysql-files/t_user.sql' INTO TABLE t_user  LINES TERMINATED BY '\r\n';

2、方式二:mysql   

   导入数据库数据

mysql -uroot -p test_db  < t_user.sql

   导入单表数据

mysql  -uroot  -p  test_db

source  sys_user.sql

3、方式三:insert into 

INSERT  INTO   t_user_back(id,name)  SELECT  id,name  FROM  t_user;

四、mysqldump参数

Option NameDescriptionIntroducedDeprecated
--add-drop-databaseAdd DROP DATABASE statement before each CREATE DATABASE statement
--add-drop-tableAdd DROP TABLE statement before each CREATE TABLE statement
--add-drop-triggerAdd DROP TRIGGER statement before each CREATE TRIGGER statement
--add-locksSurround each table dump with LOCK TABLES and UNLOCK TABLES statements
--all-databasesDump all tables in all databases
--allow-keywordsAllow creation of column names that are keywords
--apply-replica-statementsInclude STOP REPLICA prior to CHANGE REPLICATION SOURCE TO statement and START REPLICA at end of output8.0.26
--apply-slave-statementsInclude STOP SLAVE prior to CHANGE MASTER statement and START SLAVE at end of output8.0.26
--bind-addressUse specified network interface to connect to MySQL Server
--character-sets-dirDirectory where character sets are installed
--column-statisticsWrite ANALYZE TABLE statements to generate statistics histograms
--commentsAdd comments to dump file
--compactProduce more compact output
--compatibleProduce output that is more compatible with other database systems or with older MySQL servers
--complete-insertUse complete INSERT statements that include column names
--compressCompress all information sent between client and server8.0.18
--compression-algorithmsPermitted compression algorithms for connections to server8.0.18
--create-optionsInclude all MySQL-specific table options in CREATE TABLE statements
--databasesInterpret all name arguments as database names
--debugWrite debugging log
--debug-checkPrint debugging information when program exits
--debug-infoPrint debugging information, memory, and CPU statistics when program exits
--default-authAuthentication plugin to use
--default-character-setSpecify default character set
--defaults-extra-fileRead named option file in addition to usual option files
--defaults-fileRead only named option file
--defaults-group-suffixOption group suffix value
--delete-master-logsOn a replication source server, delete the binary logs after performing the dump operation8.0.26
--delete-source-logsOn a replication source server, delete the binary logs after performing the dump operation8.0.26
--disable-keysFor each table, surround INSERT statements with statements to disable and enable keys
--dump-dateInclude dump date as "Dump completed on" comment if --comments is given
--dump-replicaInclude CHANGE REPLICATION SOURCE TO statement that lists binary log coordinates of replica's source8.0.26
--dump-slaveInclude CHANGE MASTER statement that lists binary log coordinates of replica's source8.0.26
--enable-cleartext-pluginEnable cleartext authentication plugin
--eventsDump events from dumped databases
--extended-insertUse multiple-row INSERT syntax
--fields-enclosed-byThis option is used with the --tab option and has the same meaning as the corresponding clause for LOAD DATA
--fields-escaped-byThis option is used with the --tab option and has the same meaning as the corresponding clause for LOAD DATA
--fields-optionally-enclosed-byThis option is used with the --tab option and has the same meaning as the corresponding clause for LOAD DATA
--fields-terminated-byThis option is used with the --tab option and has the same meaning as the corresponding clause for LOAD DATA
--flush-logsFlush MySQL server log files before starting dump
--flush-privilegesEmit a FLUSH PRIVILEGES statement after dumping mysql database
--forceContinue even if an SQL error occurs during a table dump
--get-server-public-keyRequest RSA public key from server
--helpDisplay help message and exit
--hex-blobDump binary columns using hexadecimal notation
--hostHost on which MySQL server is located
--ignore-errorIgnore specified errors
--ignore-tableDo not dump given table
--include-master-host-portInclude MASTER_HOST/MASTER_PORT options in CHANGE MASTER statement produced with --dump-slave8.0.26
--include-source-host-portInclude SOURCE_HOST and SOURCE_PORT options in CHANGE REPLICATION SOURCE TO statement produced with --dump-replica8.0.26
--insert-ignoreWrite INSERT IGNORE rather than INSERT statements
--lines-terminated-byThis option is used with the --tab option and has the same meaning as the corresponding clause for LOAD DATA
--lock-all-tablesLock all tables across all databases
--lock-tablesLock all tables before dumping them
--log-errorAppend warnings and errors to named file
--login-pathRead login path options from .mylogin.cnf
--master-dataWrite the binary log file name and position to the output8.0.26
--max-allowed-packetMaximum packet length to send to or receive from server
--mysqld-long-query-timeSession value for slow query threshold8.0.30
--net-buffer-lengthBuffer size for TCP/IP and socket communication
--network-timeoutIncrease network timeouts to permit larger table dumps
--no-autocommitEnclose the INSERT statements for each dumped table within SET autocommit = 0 and COMMIT statements
--no-create-dbDo not write CREATE DATABASE statements
--no-create-infoDo not write CREATE TABLE statements that re-create each dumped table
--no-dataDo not dump table contents
--no-defaultsRead no option files
--no-set-namesSame as --skip-set-charset
--no-tablespacesDo not write any CREATE LOGFILE GROUP or CREATE TABLESPACE statements in output
--optShorthand for --add-drop-table --add-locks --create-options --disable-keys --extended-insert --lock-tables --quick --set-charset
--order-by-primaryDump each table's rows sorted by its primary key, or by its first unique index
--passwordPassword to use when connecting to server
--password1First multifactor authentication password to use when connecting to server8.0.27
--password2Second multifactor authentication password to use when connecting to server8.0.27
--password3Third multifactor authentication password to use when connecting to server8.0.27
--pipeConnect to server using named pipe (Windows only)
--plugin-authentication-kerberos-client-modePermit GSSAPI pluggable authentication through the MIT Kerberos library on Windows8.0.32
--plugin-dirDirectory where plugins are installed
--portTCP/IP port number for connection
--print-defaultsPrint default options
--protocolTransport protocol to use
--quickRetrieve rows for a table from the server a row at a time
--quote-namesQuote identifiers within backtick characters
--replaceWrite REPLACE statements rather than INSERT statements
--result-fileDirect output to a given file
--routinesDump stored routines (procedures and functions) from dumped databases
--server-public-key-pathPath name to file containing RSA public key
--set-charsetAdd SET NAMES default_character_set to output
--set-gtid-purgedWhether to add SET @@GLOBAL.GTID_PURGED to output
--shared-memory-base-nameShared-memory name for shared-memory connections (Windows only)
--show-create-skip-secondary-engineExclude SECONDARY ENGINE clause from CREATE TABLE statements8.0.18
--single-transactionIssue a BEGIN SQL statement before dumping data from server
--skip-add-drop-tableDo not add a DROP TABLE statement before each CREATE TABLE statement
--skip-add-locksDo not add locks
--skip-commentsDo not add comments to dump file
--skip-compactDo not produce more compact output
--skip-disable-keysDo not disable keys
--skip-extended-insertTurn off extended-insert
--skip-generated-invisible-primary-keyDo not include generated invisible primary keys in dump file8.0.30
--skip-optTurn off options set by --opt
--skip-quickDo not retrieve rows for a table from the server a row at a time
--skip-quote-namesDo not quote identifiers
--skip-set-charsetDo not write SET NAMES statement
--skip-triggersDo not dump triggers
--skip-tz-utcTurn off tz-utc
--socketUnix socket file