search
HomeDatabaseMysql Tutorial MySQL数据库备份总结

一个企业的正常运行,数据的完整性是最关键的;所以我们需要在工作中要很熟练的掌握数据的备份与恢复方法;下面是对Mysql数据库备份的三种方法总结,希望对大家

一个企业的正常运行,数据的完整性是最关键的;所以我们需要在工作中要很熟练的掌握数据的备份与恢复方法;下面是对Mysql数据库备份的三种方法总结,希望对大家会有所帮助 

备份开始前的工作环境准备:

1、创建用于保存二进制日志文件的目录

  • # mkdir /mybinlog 
  • # chown mysql.mysql /mybinlog 
  • 2、修改配置文件

    3、为备份数据库创建存放点

  • # mkdir /mybackup 
  • # chown -R mysql.mysql /mybackup 
  • 4、启动mysql服务器

  • # service mysqld start 
  • 5、插入需要备份的数据库

  • 一、使用mysqldump工具完成完全备份+增量备份基于mysqldump通常就是完整备份+二进制日志来进行恢复数据

    1.1、mysqldump用来温备份,首先需要为所有库加读锁,并且滚动一下二进制日志,记录当前二进制文件位置

    1.2、备份二进制日志

  • # cp /mybinlog/mysql-bin.000001 /mybackup/alldatabase.000001 
  • 1.3、模拟数据库意外损坏,利用完全备份实现数据库的恢复

  • # rm -rf /mydata/data/* 
  • # rm -rf /mybinlog/* 
  • 1.4、初始化mysql并启动服务器

  • 1.5、删除二进制日志,启动服务

    1.6、恢复到备份状态,导入备份的数据库文件:

  • 2、模拟往students表中添加数据,香港虚拟主机,添加完成后不小心将表删除了,我们要恢复到删除之前的状态,并且新加的数据还要存在

    2.1、往students表中添加数据

  • mysql> insert into students (Name,Age,Gender) values ('hadoop',22,'M'); 
  • 2.2、模拟一下,不小心将表删除了

  • 2.3、查看一下二进制日志文件的位置

    2.4、先恢复完整数据(恢复过程不要记录在日志中)

  • # mysql  /mybackup/alldatabase.sql 
  • 2.5、查看删除表时的记录位置

    2.5、将二进制文件中完整备份到删除表之前的记录导出

    2.6、将改变的数据库日志导入到mysql库中

  • 2.7、见证奇迹的时刻数据库恢复成功,并且插入的数据也还原回来了

     

    二、select命令也能完成逻辑备份比mysqldump更节约空间,速度更快,但比mysqldump用起来要麻烦,并且备份出来的数据都是纯文本信息,没有额外的开销空间,适合备份某张表模拟备份一张表

    1、备份出来,保存在某个目录下,但需要注意的是,这个目录下的文件的具有权限,当登录到mysql时需要具有执行的权限

    2、恢复数据库需要创建一个空表,模仿原来的表创建

  • 把原来的表删除了

  • mysql> load data infile '/tmp/tutor.txt' into table tutor; 
  • 验证:

    当然用select也可以把表中符合条件的语句备份出来,香港虚拟主机,这里不再做演示了,香港虚拟主机,很简单。

    这种方法适合于某长表的备份,但不会记录到二进制日志中

    三、利用LVM快照从物理角度实现几乎热备的完全备份,配合二进制日志备份实现增量备份,进而实现数据库的备份。用lvm的快照来备份速度是非常快的,而且几乎热备,恢复也很快速,操作也简单,完整恢复后再将相应二进制恢复即可。前提:

    1、数据文件要在逻辑卷上

    2、此逻辑卷所在卷组必须有足够空间使用快照卷

    3、数据文件和事务日志要在同一个逻辑卷上

    步骤:

    1、启动事务

    2、打开会话,施加读锁,锁定所有表,此时别人是不能执行命令的(不能往数据库中插入数据)

    注意:执行表锁定时,一定不要退出

    3、通过另一个终端,保存二进制日志文件及相关信息位置

    4、创建快照卷

  • # lvcreate -L 50M -s -p r -n mydata-snap /dev/myvg/mydata  
  • 5、释放锁

    6、挂载快照卷,备份

    7、卸载/mnt,删除快照卷

  • # umount /mnt/ 
  • # lvremove --force /dev/myvg/mydata-snap  
  • 8、增量备份二进制日志

    首先删除二进制日志文件,对我们没有太大用处

  • # rm -rf /backup/full-backup-2013-05-06/mysql-bin.* -f 
  • 现在模拟数据库被格式化

    格式化之前先把二进制日志备份出来

    恢复到了还原前的状态

    这就是逻辑卷实现的一次完全备份

    本文出自 “丽的博客” 博客,请务必保留此出处

    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
    How to Grant Permissions to New MySQL UsersHow to Grant Permissions to New MySQL UsersMay 09, 2025 am 12:16 AM

    TograntpermissionstonewMySQLusers,followthesesteps:1)AccessMySQLasauserwithsufficientprivileges,2)CreateanewuserwiththeCREATEUSERcommand,3)UsetheGRANTcommandtospecifypermissionslikeSELECT,INSERT,UPDATE,orALLPRIVILEGESonspecificdatabasesortables,and4)

    How to Add Users in MySQL: A Step-by-Step GuideHow to Add Users in MySQL: A Step-by-Step GuideMay 09, 2025 am 12:14 AM

    ToaddusersinMySQLeffectivelyandsecurely,followthesesteps:1)UsetheCREATEUSERstatementtoaddanewuser,specifyingthehostandastrongpassword.2)GrantnecessaryprivilegesusingtheGRANTstatement,adheringtotheprincipleofleastprivilege.3)Implementsecuritymeasuresl

    MySQL: Adding a new user with complex permissionsMySQL: Adding a new user with complex permissionsMay 09, 2025 am 12:09 AM

    ToaddanewuserwithcomplexpermissionsinMySQL,followthesesteps:1)CreatetheuserwithCREATEUSER'newuser'@'localhost'IDENTIFIEDBY'password';.2)Grantreadaccesstoalltablesin'mydatabase'withGRANTSELECTONmydatabase.TO'newuser'@'localhost';.3)Grantwriteaccessto'

    MySQL: String Data Types and CollationsMySQL: String Data Types and CollationsMay 09, 2025 am 12:08 AM

    The string data types in MySQL include CHAR, VARCHAR, BINARY, VARBINARY, BLOB, and TEXT. The collations determine the comparison and sorting of strings. 1.CHAR is suitable for fixed-length strings, VARCHAR is suitable for variable-length strings. 2.BINARY and VARBINARY are used for binary data, and BLOB and TEXT are used for large object data. 3. Sorting rules such as utf8mb4_unicode_ci ignores upper and lower case and is suitable for user names; utf8mb4_bin is case sensitive and is suitable for fields that require precise comparison.

    MySQL: What length should I use for VARCHARs?MySQL: What length should I use for VARCHARs?May 09, 2025 am 12:06 AM

    The best MySQLVARCHAR column length selection should be based on data analysis, consider future growth, evaluate performance impacts, and character set requirements. 1) Analyze the data to determine typical lengths; 2) Reserve future expansion space; 3) Pay attention to the impact of large lengths on performance; 4) Consider the impact of character sets on storage. Through these steps, the efficiency and scalability of the database can be optimized.

    MySQL BLOB : are there any limits?MySQL BLOB : are there any limits?May 08, 2025 am 12:22 AM

    MySQLBLOBshavelimits:TINYBLOB(255bytes),BLOB(65,535bytes),MEDIUMBLOB(16,777,215bytes),andLONGBLOB(4,294,967,295bytes).TouseBLOBseffectively:1)ConsiderperformanceimpactsandstorelargeBLOBsexternally;2)Managebackupsandreplicationcarefully;3)Usepathsinst

    MySQL : What are the best tools to automate users creation?MySQL : What are the best tools to automate users creation?May 08, 2025 am 12:22 AM

    The best tools and technologies for automating the creation of users in MySQL include: 1. MySQLWorkbench, suitable for small to medium-sized environments, easy to use but high resource consumption; 2. Ansible, suitable for multi-server environments, simple but steep learning curve; 3. Custom Python scripts, flexible but need to ensure script security; 4. Puppet and Chef, suitable for large-scale environments, complex but scalable. Scale, learning curve and integration needs should be considered when choosing.

    MySQL: Can I search inside a blob?MySQL: Can I search inside a blob?May 08, 2025 am 12:20 AM

    Yes,youcansearchinsideaBLOBinMySQLusingspecifictechniques.1)ConverttheBLOBtoaUTF-8stringwithCONVERTfunctionandsearchusingLIKE.2)ForcompressedBLOBs,useUNCOMPRESSbeforeconversion.3)Considerperformanceimpactsanddataencoding.4)Forcomplexdata,externalproc

    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 Tools

    Atom editor mac version download

    Atom editor mac version download

    The most popular open source editor

    SAP NetWeaver Server Adapter for Eclipse

    SAP NetWeaver Server Adapter for Eclipse

    Integrate Eclipse with SAP NetWeaver application server.

    PhpStorm Mac version

    PhpStorm Mac version

    The latest (2018.2.1) professional PHP integrated development tool

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    SublimeText3 Linux new version

    SublimeText3 Linux new version

    SublimeText3 Linux latest version