当前位置:网站首页>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
边栏推荐
- [experience sharing] summary of database operations commonly used in Django development
- 如何设计数据可视化平台
- Hubble database x a joint-stock commercial bank: upgrade the number management system of Guanzi, so that every RMB has an "ID card"
- Vscode shortcut key
- 几百行代码实现一个 JSON 解析器
- Introduction to PWN (1) binary Basics
- Operation and maintenance thinking | do you know the relationship between CMDB and monitoring?
- 锐捷交换机配置ssh password登录命令[通俗易懂]
- Why do more and more users give up swagger and choose apifox
- NPOI导出Excel并下载到客户端
猜你喜欢
RSLO:自监督激光雷达里程计(实时+高精度,ICRA2022)
From PDB source code to frame frame object
设计人工智能产品:技术可能性、用户合意性、商业可行性
做一个墨水屏电子钟,炫酷!
你的代码会说话吗?(上)
开源社邀您参加OpenInfra Days China 2022,议题征集进行中~
Design artificial intelligence products: technical possibility, user acceptability and commercial feasibility
抢做意大利岛主?刘强东两月套现66亿 疑一次性5.6亿“紧急转账”急购欧洲海上皇宫
CVPR再起争议:IBM中稿论文被指照搬自己承办竞赛第二名的idea
PostgreSQL surpasses MySQL
随机推荐
你的代码会说话吗?(上)
iNFTnews | 科技巨头加快进军Web3和元宇宙
Hematemesis recommends 17 "wheels" to improve development efficiency
Stackoverflow 2022 database annual survey
Is it safe for Huatai Securities to open an account? Is there any risk in opening an account
Luogu_ P1303 A*B Problem_ High precision calculation
Kubernetes 深入理解Kubernetes(二) 声明组织对象
2.01 backpack problem
2021计算机三级数据库大题总结
Recognize the startup function and find the user entry
How vscade sets auto save code
How to solve the data inconsistency between redis and MySQL?
2022年中国运维安全产品市场规模及发展趋势预测分析
Design artificial intelligence products: technical possibility, user acceptability and commercial feasibility
php获取数字的个位数并替换为指定的尾数
腾讯云国际云服务器登录之后没有网络,如何排查?
Hubble数据库x某股份制商业银行:冠字号码管理系统升级,让每一张人民币都有 “身份证”
RSLO:自监督激光雷达里程计(实时+高精度,ICRA2022)
Mobile web training -flex layout test question 1
《畅玩NAS》家庭 NAS 服务器搭建方案「建议收藏」