搜索
首页数据库mysql教程怎么通过日志文件恢复MySQL数据

    1、找到最新的 binlog 文件

    进入 mysql 命令行执行如下命令

    mysql> show master status;
    +------------------+-----------+
    | Log_name         | File_size |
    +------------------+-----------+
    | binlog.000001 |       967 |
    | binlog.000002 |       965 |
    +------------------+-----------+

    一般最新的编号大,上面最新的就是 binlog.000002

    2、找到我们想恢复数据在日志文件里的开始结束位置

    这里有两种方式来确定开始位置和结束位置,一种是使用时间作为开始结束,一种是使用日志的 position 作为开始结束位置

    2.1、使用时间范围

    通过 mysqlbinlog mysql-bin.000002 命令查看日志内容,然后找到删除的时间点:

    # at 131708213
    #210610 11:27:01 server id 1  end_log_pos 131708311 CRC32 0x0fc755e2     Table_map: `loongwind_base`.`xxxx` mapped to number 139
    # at 131708311
    #210610 11:27:01 server id 1  end_log_pos 131708411 CRC32 0xa91616b9     Write_rows: table id 139 flags: STMT_END_F
    
    BINLOG '
    BffBYBMBAAAAYgAAAJe12QcAAIsAAAAAAAEADmR4bWhfYmFzZV9oenN5ABpkeF9zeV9hc3NldHNf
    ZXh0ZW5kc19jb3B5MQAICAgIDwgSCBIEAAgAAPgBAQACA/z/AOJVxw8=
    BffBYB4BAAAAZAAAAPu12QcAAIsAAAAAAAMAAgAI/wDRAwAAm1M8AUIAAADRUjwBCgAAAFYL5gAM
    AOWFrOWuieacuuWFs7EBAADbwZkAmama6E+xAQAA28GZAJmpmuhPuRYWqQ==
    '/*!*/;

    然后确定上次备份的时间点,如果通过日志找不到上次备份的时间点可以填一个你记忆中确定小于上次备份的时间点

    2.2、使用 position 范围

    使用如下命令查看日志 event 的 position

    mysql -uroot -p'password' -e "show binlog events in 'binlog.000002'"|grep -i 'DROP TABLE'

    执行结果如下:

    binlog.000002    820474948    Query    1    820475111    use `loongwind_base`; DROP TABLE IF EXISTS `undo_log` /* generated by server */ /* xid=11790691 */

    即删除的 position 为 820474948

    还是通过上述命令,替换关键字查找到上次备份的 position 点

    3、恢复

    3.1 通过时间恢复

    mysqlbinlog --no-defaults --database=loongwind_base --start-datetime="2021-06-07 09:00:00" --stop-datetime="2021-06-10 16:37:58" binlog.000005 | mysql -uroot -p'password' -s -N -f -D loongwind_base

    其中 dxmh_base_hzsy 是数据库名称

    3.2 通过 position 恢复

    mysqlbinlog  --start-position=1178  --stop-position=2751 -d dxmh-sy binlog.000002|mysql -uroot -p'password' -s -N -f -D loongwind_base
    loongwind_base 为数据库名称

    如果实在找不到开始时间或者开始 position 也可以不写 --start-datetime 或 --start-position ,这样就是用这个日志文件的开始一直恢复到结束,为了防止与已有数据的冲突,需要加上 -f 即 force 跳过错误继续往下执行。

    以上是怎么通过日志文件恢复MySQL数据的详细内容。更多信息请关注PHP中文网其他相关文章!

    声明
    本文转载于:亿速云。如有侵权,请联系admin@php.cn删除
    在MySQL中使用视图的局限性是什么?在MySQL中使用视图的局限性是什么?May 14, 2025 am 12:10 AM

    mysqlviewshavelimitations:1)他们不使用Supportallsqloperations,限制DatamanipulationThroughViewSwithJoinSorsubqueries.2)他们canimpactperformance,尤其是withcomplexcomplexclexeriesorlargedatasets.3)

    确保您的MySQL数据库:添加用户并授予特权确保您的MySQL数据库:添加用户并授予特权May 14, 2025 am 12:09 AM

    porthusermanagementInmysqliscialforenhancingsEcurityAndsingsmenting效率databaseoperation.1)usecReateusertoAddusers,指定connectionsourcewith@'localhost'or@'%'。

    哪些因素会影响我可以在MySQL中使用的触发器数量?哪些因素会影响我可以在MySQL中使用的触发器数量?May 14, 2025 am 12:08 AM

    mysqldoes notimposeahardlimitontriggers,butacticalfactorsdeterminetheireffactective:1)serverConfiguration impactactStriggerGermanagement; 2)复杂的TriggerSincreaseSySystemsystem load; 3)largertablesslowtriggerperfermance; 4)highConconcConcrencerCancancancancanceTigrignecentign; 5); 5)

    mysql:存储斑点安全吗?mysql:存储斑点安全吗?May 14, 2025 am 12:07 AM

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

    mySQL:通过PHP Web界面添加用户mySQL:通过PHP Web界面添加用户May 14, 2025 am 12:04 AM

    通过PHP网页界面添加MySQL用户可以使用MySQLi扩展。步骤如下:1.连接MySQL数据库,使用MySQLi扩展。2.创建用户,使用CREATEUSER语句,并使用PASSWORD()函数加密密码。3.防止SQL注入,使用mysqli_real_escape_string()函数处理用户输入。4.为新用户分配权限,使用GRANT语句。

    mysql:blob和其他无-SQL存储,有什么区别?mysql:blob和其他无-SQL存储,有什么区别?May 13, 2025 am 12:14 AM

    mysql'sblobissuitableForStoringBinaryDataWithInareLationalDatabase,而alenosqloptionslikemongodb,redis和calablesolutionsoluntionsoluntionsoluntionsolundortionsolunsolunsstructureddata.blobobobsimplobissimplobisslowderperformandperformanceperformancewithlararengelitiate;

    mySQL添加用户:语法,选项和安全性最佳实践mySQL添加用户:语法,选项和安全性最佳实践May 13, 2025 am 12:12 AM

    toaddauserinmysql,使用:createUser'username'@'host'Indessify'password'; there'showtodoitsecurely:1)choosethehostcarecarefullytocon trolaccess.2)setResourcelimitswithoptionslikemax_queries_per_hour.3)usestrong,iniquepasswords.4)Enforcessl/tlsconnectionswith

    MySQL:如何避免字符串数据类型常见错误?MySQL:如何避免字符串数据类型常见错误?May 13, 2025 am 12:09 AM

    toAvoidCommonMistakeswithStringDatatatPesInMysQl,CloseStringTypenuances,chosethirtightType,andManageEngencodingAndCollat​​ionsEttingsefectery.1)usecharforfixed lengengters lengengtings,varchar forbariaible lengength,varchariable length,andtext/blobforlabforlargerdata.2 seterters seterters seterters seterters

    See all articles

    热AI工具

    Undresser.AI Undress

    Undresser.AI Undress

    人工智能驱动的应用程序,用于创建逼真的裸体照片

    AI Clothes Remover

    AI Clothes Remover

    用于从照片中去除衣服的在线人工智能工具。

    Undress AI Tool

    Undress AI Tool

    免费脱衣服图片

    Clothoff.io

    Clothoff.io

    AI脱衣机

    Video Face Swap

    Video Face Swap

    使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

    热门文章

    热工具

    Atom编辑器mac版下载

    Atom编辑器mac版下载

    最流行的的开源编辑器

    SublimeText3 英文版

    SublimeText3 英文版

    推荐:为Win版本,支持代码提示!

    禅工作室 13.0.1

    禅工作室 13.0.1

    功能强大的PHP集成开发环境

    mPDF

    mPDF

    mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

    Dreamweaver Mac版

    Dreamweaver Mac版

    视觉化网页开发工具