我的MYSQL是5.7版本
binlog-format=row
我在用mysqlbinlog -v -v  --base64-output=DECODE-ROWS 查看二进制文件时 所有的DML语句前面都有“###”3个这个符号,导致我没法进行数据恢复 要怎么解决啊 狼头大哥

解决方案 »

  1.   


    可以用shell替换,或者直接用python替换里面的 ###3 字符串
      

  2.   

    cat delete.sql | sed 's/^###//g;s/DELETE FROM/replace into/g;s/WHERE/select/g;s/\/\*/,\/\*/g' | sed -r 's/(@3.*),/\1;/g' | sed 's/@[1-9]=//g' > 1.sql
      

  3.   

    给你一个我用PHP写的SQL语句执行类,用于执行SQL文件,恢复数据库用的。你可以参考一下。class CHXSQLParser {    protected $sqlFile = null;
        protected $delimiter = ';';
        protected $sCode;
        protected $totalSize = 0;
        protected $progressed = 0;    public function __construct( $fileName, $code = 'utf-8' )
        {
            if( file_exists( $fileName )) {
                $this->totalSize = filesize( $fileName );
                $this->sqlFile = fopen($fileName, 'r');
            }
            $this->sCode = $code;
        }    public function __destruct()
        {
            if( $this->sqlFile )
                fclose( $this->sqlFile );
        }    public function getProgress()
        {
            return ( $this->progressed / $this->totalSize );
        }    public function runSqlfile( PDO& $pdo, $callBack = null, $progressSize = 1024 )
        {
            $curr = $this->progressed;
            while( $comm = $this->getNextCommand()) {
                if( $comm ) {
                    $pdo->exec($comm);
                    if (($callBack != null) && (($this->progressed - $curr) >= $progressSize)) {
                        $curr = $this->progressed;
                        call_user_func_array($callBack, array($this->progressed, $this->totalSize));
                    }
                }
            }
            if( $callBack != null ) {
                call_user_func_array( $callBack, array( $this->progressed, $this->totalSize ));
            }
        }    protected function getNextCommand()
        {
            $relCmd = '';
            while (!feof($this->sqlFile)) {            $comm = fgets($this->sqlFile);
                $this->progressed += strlen( $comm );            if ($this->sCode != 'utf-8')
                    $comm = iconv($this->sCode, 'utf-8//TRANSLIT', $comm);            $arrMatched = array();
                if (preg_match('/^\s*[\r\n]+/i', $comm )) {
                    continue;
                }
                else if (preg_match('/^\s*delimiter\s+([^\s]*)\s*[\r\n]+/i', $comm, $arrMatched)) {
                    $this->delimiter = $arrMatched[1];
                    continue;
                }
                else if (preg_match('/^\s*--(.*)[\r\n]+/i', $comm )) {
                    continue;
                }
                else if (preg_match('/.*' . $this->delimiter. '\s*[\r\n]+$/i', $comm )) {
                    return $relCmd . $comm;
                }
                else {
                    $relCmd .= $comm;
                }
            }
            return false;
        }
    }
      

  4.   


    说明一下,其中的
    public function getProgress()
        {
            return ( $this->progressed / $this->totalSize );
        }
    以及runSqlfile中的 $callBack参数,都是用来显示处理进度用的,给用户呈现处理进度的,跟核心功能无关
      

  5.   

    对于恢复来说,你需要做 decode 啊,直接把结果重定向到 mysql 执行就行了
    mysqlbinlog gbichot2-bin.000007 gbichot2-bin.000008 | mysql
      

  6.   

    当然,你直接把结果放到文件,然后用 mysql 执行这个文件也是可以恢复的,但试了自己的程序读取和执行文件没效果
      

  7.   

    注意是不要做 decode