当前位置:网站首页>How to back up MySQL_ The most complete MySQL backup method in history
How to back up MySQL_ The most complete MySQL backup method in history
2022-06-28 13:44:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm your friend, Quan Jun .
I have
The backup methods used are :mysqldump、mysqlhotcopy、BACKUP TABLE 、SELECT INTO
OUTFILE, Or back up binary logs (binlog), You can also copy data files and related configuration files directly .MyISAM
Tables are saved as files , Therefore, it is relatively easy to back up , Several methods mentioned above can be used .Innodb All tables are stored in the same data file ibdata1
in ( It could be multiple files , Or a separate tablespace file ), Relatively bad backup , The free solution can be to copy data files 、 Backup binlog, Or use
mysqldump.
1.mysqldump Backup
mysqldump Is to use SQL Level backup mechanism , It leads the data table to SQL Script files , In different MySQL The upgrade between versions is relatively appropriate , This is also the most common backup method .
Example :mysqldump -uroot -p database table > /home/jobs/back.sql
mysqldump It can also be used for incremental backup ,mysqldump There are many related parameters online , Let's not repeat them here
2.mysqlhotcopy Backup
mysqlhotcopy It's a PERL Program . It USES LOCK TABLES、FLUSH
TABLES and cp or scp
To quickly back up the database . It's the fastest way to back up a database or a single table , But it can only run in database files ( Include data table definition file 、 Data files 、 Index file ) On the same machine .
mysqlhotcopy Can only be used for backup MyISAM, And it can only run in class Unix and NetWare On the system .
mysqlhotcopy It supports copying multiple databases at one time , It also supports regular expressions .
Example : root#/usr/local/mysql/bin/mysqlhotcopy -h=localhost -u=root
-p=123456 database /tmp ( Put the database directory database copy to /tmp
Next )root#/usr/local/mysql/bin/mysqlhotcopy -h=localhost -u=root -p=123456
db_name_1 … db_name_n /tmproot#/usr/local/mysql/bin/mysqlhotcopy
-h=localhost -u=root -p=123456 db_name./regex/
/tmp Please refer to the manual for more detailed usage , Or call the following command to view mysqlhotcopy With the help of the :
perldoc /usr/local/mysql/bin/mysqlhotcopy Be careful , Want to use mysqlhotcopy, There must be
SELECT、RELOAD( To execute FLUSH TABLES) jurisdiction , And must be able to read datadir/db_name Directory permissions .
Restore mysqlhotcopy The whole database directory is backed up , It can be directly copied to mysqld
designated datadir ( Here it is. /usr/local/mysql/data/) Under the directory , At the same time, attention should be paid to the issue of permissions , Here's an example : root#cp
-rf db_name /usr/local/mysql/data/root#chown -R nobody:nobody
/usr/local/mysql/data/ ( take db_name The owner of the directory is changed to mysqld Run the user )
3.SQL Syntax backup
3.1 Backup BACKUP TABLE Grammar is actually the same as mysqlhotcopy
It works pretty much the same way , All locked watches , Then copy the data file . It enables online backup , But the effect is not ideal , Therefore, it is not recommended . It only copies table structure files and data files , Do not copy the citation at the same time
Pieces of , Therefore, the recovery time is relatively slow . Example : BACK TABLE tbl_name TO ‘/tmp/db_name/’; Be careful , There must be FILE
Permission to execute this SQL, And the directory /tmp/db_name/ Must be able to be mysqld Users can write , The exported file cannot overwrite the existing file , To avoid safety issues .
Recovery BACKUP TABLE Method to back up the file , Can run RESTORE TABLE Statement to restore the data table . Example : RESTORE TABLE FROM ‘/tmp/db_name/’; Permission requirements are similar to those described above .
3.2 SELECT INTO OUTFILE Is to export the data into a common text file , You can customize how fields are spaced , It is convenient to process these data . Example :
SELECT INTO OUTFILE ‘/tmp/db_name/tbl_name.txt’ FROM tbl_name; Be careful , There must be
FILE Permission to execute this SQL, And the documents /tmp/db_name/tbl_name.txt Must be able to be mysqld
Users can write , The exported file cannot overwrite the existing file , To avoid safety issues .
use SELECT INTO OUTFILE Method to back up the file , Can run LOAD DATA INFILE Statement to restore the data table . Example : LOAD
DATA INFILE ‘/tmp/db_name/tbl_name.txt’ INTO TABLE
tbl_name; Permission requirements are similar to those described above . Before pouring data , The data table must already exist . If you worry about data duplication , Can increase REPLACE
Keyword to replace existing records or use IGNORE Keywords to ignore them .
4. Enable binary logging (binlog)
use binlog The method is relatively more flexible , Save the mind and save labour , It can also support incremental backup .
Enable binlog It must be restarted mysqld. First , close mysqld, open my.cnf, Add the following lines :
server-id = 1
log-bin = binlog
log-bin-index = binlog.index
Then start mysqld That's all right. . During operation, it will produce binlog.000001 as well as binlog.index, The previous file is mysqld
Record all updates to the data , The following file is all binlog The index of , Can not be easily deleted . About binlog Please refer to the manual for more information .
When backup is required , You can do it first SQL sentence , Give Way mysqld Terminate the current binlog
Writing , You can back up the files directly , In this way, the purpose of incremental backup can be achieved : FLUSH LOGS; If it is a slave server in the backup replication system , You should also back up
master.info and relay-log.info file .
Back up binlog Documents available MySQL Tools provided mysqlbinlog Check it out. , Such as :
/usr/local/mysql/bin/mysqlbinlog /tmp/binlog.000001 This tool allows you to display all the data under the specified database
SQL sentence , And it can also limit the time range , Quite handy , Please refer to the manual for details .
When you recover , You can do this with statements like the following : /usr/local/mysql/bin/mysqlbinlog /tmp/binlog.000001
| mysql -uyejr -pyejr db_name hold mysqlbinlog Output SQL Statement executes it directly as input .
If you have a free machine , This is a good way to back up . As a result of slave The performance requirements of the machine are not so high , So the cost is low , Incremental backup can be realized at a low cost, and some data query pressure can be shared , Why not do it ?
5. Copy files
Compared with the previous methods, the direct backup of data files , Backing up data files is the most direct 、 Fast 、 convenient , The disadvantage is that incremental backup is basically impossible .
To ensure data consistency , Need to be in front of the backrest file , Perform the following SQL sentence : FLUSH TABLES WITH READ
LOCK; That is to refresh the data in the memory to the disk , Lock the data table at the same time , To ensure that no new data is written during the copy process . The recovery of data backed up by this method is also very simple , Copy directly back to
Under the original database directory .
Be careful , about Innodb Type table , You also need to back up its log files , namely ib_logfile* file . Because when Innodb When the watch is damaged , You can rely on these log files to recover .
6. utilize rsync Backup
rsync As a synchronization tool, it can also be used for backup , But you need to configure the server side and the client side
Example rsync -vzrtopg –progress –delete [email protected]::root /tmp/
The disadvantage is that rsync It is an incremental backup based on the file modification time , Therefore, all backup databases are fully backed up , And the configuration is troublesome .
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/150622.html Link to the original text :https://javaforall.cn
边栏推荐
- Build a learning environment
- BERT为何无法彻底干掉BM25??
- Successful cases of rights protection of open source projects: successful rights protection of SPuG open source operation and maintenance platform
- NFT digital collection system development (3D modeling economic model development case)
- 30 sets of JSP website source code collection "suggestions collection"
- The difference between align items and align content
- 木兰开放作品许可证1.0面向社会公开征求意见
- How to open an account on the flush? Is it safe
- Prediction of red wine quality by decision tree
- (原创)【MAUI】一步一步实现“悬浮操作按钮”(FAB,Floating Action Button)
猜你喜欢

