当前位置:网站首页>Backup method of mysqldump

Backup method of mysqldump

2022-06-24 04:05:00 User 8989785

mysqldump yes MySQL The logic backup tool of the system , It is mainly used to dump the database . It mainly produces a series of SQL sentence , Can be encapsulated into a file , This file contains the SQL Orders such as CREATE DATABASE ,CREATE TABLE ,INSERT wait . When we need to restore this data , Just execute this file , The corresponding data can be restored .

mysqldump The basic syntax is as follows :

Usage: mysqldump [OPTIONS] database [tables] 
OR     mysqldump [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...] 
OR     mysqldump [OPTIONS] --all-databases [OPTIONS] 

perform mysqldump --help Or reference MySQL Official documents , We found that mysqldump There are many parameters that the tool can configure , The following is a brief description of some commonly used parameters .

The table above shows some common mysqldump Related options , When you don't understand what a parameter does , It can be executed mysqldump --help To get help . For boolean type parameters , In general, there is an opposite parameter , Such as --triggers Default on , have access to --skip-triggers To disable it .

A few backup tips

although mysqldump It's not suitable for backup with large amount of data , But because of its flexibility and convenience 、 Parameters can be customized according to the scene , Or is it widely used in the field of data export .

According to my own experience , Let's share a few points mysqldump Backup tips :

  • It is recommended to use --single-transaction Parameter to get a consistent backup , Reduce the lock .
  • Export on demand , As long as you want the data , Minimize the export file size .
  • If you want to build a slave Library , It is recommended to use --master-data = 2 Parameter record main library binlog Information .
  • If you want to back up stored procedures 、 Custom functions and events , Please add -R -E Parameters , They are not turned on by default .
  • Don't add parameters you don't know , By default .

Let's share a few different scenarios mysqldump Usage method :

#  Back up all databases ( Contains stored procedures 、 Custom functions and events ) 
mysqldump -uroot -pxxxxxx --single-transaction -R -E --all-databases > /tmp/all_database.sql 
 
#  Record required  binlog  Site information   It can be used to build slave libraries  
mysqldump -uroot -pxxxxxx --single-transaction -R -E --all-databases --master-data=2 > /tmp/all_database.sql 
 
#  Back up the specified database  
mysqldump -uroot -pxxxxxx --single-transaction -R -E --databases db1 > /tmp/db1.sql 
mysqldump -uroot -pxxxxxx --single-transaction -R -E --databases db1 db2 > /tmp/db1_db2.sql 
 
#  Back up some tables  
mysqldump -uroot -pxxxxxx --single-transaction db1 tb1 > /tmp/tb1.sql 
mysqldump -uroot -pxxxxxx --single-transaction db1 tb1 tb2 tb3 > /tmp/tb.sql 
 
#  Export a table , The data is in a single line insert Show  
mysqldump -uroot -pxxxxxx --single-transaction --skip-extended-insert db1 tb1 > /tmp/tb1.sql 
 
#  Export part of the data of a single table  
mysqldump -uroot -pxxxxxx --single-transaction db1 tb1 --where=" create_time >= '2021-06-01 00:00:00' " > /tmp/tb1.sql 
mysqldump -uroot -pxxxxxx --single-transaction db1 tb1 --where='id < 10' > /tmp/tb1.sql 
 
#  Exclude some table exports  
mysqldump -uroot -pxxxxxx --single-transaction --databases db1 --ignore-table=db1.tb1 --ignore-table=db1.tb2 > /tmp/db1.sql 
 
#  Export only structure or data  
mysqldump -uroot -pxxxxxx db1 --no-data > /tmp/db1_jiegou.sql 
mysqldump -uroot -pxxxxxx db1 --no-create-info > /tmp/db1_data.sql 
 
#  Only export stored procedures and custom functions of a library  
mysqldump -uroot -pxxxxxx -d -t -R db1 > /tmp/db1_routine.sql 
 
#  Remote export   namely MySQL The server is not local  
mysqldump -uroot -pxxxxxx -hxxx.xxx.xx -P3306 --single-transaction --databases db1 > /tmp/db1.sql 
原网站

版权声明
本文为[User 8989785]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/09/20210913131307326b.html