search
HomeDatabaseMysql TutorialDetailed explanation of MySQL backup and recovery

MySQL data backup

In mySQL, there are logical backups and physical backups. The biggest advantage of logical backup is that the same method can be used for backup of various storage engines. Physical backup is different, and different storage engines have different backup methods.

Logical backup and recovery

Backup

In MySQL, logical backup uses mysqldump to back up the data in the database as a text file. The backed up file can be viewed and edited. . According to the backup scope, backup can be divided into the following three types of backup.

  • Back up a specified database or some tables in the database
    mysqldump [options] database name [table name] > data.sql

  • Back up multiple specified databases
    mysqldump [options] –database > data.sql database 1 database 2 database 3...

  • Back up all Database
    mysqldump [options] –all-database > data.sql
    Analysis: [options] During backup, required permission information, etc. There are many options for mysqldump, which can be viewed through mysqldump –help. In order to ensure the consistency of data backup, when the MySQL storage engine is in the north, you need to add the -l parameter, which means to add read locks to all tables. During the backup period, all tables will only be read, not written. But for the InnoDB engine, –single-transaction can be used. Data file backed up by data.sql
    Parameters:
    l: represents locking all tables
    f: represents generating a new daily file

The instance will be xxpt All tables in the database are backed up to the dequan.sql table. The command is as follows:
mysqldump -uroot -p xxpt >dequan.sql
Detailed explanation of MySQL backup and recovery
Because I did not specify the backup path above, by default, the backup will be to the current path, so the backup will be to D:\ The wamp\bin\mysql\mysql5.6.17\bin path is under.

Recovery

Full recovery

Mysqldump recovery is also very simple, just execute the backup as input. The results are as follows:
mysql -uroot -p dbname

Incomplete recovery

Incomplete recovery includes point-in-time-based recovery and location-based recovery. The time point and position correspond to the time point and position in the binary log (binlog log).
Detailed explanation of MySQL backup and recovery

Based on time point

If the data room is set to be wrong between 4:00 pm and before 5:00 pm, it needs to be skipped during recovery. First let's take a look at the binlog log. For example, updating data after 4 pm is wrong and needs to be skipped during recovery. Deleting data after 5 pm is correct and needs to be retained.
Detailed explanation of MySQL backup and recovery
1. Through mysql -uroot -p dbname

Location-based recovery

When using location recovery, we need to first check the binlog log file to determine the location number , and then use the following command to restore:
mysqlbinlog D:\wamp\bin\mysql\mysql5.6.17\data\mybinlog.000012 –stop-position=716406|mysql -uroot -p
Restore the data after 5 points Operation
D:\wamp\bin\mysql\mysql5.6.17\data\mybinlog.000012 –start-position=723613|mysql -uroot -p
Detailed explanation of MySQL backup and recovery

Physical backup and recovery

Physical backup is divided into cold backup and hot backup. Compared with logical backup, its biggest advantage is the fast backup and recovery speed. Because the principle of physical backup is based on file cp.

Cold backup and recovery

Cold backup is actually a method of stopping the database service and copying the data files. This method is suitable for both MyISAM and InnoDB.
Recovery: First stop the Mysql service, restore the MySQL data files at the operating system level, then restart the Mysql service, and use the Mysqlbinlog tool to restore all binlogs since the backup.

Hot backup

The hot backup of different storage engines in mysql is different.

MyISAM Storage Engine

The backup principle of MyISAM storage engine is to add a read lock to the table to be backed up, and then cp the data file to the backup directory. Commonly used methods

  • Method 1: Use mysqlhotcop
    mysqlhotcop db_name [Directory]

  • Method 2: Manually lock the table copy
    First Add read locks to all tables in the database, and then cp the data.
    Lock all tables flush tables with read lock;
    Detailed explanation of MySQL backup and recovery

InnoDB storage engine

Learning···

表的导入与导出

