当前位置:网站首页>Three methods of MySQL backup
Three methods of MySQL backup
2022-07-02 18:08:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm your friend, Quan Jun .
Catalog
Two 、 Backup issues to consider
3、 ... and 、 The type of backup
5、 ... and 、 Implementation of backup and recovery
1、 utilize select into outfile Realize data backup and restore
2、 utilize mysqldump Tools backup and restore data
3、 utilize lvm Snapshot realizes almost hot standby data backup and recovery
4、 be based on Xtrabackup Backup and restore
The essence of backup is to save a data set as a copy , But the original data will keep changing , Therefore, using backup can only restore the data before the data changes . After the change ? So it's important to make a good backup strategy .
One 、 Purpose of backup
Doing disaster recovery : Recover and restore the damaged data
Demand changes : Due to the change of demand, the data needs to be restored before the change
test : Test whether the new function is available
Two 、 Backup issues to consider
How long data loss can be tolerated ;
How long does it take to recover data ;
Whether it is necessary to provide continuous services during recovery ;
The object of recovery , It's the whole library , Multiple tables , Or a single library , A single watch .
3、 ... and 、 The type of backup
1、 Depending on whether the database needs to be offline
Cold standby (cold backup): It needs to be closed mysql service , Read and write requests are not allowed ;
Warm preparation (warm backup): Service online , But only read requests are supported , Write request is not allowed ;
Hot standby (hot backup): While backing up , Business is not affected .
notes : 1、 This type of backup , It depends on the needs of the business , Not backup tools 2、MyISAM Hot standby is not supported ,InnoDB Support hot standby , But special tools are needed
2、 Depending on the range of data sets to be backed up
Full backup :full backup, Back up all character sets .
Incremental backup : incremental backup Data that has changed since the last full or incremental backup , Not to be used alone , With full backup , The frequency of backups depends on how often the data is updated .
Differential backup :differential backup Data changed since last full backup .
Recommended recovery strategy :
Completely + The incremental + Binary log
Completely + differences + Binary log
3、 According to the backup data or files
The physical backup : Direct backup of data files
advantage :
Backup and recovery operations are relatively simple , Be able to cross mysql Version of ,
Fast recovery , Belongs to the file system level
Suggest :
Don't assume that backups are always available , To test
mysql>check tables; Check whether the table is available
Logical backup : Back up the data and code in the table
advantage :
It's easy to recover 、
The result of the backup is ASCII file , Can edit
It's not about the storage engine
It can be backed up and restored through the network
shortcoming :
Backup or recovery requires mysql Server processes participate in
Backup results take up more space ,
Floating point numbers may lose precision
After restoration , The miniature needs to be rebuilt
Four 、 Backup objects
1、 data ;
2、 The configuration file ;
3、 Code : stored procedure 、 Storage function 、 trigger
4、os Related configuration files
5、 Copy related configuration
6、 Binary log
5、 ... and 、 Implementation of backup and recovery
1、 utilize select into outfile Realize data backup and restore
1.1 Back up the data that needs to be backed up
mysql> use hellodb; // open hellodb library
mysql> select * from students; see students Properties of
mysql> select * from students where Age > 30 into outfile ‘/tmp/stud.txt’ ; // Back up the information of students older than 30
Be careful :
The directory path of the backup must allow the current operation mysql Users of the server mysql Have access rights
After the backup is completed, you need to remove the backed up files from tmp Copy the directory , Otherwise, the purpose of backup will be lost
go back to tmp Check the file just backed up under the directory
[[email protected] ~]# cd /tmp
[[email protected] tmp]# cat stud.txt
3Xie Yanke53M216
4Ding Dian32M44
6Shi Qing46M5\N
13Tian Boguang33M2\N
25Sun Dasheng100M\N\N
[[email protected] tmp]#
You will find it is a text file . So you can't import the database directly . Need to use load data infile recovery
go back to mysql Server side , Delete older than 30 Users of , Simulation data is destroyed
mysql> delete from students where Age > 30;
mysql> load data infile ‘/tmp/stud.txt’ into table students;
2、 utilize mysqldump Tools backup and restore data
mysqldump Often used for warm preparation , So we first need to apply a read lock to the data we want to back up ,
2.1 Apply read lock :
1. Add options directly during backup
–lock-all-tables It is to apply read locks to all tables of the database to be backed up
–lock-table Apply read lock to single table only , Even backing up the entire database , It also imposes a read lock on a table when we back it up , Therefore, it is suitable for backing up a single table
2、 Write commands on the server ,
mysql> flush tables with read lock; Apply lock , Means to synchronize all the tables in memory to disk , Then apply read lock
mysql> flush tables with read lock; Release read lock
But that's right. InnoDB In terms of storage engine , Although you can also request Dao read lock , But it doesn't mean that all its data has been synchronized to the disk , So when faced with InnoDB When , We're going to use mysql> show engine innodb status; have a look InnoDB All the data has been synchronized to the disk , Before the backup operation .
2.2 Backup strategy :
Full backup + Incremental backup + Binary log
Demonstrate the process of backup ;
2.3 First make a full backup of the database :
[[email protected] ~]# mysqldump -uroot --single-transaction --master-data=2 --databases hellodb > /backup/hellodb_`date +%F`.sql
--single-transaction: Based on this option, hot standby can be realized InnoDB surface ; therefore , You don't need to use it at the same time --lock-all-tables;
--master-data=2 Record the location of the binary log at the time of backup , And comment out ,1 It is not annotated
--databases hellodb Specify the database to back up
Then back mysql Server side ,
2.4 go back to mysql Update data on the server
mysql> create table tb1(id int); Create table
mysql> insert into tb1 values (1),(2),(3); insert data , This is just a demonstration , Casually inserted a few data
2.5 First check the location recorded in the full backup file :
[[email protected] backup]# cat hellodb_2013-09-08.sql | less
-- CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000013', MASTER_LOG_POS=15684; The location of the binary log is recorded
2.6 Back to the server :
mysql> show master status; Display the location of the binary log at this time
From the location recorded in the backup file to our current location , That is, the incremental part
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000004 | 15982 | | |
+------------------+----------+--------------+------------------+
2.7 Make incremental backups
[[email protected] backup]# mysqlbinlog --start-position=15694 --stop-position=15982
/mydata/data/mysql-bin.000013 > /backup/hellodb_`date +$F_%H`.sql
2.8 Back to the server
mysql> insert into tb1 values (4),(5); Insert some values
mysql> drop database hellodb; Delete hellodb library
2.9 Export the binary log this time :
[[email protected] backup]# mysqlbinlog --start-position=15982 /mydata/data/mysql-bin.000013 View the location of the binary log when deleting
[[email protected] backup]# mysqlbinlog --start-position=15982 --stop-position=16176 /mydata/data/mysql-bin.000013 > /tmp/hellodb.sql // Export binary log
2.10 First, let mysql offline
Back to the server :
mysql> set sql_log_bin=0; Close binary log
mysql> flush logs; Scroll down the log
2.11 Simulation database corruption
mysql> drop database hellodb;
2.12 Start restoring data :
[[email protected] ]# mysql < /backup/hellodb_2013-09-08.sql // Import a full backup file
[[email protected] ]# mysql < /backup/hellodb_2013-09-08_05.sql // Import incremental backup file
[[email protected] ]# mysql< hellodb.sql // Import binaries
Verification complete , The display results are as we expected
notes : 1、 Really in the production environment , What we should export is the whole mysql Data in the server , Instead of a single library , So you should use –all-databases 2、 When exporting binary logs , You can directly copy files , But here's the thing , Scroll down the log before backup .
3、 utilize lvm Snapshot realizes almost hot standby data backup and recovery
3.1 Strategy :
Full backup + Binary log ;
3.2 Get ready :
notes : The transaction log must be in the same place as the data file LV On ;
3.3 establish lvm Lvm I won't say much about the creation of , If you want to know, click http://limian.blog.51cto.com/7542175/1256660
3.4 modify mysql The permissions and belonging groups of the files in the main configuration file storage directory , And initialization mysql
[[email protected] ~]# mkdir /mydata/data // Create a data directory
[[email protected] ~]# chown mysql:mysql /mydata/data // Change to group owner
[[email protected] ~]#
[[email protected] ~]# cd /usr/local/mysql/ // You must stand under this directory
[[email protected] mysql]# scripts/mysql_install_db --user=mysql --datadir=/mydata/data // initialization mysql
3.5 Modify the configuration file :
vim /etc/my.cof
datadir=/mydata/data Add data directory
sync_binlog = 1 Turn on this function
3.6 Start the service
[[email protected] mysql]# service mysqld start
mysql> set session sql_log_bin=0; Close binary log
mysql> source /backup/all_db_2013-09-08.sql Read backup file
3.7 go back to mysql The server :
mysql> FLUSH TABLES WITH READ LOCK; Request read lock
notes : Don't quit , Another terminal :
mysql> SHOW MASTER STATUS; Check the location of binary files
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000004 | 107 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
mysql> FLUSH LOGS; It is recommended to scroll down the log . It will be convenient to back up logs
3.8 Export binaries , Create a directory to store separately
[[email protected] ~]# mkdir /backup/limian
[[email protected] ~]# mysql -e 'show master status;' > /backup/limian/binlog.txt
[[email protected] ~]#
3.9 Create a snapshot of the volume where the data resides :
[[email protected] ~]# lvcreate -L 100M -s -p r -n mysql_snap /dev/myvg/mydata
Back to the server , Release read lock
mysql> UNLOCK TABLES;
[[email protected] ~]# mount /dev/myvg/mysql_snap /mnt/data
[[email protected] data]# cp * /backup/limian/
[[email protected] data]#lvremove /dev/myvg/mylv_snap
3.10 Update the data in the database , And delete the data file in the data directory , Simulation database corruption
mysql> create table limiantb (id int,name CHAR(10));
mysql> insert into limiantb values (1,'tom');
[[email protected] data]# mysqlbinlog --start-position=187 mysql-bin.000003 > /backup/limian/binlog.sql
[[email protected] backup]# cd /mydata/data/
[[email protected] data]# rm -rf *
[[email protected] ~]# cp -a /backup/limian/* /mydata/data/
[[email protected] data]# chown mysql:mysql *
3.11 test
Start the service
[[email protected] data]# service mysqld start
[[email protected] data]# mysql Login test
mysql> SHOW DATABASES;
mysql> SET sql_log_bin=0
mysql> source/backup/limian/binlog.sql; # Binary recovery
mysql> SHOW TABLES; # View the recovery results
mysql> SET sql_log_bin=1; # Turn on binary log
notes : This method realizes the backup of data files close to hot standby , And the data files are placed in lvm Can be flexibly changed according to the size of data lvm Size , The backup method is also very simple .
4、 be based on Xtrabackup Backup and restore
The official site :www.percona.com
advantage :
1、 Fast and reliable full backup
2、 Transactions will not be affected during backup
3、 Support data flow 、 Network transmission 、 Compress , So it can effectively save disk resources and network bandwidth .
4、 It can automatically back up the availability of verification data .
install Xtrabackup
[[email protected] ~]# rpm -ivh percona-xtrabackup-2.1.4-656.rhel6.i686.rpm
The latest version of the software can be downloaded from http://www.percona.com/software/percona-xtrabackup/ get
Be careful : When backing up the database , We should have permission , However, it should be noted that the user should be given the minimum permission when backing up the database , To ensure safety ,
4.1 Premise :
You should make sure that you are using a single table, a single table space , Otherwise, single table backup and recovery are not supported .
In the configuration file mysqld Segment plus
innodb_file_per_table = 1
4.2 Backup policy
Full backup + Incremental backup + Binary log
4.3 Prepare a directory for storing backup data
[[email protected] ~]# makdir /innobackup
4.4 Make a full backup :
[[email protected] ~]# innobackupex --user=root --password=mypass /innobackup/
notes :
1、 Just show it on the last line innobackupex: completed OK!, It means that your backup is correct .
2、 Another thing to note is that after each backup , A directory named after the current time point will be automatically created under the data directory to store the backup data , Let's go and see what we have
[[email protected] 2013-09-12_11-03-04]# ls
backup-my.cnf ibdata1 performance_schema xtrabackup_binary xtrabackup_checkpoints
hellodb mysql test xtrabackup_binlog_info xtrabackup_logfile
[[email protected] 2013-09-12_11-03-04]#
xtrabackup_checkpoints : Backup type 、 Backup status and LSN( Log serial number ) Scope information ;
xtrabackup_binlog_info :mysql The location of the binary log file currently in use by the server and the binary log events up to the time of backup .
xtrabackup_logfile : Non text files ,xtrabackup Your own log file
xtrabackup_binlog_pos_innodb : Binary log file and its application in InnoDB or XtraDB Table's binary log file position.
backup-my.cnf : During backup, the data file is about mysqld Configuration of
4.5 go back to mysql The server side updates the data
mysql> use hellodb;
mysql> delete from students where StuID>=24;
4.6 Incremental backup
innobackupex --user=root --password=mypass --incremental /innobackup/--incremental-basedir=/innobackup/2013-09-12_11-03-04/
--incremental Specify the backup type
--incremental-basedir= Specify which backup this incremental backup is based on , Here is the full backup file , In this way, the incremental backup data can be merged into the full backup
4.7 The second increment
Modify the data first
mysql> insert into students (Name,Age,Gender,ClassID,TeacherID) values ('tom',33,'M',2,4);
innobackupex --user=root --password=mypass --incremental /innobackup/ --incremental-basedir=/innobackup/2013-09-12_11-37-01/
Here, just change the last directory to the data directory of the first incremental backup
4.8 The last time the data was changed, but no incremental backup was made
mysql> delete from coc where id=14;
4.9 Back up binary log files ,( Because the last modification , No incremental backup , Rely on binary logs for point in time recovery )
[[email protected] data]# cp mysql-bin.000003 /tmp/
4.10 Simulated database crash
[[email protected] data]# service mysqld stop
[[email protected] data]# rm -rf *
Prepare for recovery
4.11 Do data synchronization for full backup
[[email protected] ~]# innobackupex --apply-log --redo-only /innobackup/2013-09-12_11-03-04/
4.12 Do data synchronization for the first increment
innobackupex --apply-log --redo-only /innobackup/2013-09-12_11-03-04/ --incremental-basedir=/innobackup/2013-09-12_11-37-01/
4.13 Do data synchronization for the second increment
innobackupex --apply-log --redo-only /innobackup/2013-09-12_11-03-04/ --incremental-basedir=/innobackup/2013-09-12_11-45-53/
--apply-log The significance of backup is that there is no commit Transaction revocation of , already commit But it is also applied to the database in the transaction log
notes :
about xtrabackup Speaking of , It is based on transaction log and data file backup , The backed up data may contain uncommitted transactions or transactions that have been committed but have not yet been synchronized to the database file , It should also be pretreated , Synchronize the committed transactions to the data file , Uncommitted transactions should be rolled back . So the database it backs up , It cannot be recovered immediately .
The process of pretreatment :
First, for the full backup file, only the committed transactions are synchronized to the data file , Pay attention to the increment , You cannot rollback transactions , Otherwise, your incremental backup will have no effect .
Then merge the first incremental backup into the full backup file ,
And so on , Merge the last few increments into the file after the previous merge , In this case , We just need to take a full backup + Binary log , You can do point in time recovery .
4.14 Data recovery
[[email protected] ~]# service mysqld stop
[[email protected] data]# rm -rf * Simulated database crash
[[email protected] ~]# innobackupex --copy-back /innobackup/2013-09-12_11-03-04/
--copy-back Database recovery , Follow the location of the backup directory
4.15 testing :
Log in and copy
[[email protected] ~]# cd /mydata/data/
[[email protected] data]# chown mysql:mysql *
[[email protected] data]#service mysqld start
The test result data is normal .
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/148258.html Link to the original text :https://javaforall.cn
边栏推荐
- 微信小程序视频分享平台系统毕业设计毕设(5)任务书
- 辉芒微IO单片机FT60F11F-MRB
- Linux中,mysql设置job任务自动启动
- Bluetooth technology | new working mode of wearable devices of the Internet of things, and Bluetooth ble helps the new working mode
- vimium映射键
- Yilong em78p153k dip14 MCU
- Easyai notes - machine learning
- Aloam code reading and summary
- What should we pay attention to in the development process of Yingguang single chip microcomputer?
- 1288_ Implementation analysis of vtask resume() interface and interrupt Security version interface in FreeRTOS
猜你喜欢
Customize a loading instruction
[golang | grpc] generate certificates using OpenSSL
MySQL -- basic operation of database
拿起相机,便是最好的艺术疗愈
[games101] operation 4 B é zier curve
finally详解
微信小程序视频分享平台系统毕业设计毕设(6)开题答辩PPT
深入理解ThreadLocal
求求你们,别再刷 Star 了!这跟“爱国”没关系!
Edgenext hit a mixed punch: a lightweight architecture integrating CNN and transformer
随机推荐
MySQL -- basic operation of database
详解Kubernetes网络模型
怎么可以省去大量的switch语句,省去switch语句
MySQL安装与配置
Yingguang pmc131 SOP16 16pin eight bit MCU
Pms150c Yingguang MCU development case
What should we pay attention to in the development process of Yingguang single chip microcomputer?
Huimang micro IO MCU ft60f010a-urt
应广单片机开发案例
[golang | grpc] generate certificates using OpenSSL
Embedded development board ~ description
wait_ for_ Gap -- restore archive from primary archive to secondary Archive
原厂原装 应广单片机PMS134方案开发应用案例
Unified interface for reading and writing data files in xml/json/ini and ubjson formats
Pms132b single chip microcomputer TWS digital tube Bluetooth charging chamber program development
matplotlib的安装教程以及简单调用
【Zuul】com. netflix. zuul. exception. ZuulException: Hystrix Readed time out
MySQL -- basic concept of database
android之循环定时器实现,实现定Android时缓存清理
MySQL installation and configuration