MySQL database backup and recovery (4)-detailed explanation of mysqldump parameters

MySQL database backup and recovery (4)-detailed explanation of mysqldump parameters

mysqldump is a logical backup command that comes with MySQL. The backup file contains a set of SQL statements, which can be executed to generate database object definitions and table data before backup. The mysqldump command can also generate output in CSV, other delimited text or XML format.

The format of the mysqldump command is as follows:

mysqldump [options] [db_name [tbl_name ...]]

Commonly used [options] parameters are as follows:

1. Specify the login user name

-uuser_name
--user=user_name

Description: Specify the username used by MySQL when connecting to the server.

2. Specify the login host

--host = host_name
-h host_name

Note: This parameter can be omitted, the default host is localhost.

3. Specify login password

-pyour_pass
--password[=your_pass]

Description: The password used when connecting to the server.

4. Specify the port number used to connect to the host

-P port_num
--port=port_num

Description: Specify the TCP/IP port number used when connecting to the host (used to connect to a host other than localhost).

5. Back up all databases

--all-databases
-A

Note: The same as using the -databases option and naming all databases.

6. Back up the specified database

--databases db_name1 db_name2 ...
-B db_name1 db_name2 ...

Explanation:
(1) If this option is omitted, mysqldump treats the first name parameter as the database name, and the subsequent names as the table name, and includes CREATE DATABASE and USE statements.
(2) Use this option to treat all name parameters as database names, and the CREATE DATABASE and USE statements are included in the output before each new database.
(3) This option can be used to back up the INFORMATION_SCHEMA and performace_schema databases, these two databases will not be backed up even if the –all-databases option is used.

7, only backup table structure

-d
--no-data   

Note: Only the table structure is exported, not the data in the table.

8. Back up only the data in the table

-t
--no-create-info

9. Refresh the log file before exporting (generate a new binlog file)

-F
--flush-logs   

Note: After exporting the data, a new binlog file is generated to facilitate data recovery.

10. Export records that meet specified conditions

-w
--where='where-condition'   

Description:
(1) Export records that meet the specified query conditions.
(2) If the condition contains single quotes, the parameter can use double quotes, such as:

[root@Mysql11 tmp]# mysqldump -uroot -p123456 hist stu --where="name='jack'" > /tmp/stu_name.sql;
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@Mysql11 tmp]# cat /tmp/stu_name.sql 
..........
--
-- Table structure for table `stu`
--

DROP TABLE IF EXISTS `stu`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `stu` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` char(20) DEFAULT NULL,
  `age` int(11) DEFAULT NULL,
  `address` varchar(20) DEFAULT NULL,
  `phone` char(11) DEFAULT NULL,
  `dept_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `stu`
--
-- WHERE:  name='jack'

LOCK TABLES `stu` WRITE;
/*!40000 ALTER TABLE `stu` DISABLE KEYS */;
INSERT INTO `stu` VALUES (3,'jack',20,'Zhengzhou','13675871454',1);
/*!40000 ALTER TABLE `stu` ENABLE KEYS */;
UNLOCK TABLES;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

..............

-- Dump completed on 2020-07-02 12:16:55

11. The export file format is a text file

-T
--tab=path-to-some-directory   

Description: For each given table, create a table_name.sql file (containing SQL CREATE command) and a table_name.txt file (containing data).

12. Specify the format of the exported text file

--fields-enclosed-by=...   
--fields-optionally-enclosed-by=...   
--fields-escaped-by=...   
--fields-terminated-by=...

Note: These options are used with the -T option and have the same meaning as the corresponding LOAD DATA INFILE clause.

E.g:

[root@Mysql11 tmp]# mysqldump -uroot -p123456 hist stu --tab='/tmp' --fields-terminated-by=',' 
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@Mysql11 tmp]# ls /tmp
stu.sql  stu.txt
[root@Mysql11 tmp]# cat /tmp/stu.txt
1,zhangsan,20,Xinxiang,15578941258,1
2,tom,20,Xinxiang,13778942222,1
3,jack,20,Zhengzhou,13675871454,1
4,john,21,Zhengzhou,13937681111,2
5,mark,22,Aanyang,13055882233,2

13. Ensure the consistency of exported data

--single-transaction

Description:
(1) This parameter is suitable for the database backup of the innoDB engine;
(2) When the innoDB table is backed up, the option –single-transaction is usually enabled to ensure the consistency of the backup. In fact, its working principle is to set this session The isolation level is: repeatable read to ensure that when this session is backed up, you will not see the data that has been submitted by other sessions.

14. Specify the character set of the exported file

--default-character-set=name

E.g:

[root@Mysql11 tmp]# mysqldump -uroot -p123456 --databases hist --default-character-set=utf8 > /tmp/hist.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.

15. Append the location and file name of the current server binlog to the output file

--master-data=[1,2]

Description: This parameter records the binlog of the current server, which is equivalent to executing show master status, the value of status (file, position).

E.g:

[root@Mysql11 tmp]# mysqldump -uroot -p123456 --master-data=1 hist > /tmp/hist.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.

[root@Mysql11 tmp]# cat /tmp/hist.sql
.............

CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000002', MASTER_LOG_POS=154;
--
-- Table structure for table `course`
--
......................

16. Append the binlog location and file name of the main library to the exported data file

--dump-slave=[1,2]

Description:
(1) If the current server is a slave server, use this command to execute stop slave to obtain the master binlog file and location, and after the backup is completed, it will automatically execute start slave to start the slave server. Use –dump-slave to get only the position of the master binglog where the current data from the slave server is executed, namely: relay_mater_log_file, exec_master_log_pos, not the current binlog execution position of the master server, which depends on the data delay of the master and slave.
(2) This parameter is executed on the slave server, which is equivalent to executing show slave status.
(3) When it is set to 1, it will be output to the data file with the CHANGE MASTER command, and when it is set to 2, a comment will be added before change.

Guess you like

Origin blog.csdn.net/weixin_44377973/article/details/107081758