• <fieldset id="8imwq"><menu id="8imwq"></menu></fieldset>
  • <bdo id="8imwq"><input id="8imwq"></input></bdo>
    最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
    問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
    當前位置: 首頁 - 科技 - 知識百科 - 正文

    讀取mysqlbinlog開始和結束時間_MySQL

    來源:懂視網 責編:小采 時間:2020-11-09 18:12:50
    文檔

    讀取mysqlbinlog開始和結束時間_MySQL

    讀取mysqlbinlog開始和結束時間_MySQL:bitsCN.com mysql binlog 記錄了所有可能涉及更新的操作,可以用來作為增量備份的一種選擇。為了管理binlog ,需要讀取每個binlog 文件的準確的開始和結束時間。用mysqlbinlog 工具可以解析binlog 文件,所以也可以通過分析輸出結果來獲取。但是mysqlb
    推薦度:
    導讀讀取mysqlbinlog開始和結束時間_MySQL:bitsCN.com mysql binlog 記錄了所有可能涉及更新的操作,可以用來作為增量備份的一種選擇。為了管理binlog ,需要讀取每個binlog 文件的準確的開始和結束時間。用mysqlbinlog 工具可以解析binlog 文件,所以也可以通過分析輸出結果來獲取。但是mysqlb

    bitsCN.com mysql binlog 記錄了所有可能涉及更新的操作,可以用來作為增量備份的一種選擇。為了管理binlog ,需要讀取每個binlog 文件的準確的開始和結束時間。用mysqlbinlog 工具可以解析binlog 文件,所以也可以通過分析輸出結果來獲取。但是mysqlbinlog 只能順序讀取記錄,如果只是分析開始時間還好,要分析結束時間,就必須等它把整個binlog 處理完。在binlog 文件體積大的時候,代價就大了些。好在mysql 對binlog 文件的格式是公開的,所以我們可以直接通過解析文件自己實現。

    binlog 文件的格式在http://forge.mysql.com/wiki/MySQL_Internals_Binary_Log 可以找到。每個binlog 文件都有相同的開頭:0xfe 0×62 0×69 0x6e 。也就是0xfe 后面加上bin 。之后,就是一個個事件數據。binlog 的事件類型有很多種,但每個binlog 文件的第一個事件一定是格式描述事件(format description event),描述了binlog 文件格式版本信息;最后一個時間一定是輪轉事件(rotate event),記錄了下一個binlog 的文件名和事件開始偏移位置。每個事件都有一個一致的事件頭,其中就有事件的時間戳、事件類型等。讀取第一個事件和最后一個事件的信息就可以獲取binlog 文件的準確開始和結束時間了。

    讀取第一個事件format description event 要容易一些,seek 跳過文件頭,讀取事件頭就行了。讀取最后一個事件的時間要稍麻煩些。因為事件的長度是不固定的。對于輪轉事件來說,除了事件頭以外,后面還有一個64位整數的開始位置偏移量以及下一個binlog 的文件名。長度不確定的部分就是最后的文件名部分。好在那個偏移量是一個固定的值:4(也就是跳過文件頭),所以可以從后往前讀取,用它來作為標記,檢查是否讀完了文件名。然后就可以跳過文件名和偏移量,讀取最后一個事件的事件頭了。

    php 代碼如下:

    /**
    * read binlog info
    *
    * A mysql binlog file is begin with a head "/xfebin" and then log evnets. The
    * first event is a format description event, the last event is a rotate event.
    *
    * For more infomation about mysql binlog format, see http://forge.mysql.com/wiki/MySQL_Internals_Binary_Log
    */
    class BinlogInfo {
    const EVENT_HEAD_SIZE = 19;
    const FORMAT_DESCRIPTION_EVENT_DATA_SIZE = 59;
    const BINLOG_HEAD = "/xfebin";
    const FORMAT_DESCRIPTION_EVENT = 15;
    const ROTATE_EVENT = 4;

    private $eventHeadPackStr = '';
    private $formatDescriptionEventDataPackStr = '';

    function __construct() {
    $this->eventHeadPackStr = $this->eventHeadPackStr();
    $this->formatDescriptionEventDataPackStr = $this->formatDescriptionEventDataPackStr();
    }

    protected function eventHeadPackStr() {
    $event_header_struct = array(
    'timestamp' => 'l',
    'type_code' => 'c',
    'server_id' => 'l',
    'event_length' => 'l',
    'next_position' => 'l',
    'flags' => 's',
    );
    return $this->toPackStr($event_header_struct);
    }

    protected function formatDescriptionEventDataPackStr() {
    $format_description_event_data_struct = array(
    'binlog_version' => 's',
    'server_version' => 'a50',
    'create_timestamp' => 'l',
    'head_length' => 'c'
    );
    return $this->toPackStr($format_description_event_data_struct);
    }

    protected function toPackStr($arr) {
    $ret = '';
    foreach ($arr as $k=>$v) {
    $ret.= '/'.$v.$k;
    }
    $ret = substr($ret, 1);
    return $ret;
    }

    /**
    * @param resource $file
    *
    * Mysql binlog file begin with a 4 bytes head: "/xfebin".
    */
    protected function isBinlog($file) {
    rewind($file);
    $head = fread($file, strlen(self::BINLOG_HEAD));
    return $head == self::BINLOG_HEAD;
    }

    /**
    * @param resource $file
    *
    * Format description event is the first event of a binlog file
    */
    protected function readFormatDescriptionEvent($file) {
    fseek($file, strlen(self::BINLOG_HEAD), SEEK_SET);
    $head_str = fread($file, self::EVENT_HEAD_SIZE);
    $head = unpack($this->eventHeadPackStr, $head_str);
    if ($head['type_code'] != self::FORMAT_DESCRIPTION_EVENT) {
    return null;
    }
    $data_str= fread($file, self::FORMAT_DESCRIPTION_EVENT_DATA_SIZE);
    $data = unpack($this->formatDescriptionEventDataPackStr, $data_str);

    return array('head'=>$head, 'data'=>$data);
    }

    /**
    * @param resource $file
    *
    * Rotate event is the last event of a binglog.
    * After event header, there is a 64bit int indicate the first event
    * position of next binlog file and next binlog file name without /0 at end.
    * The position is always be 4 (hex: 0400000000000000).
    *
    */
    protected function readRotateEvent($file)
    {
    /**
    * Rotate event size is 19(head size) + 8(pos) + len(filename).
    * 100 bytes can contain a filename which length less than 73 bytes and
    * it is short than the length of format description event so filesize -
    * bufsize will never be negative.
    */
    $bufsize = 100;
    $size_pos = 8;
    fseek($file, -$bufsize, SEEK_END);
    $buf = fread($file, $bufsize);
    $min_begin = strlen(self::BINLOG_HEAD) + self::EVENT_HEAD_SIZE + $size_pos;
    $ok = false;
    for ($i = $bufsize - 1; $i > $min_begin; $i--) {
    if ($buf[$i] == "/0") {
    $ok = true;
    break;
    }
    }
    if (!$ok) {
    return null;
    }
    $next_filename = substr($buf, $i + 1);

    $head_str = substr($buf, $i + 1 - $size_pos - self::EVENT_HEAD_SIZE, self::EVENT_HEAD_SIZE);
    $head = unpack($this->eventHeadPackStr, $head_str);
    if ($head['type_code'] != self::ROTATE_EVENT) {
    return null;
    }
    return array('head'=>$head, 'nextFile'=>$next_filename);
    }

    /**
    * @param string $path path to binlog file
    */
    function read($path) {
    $file = fopen($path, 'r');
    if (!$file) {
    return null;
    }
    if (!$this->isBinlog($file)) {
    fclose($file);
    return null;
    }

    $fde = $this->readFormatDescriptionEvent($file);
    $re = $this->readRotateEvent($file);
    fclose($file);
    return array(
    'beginAt' => $fde['head']['timestamp'],
    'endAt' => $re['head']['timestamp'],
    'nextFile' => $re['nextFile'],
    'serverVersion' => $fde['data']['server_version'],
    );
    }
    } bitsCN.com

    聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

    文檔

    讀取mysqlbinlog開始和結束時間_MySQL

    讀取mysqlbinlog開始和結束時間_MySQL:bitsCN.com mysql binlog 記錄了所有可能涉及更新的操作,可以用來作為增量備份的一種選擇。為了管理binlog ,需要讀取每個binlog 文件的準確的開始和結束時間。用mysqlbinlog 工具可以解析binlog 文件,所以也可以通過分析輸出結果來獲取。但是mysqlb
    推薦度:
    • 熱門焦點

    最新推薦

    猜你喜歡

    熱門推薦

    專題
    Top
    主站蜘蛛池模板: 亚洲一区精品中文字幕| 亚洲精品乱码久久久久久 | 久久国产成人精品麻豆| 国产精品久久免费| 精品一区二区三区中文字幕| 欧美久久久久久午夜精品| 中文国产成人精品久久不卡| 日本午夜精品一区二区三区电影| 精品欧洲av无码一区二区三区| 成人精品在线视频| 国产欧美日本精品| 亚洲精品高清在线| 凹凸国产熟女精品视频app| 午夜影视日本亚洲欧洲精品一区| 日产国产精品亚洲系列| 91麻豆精品国产| 国产亚洲精品国产| 精品国际久久久久999波多野| 香港三级精品三级在线专区 | 人妻VA精品VA欧美VA| 国产欧美日本精品| 国产三级精品三级| 国产精品人成在线观看| 久久久久一级精品亚洲国产成人综合AV区 | 国产精品 视频一区 二区三区| 精品在线免费观看| 久久精品一区二区| 日产国产精品亚洲系列| 狠狠精品干练久久久无码中文字幕| 日韩精品系列产品| 99久久精品国产一区二区三区 | 2022国产精品自产拍在线观看| 亚洲AV永久精品爱情岛论坛| 亚洲国产精品尤物yw在线| 欧美亚洲另类精品第一页 | 国产2021精品视频免费播放| 999国内精品永久免费观看| 成人伊人精品色XXXX视频| 999精品在线| 久久99国产精品久久99果冻传媒| 九九精品成人免费国产片|