Home > Database > Mysql Tutorial > body text

通过BINLOG日志查找指定日期的SQL语句_MySQL

WBOY
Release: 2016-06-01 13:36:01
Original
1884 people have browsed it

bitsCN.com

通过BINLOG日志查找指定日期的SQL语句

 

在95数据库服务器查找posts.post_thread的postid=1652971,在 2013-02-24 日执行的对数据有修改的SQL语句

   www.bitsCN.com  

# grep datadir /etc/mysql/my.cnf

datadir                                         = /ssd/mysql

# cd /ssd/mysql

# mysql -A -e "show master status " | awk 'NR==2{print $1}' | awk -F. '{print $1}'

mysqld-bin  这里的内容为mysqlbinlog日志的前缀,因为如果是从数据库,还会有replylog.

找Binlog日志,取前一天的最后一个日志,以及24号创建的所有的日志

# ls –l

-rw-rw---- 1 mysql mysql 1075302338 Feb 22 04:00 mysqld-bin.000035

-rw-rw---- 1 mysql mysql 1092104643 Feb 22 14:10 mysqld-bin.000036

-rw-rw---- 1 mysql mysql 1073742083 Feb 23 00:06 mysqld-bin.000037

-rw-rw---- 1 mysql mysql 1073742129 Feb 23 05:26 mysqld-bin.000038

-rw-rw---- 1 mysql mysql 1073742102 Feb 23 15:30 mysqld-bin.000039

-rw-rw---- 1 mysql mysql 1073742095 Feb 24 01:36 mysqld-bin.000040

-rw-rw---- 1 mysql mysql 1076478877 Feb 24 09:00 mysqld-bin.000041

-rw-rw---- 1 mysql mysql 1087015180 Feb 24 15:00 mysqld-bin.000042

-rw-rw---- 1 mysql mysql 1073742090 Feb 25 01:31 mysqld-bin.000043

-rw-rw---- 1 mysql mysql 1073742037 Feb 25 08:47 mysqld-bin.000044

-rw-rw---- 1 mysql mysql 1073741919 Feb 25 15:45 mysqld-bin.000045

-rw-rw---- 1 mysql mysql 1073742218 Feb 26 03:36 mysqld-bin.000046

-rw-rw---- 1 mysql mysql  689342483 Feb 26 09:45 mysqld-bin.000047

这个时候我们需要分析的binlog日志为mysqld-bin.0000{39,4[0-2]}

# ls mysqld-bin.0000{39,4[0-2]}

mysqld-bin.000039  mysqld-bin.000040  mysqld-bin.000041  mysqld-bin.000042

# cat /root/findsql.sh

#!/bin/bash

 

BINLOGDIR=`cat /etc/mysql/my.cnf | grep datadir | awk '{print $3}'`

MYSQL="/usr/bin/mysql -A -e "

DATABASE="posts"

cd "${BINLOGDIR}"

 

 

BINLOGS=`ls mysqld-bin.0000{39,4[0-2]}`

for i in ${BINLOGS}

do

  ${MYSQL} "show binlog events in '${i}'" | grep "Query">>"${BINLOGDIR}/${i}.sql"

done

 

# chmod 755 /root/findsql.sh

先把binlog日志导出来,别做任何修改。至少这样的语句我们就认识了,然后再来搜索我们需要的语句。  www.bitsCN.com  

# /root/findsql.sh

# ls mysqld-bin.0000{39,4[0-2]}.sql

mysqld-bin.000039.sql  mysqld-bin.000041.sql

mysqld-bin.000040.sql  mysqld-bin.000042.sql

 

在这些SQL中对数据进行筛选,这里的筛选需要看你自己的能力了,

会awk sed grep这三个就够了,awk建议必会。grep平时用的也特别多,应该不难。

# cat /root/parsesql.sh

#!/bin/bash

for i in `ls mysqld-bin.0000{39,4[0-2]}.sql`

do

 

        awk -F 'use `posts`; ' '{print $2}' $i | grep -v 'chapterclick=chapterclick' | grep -v 'novelscore=novelscore' | grep 1652971 >> 1652971.sql

        echo $i

done

# chmod 755 /root/parsesql.sh

# /root/parsesql.sh

1652971.sql这个里面的内容就是我们最后的成果

 

bitsCN.com
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template