Oracle 云基础设施扩展分布式云服务,为组织提供更高的灵活性和可控性

腾讯云国际云服务器登录之后没有网络,如何排查?

药物发现新方法,阿斯利康团队通过课程学习改进从头分子设计

Stackoverflow 2022 database annual survey

Fs7022 scheme series fs4059a dual two lithium battery series charging IC and protection IC

How fragrant! The most complete list of common shortcut keys for pychar!

Hematemesis recommends 17 "wheels" to improve development efficiency

(原创)【MAUI】一步一步实现“悬浮操作按钮”(FAB,Floating Action Button)

中国广电5G套餐来了,比三大运营商低,却没预期那么低

Solution to directory access of thinkphp6 multi-level controller
随机推荐
First knowledge of exception
Is it safe for Huatai Securities to open an account? Is there any risk in opening an account
Rk3399 platform development series explanation (use part) pinctrl subsystem introduction - Video Introduction
G : 最大流问题
The difference between align items and align content
我呕血收集融合了来自各路经典shell书籍的脚本教学,作为小白的你快点来吧
正则匹配数字,英文以及英文符号
En parlant d'exception - que se passe - t - il lorsque l'exception est lancée?
Vscode shortcut key
Complete backpack beginner chapter "suggestions collection"
How about stock online account opening and account opening process? Is it safe to open a mobile account?
Latest summary! 30 provinces announce 2022 college entrance examination scores
Explanation of sprintf function in C language
yii2编写swoole的websocket服务
The company leader said that if the personal code exceeds 10 bugs, he will be dismissed. What is the experience?
How vscade sets auto save code
PCB understand Wang, are you? I am not
Mobile web training -flex layout test question 1
(original) [Maui] realize "floating action button" step by step
Mobile web training day-2