导出

  • 使用SELECT …INTO OUTFILE …+[options]命令实现
    关于options参数如下
    Detailed explanation of MySQL backup and recovery
    默认路径为该数据对应的路径下:
    Detailed explanation of MySQL backup and recovery
    Detailed explanation of MySQL backup and recovery

  • 使用mysqldump
    mysqldump -u username -T targetDir dbname tableName[options]
    比如:
    mysqldump -uroot -p -T  D:/wamp/bin/mysql/mysql5.6.17/  xxpt t1  
    生成了两个文件,如下图:
    Detailed explanation of MySQL backup and recovery
    t1.txt中保存中数据信息,t1.sql文件内容如下

-- MySQL dump 10.13  Distrib 5.6.17, for Win32 (x86)
---- Host: localhost    Database: xxpt
-- ------------------------------------------------------
-- Server version   5.6.17-log
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
---- Table structure for table `t1`--DROP TABLE IF EXISTS `t1`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `t1` (  `id1` int(11) NOT NULL DEFAULT '0',  
`id2` int(3) unsigned zerofill NOT NULL DEFAULT '000') ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
-- Dump completed on 2016-04-18 19:30:51

导入

方法一:
load data infile
eg:
load data infile ‘D:/wamp/bin/mysql/mysql5.6.17/t1.txt’ into table t1;
Detailed explanation of MySQL backup and recovery

方法二:使用mysqlinport

 以上就是MySQL的备份与恢复详解的内容,更多相关内容请关注PHP中文网(www.php.cn)!


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What Are the Limitations of Using Views in MySQL?What Are the Limitations of Using Views in MySQL?May 14, 2025 am 12:10 AM

MySQLviewshavelimitations:1)Theydon'tsupportallSQLoperations,restrictingdatamanipulationthroughviewswithjoinsorsubqueries.2)Theycanimpactperformance,especiallywithcomplexqueriesorlargedatasets.3)Viewsdon'tstoredata,potentiallyleadingtooutdatedinforma

Securing Your MySQL Database: Adding Users and Granting PrivilegesSecuring Your MySQL Database: Adding Users and Granting PrivilegesMay 14, 2025 am 12:09 AM

ProperusermanagementinMySQLiscrucialforenhancingsecurityandensuringefficientdatabaseoperation.1)UseCREATEUSERtoaddusers,specifyingconnectionsourcewith@'localhost'or@'%'.2)GrantspecificprivilegeswithGRANT,usingleastprivilegeprincipletominimizerisks.3)

What Factors Influence the Number of Triggers I Can Use in MySQL?What Factors Influence the Number of Triggers I Can Use in MySQL?May 14, 2025 am 12:08 AM

MySQLdoesn'timposeahardlimitontriggers,butpracticalfactorsdeterminetheireffectiveuse:1)Serverconfigurationimpactstriggermanagement;2)Complextriggersincreasesystemload;3)Largertablesslowtriggerperformance;4)Highconcurrencycancausetriggercontention;5)M

MySQL: Is it safe to store BLOB?MySQL: Is it safe to store BLOB?May 14, 2025 am 12:07 AM

Yes,it'ssafetostoreBLOBdatainMySQL,butconsiderthesefactors:1)StorageSpace:BLOBscanconsumesignificantspace,potentiallyincreasingcostsandslowingperformance.2)Performance:LargerrowsizesduetoBLOBsmayslowdownqueries.3)BackupandRecovery:Theseprocessescanbe

MySQL: Adding a user through a PHP web interfaceMySQL: Adding a user through a PHP web interfaceMay 14, 2025 am 12:04 AM

Adding MySQL users through the PHP web interface can use MySQLi extensions. The steps are as follows: 1. Connect to the MySQL database and use the MySQLi extension. 2. Create a user, use the CREATEUSER statement, and use the PASSWORD() function to encrypt the password. 3. Prevent SQL injection and use the mysqli_real_escape_string() function to process user input. 4. Assign permissions to new users and use the GRANT statement.

MySQL: BLOB and other no-sql storage, what are the differences?MySQL: BLOB and other no-sql storage, what are the differences?May 13, 2025 am 12:14 AM

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

MySQL Add User: Syntax, Options, and Security Best PracticesMySQL Add User: Syntax, Options, and Security Best PracticesMay 13, 2025 am 12:12 AM

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

MySQL: How to avoid String Data Types common mistakes?MySQL: How to avoid String Data Types common mistakes?May 13, 2025 am 12:09 AM

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools