mysql誤修改全表記錄怎么辦

這篇文章給大家分享的是有關(guān)MySQL誤修改全表記錄怎么辦的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。

10年積累的網(wǎng)站設(shè)計(jì)、網(wǎng)站制作經(jīng)驗(yàn),可以快速應(yīng)對客戶對網(wǎng)站的新想法和需求。提供各種問題對應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先網(wǎng)站設(shè)計(jì)制作后付款的網(wǎng)站建設(shè)流程,更有元氏免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。

#添加數(shù)據(jù)

insert into testdb1.student(id,name,class,score) value(a,'a',1,45),(2,'b',1,46),(3,'c',2,89),(4,'d',2,90),(5,'e',3,67),(6,'f',3,87),(7,'g',4,77),(8,'h',4,91);

mysql> select * from testdb1.student;

+------+------+-------+-------+

| id   | name | class | score |

+------+------+-------+-------+

|    1 | a    | 1     |    45 |

|    2 | b    | 1     |    46 |

|    3 | c    | 2     |    89 |

|    4 | d    | 2     |    90 |

|    5 | e    | 3     |    67 |

|    6 | f    | 3     |    87 |

|    7 | g    | 4     |    77 |

|    8 | h    | 4     |    91 |

+------+------+-------+-------+

8 rows in set (0.00 sec)

#修改數(shù)據(jù)

update student set score=100;

commit;

mysql> select * from testdb1.student;

+------+------+-------+-------+

| id   | name | class | score |

+------+------+-------+-------+

|    1 | a    | 1     |   100 |

|    2 | b    | 1     |   100 |

|    3 | c    | 2     |   100 |

|    4 | d    | 2     |   100 |

|    5 | e    | 3     |   100 |

|    6 | f    | 3     |   100 |

|    7 | g    | 4     |   100 |

|    8 | h    | 4     |   100 |

+------+------+-------+-------+

8 rows in set (0.00 sec)

mysql> set global read_only=1

mysql> show master status\G

*************************** 1. row ***************************

             File: ray-bin.000004

         Position: 1992

     Binlog_Do_DB:

Binlog_Ignore_DB:

Executed_Gtid_Set:

1 row in set (0.00 sec)

# at 2192是在binlog日志中查到的 

[root@localhost ~]# mysqlbinlog /data/3306/logs/ray-bin.000004 -v -v -S /data/3306/soket/mysql.sock --base64-output=decode-rows | grep -A 15 student | sed -n '/# at 2192/,/COMMIT/p'| sed -n 's\### \\p' | sed "s/\/\*.*\*\///g" | sed 's/`//g'> /tmp/1.txt

[root@localhost ~]# cat /tmp/1.txt

UPDATE testdb1.student

WHERE

  @1=1

  @2='a'

  @3='1'

  @4=45

SET

  @1=1

  @2='a'

  @3='1'

  @4=100

UPDATE testdb1.student

WHERE

  @1=2

  @2='b'

  @3='1'

  @4=46

SET

  @1=2

  @2='b'

  @3='1'

  @4=100

UPDATE testdb1.student

WHERE

  @1=3

  @2='c'

  @3='2'

  @4=89

SET

  @1=3

  @2='c'

  @3='2'

  @4=100

UPDATE testdb1.student

WHERE

  @1=4

  @2='d'

  @3='2'

  @4=90

SET

  @1=4

  @2='d'

  @3='2'

  @4=100

UPDATE testdb1.student

WHERE

  @1=5

  @2='e'

  @3='3'

  @4=67

SET

  @1=5

  @2='e'

  @3='3'

  @4=100

UPDATE testdb1.student

WHERE

  @1=6

  @2='f'

  @3='3'

  @4=87

SET

  @1=6

  @2='f'

  @3='3'

  @4=100

UPDATE testdb1.student

WHERE

  @1=7

  @2='g'

  @3='4'

  @4=77

SET

  @1=7

  @2='g'

  @3='4'

  @4=100

UPDATE testdb1.student

WHERE

  @1=8

  @2='h'

  @3='4'

  @4=91

SET

  @1=8

  @2='h'

  @3='4'

  @4=100

[root@localhost ~]# cat column.txt

id

name

class

score

[root@localhost ~]# cat getSQL.sh

#!/bin/bash

# by ray

iswhere=1   #判斷循環(huán)的行的位置,1表示在where后,0表示不再where后

colNum=0    #計(jì)算列數(shù),一般在binlog日志內(nèi)第一列為@1,第二列為@2一次類推

whereNum=0  #判斷where后面字段出現(xiàn)的次數(shù),便于拼接字符串,第一次出現(xiàn)不適用都會(huì),第二次以后使用逗號(hào)拼接

setNum=0 #判斷set后面字段出現(xiàn)的次數(shù),便于拼接字符串,第一次出現(xiàn)不適用都會(huì),第二次以后使用逗號(hào)拼接

replaceColumn(){   #把@開頭的列替換為列配置文件內(nèi)的列,安配置文件的順序執(zhí)行

     cat $1 | while read line

     do

          colNum=$[${colNum}+1]

          sed -i "s/@${colNum}/${line}/g" ./execSQL.sql  #替換列

     done

}

getSQL(){   #獲取sql

     sql1=''

     sql_result=''

     sql_condition=''

     while read line #讀取處理過的binlog日志

     do

          if [[ ${line} =~ 'UPDATE' ]];then     #匹配是否update

               if [ "${sql1}" != "" ];then

                    echo ${sql1}' '${sql_result}' '${sql_condition}';' >> ./execSQL.sql  #打印sql

                    sql1=''

                    sql_result=''

                    sql_condition=''

                    whereNum=0

                    setNum=0

               fi

             sql1=${line}        #拼接sql字符串,獲取update

        elif [[ ${line} =~ 'WHERE' ]];then

             sql_condition=${line}   #拼接字符串,把binlog日志內(nèi)where后面內(nèi)容

             iswhere=1               #判斷是否為where,因?yàn)橐褀here和set后面的內(nèi)容互換

        elif [[ ${line} =~ 'SET' ]];then

             sql_result=' SET '${sql_result} #拼接字符串

             iswhere=0

        elif [[ ${iswhere} -eq 1 ]];then            #1為where后面,把binlog日志where后面的內(nèi)容拼接到sql的set后

             if [[ ${whereNum} -eq 0 ]];then              #判斷where字符串后的字符串是否一次出現(xiàn)

                  sql_result=${sql_result}' '${line}

                  whereNum=1                    #設(shè)置為1,表示不是第一次出現(xiàn)

             elif [[ ${whereNum} -eq 1 ]];then

                  sql_result=${sql_result}', '${line}

             fi

        elif [[ ${iswhere} -eq 0 ]];then           #判斷是否為set后面的字符串

             if [[ ${setNum} -eq 0 ]];then               #判斷set字符串后的字符串是否一次出現(xiàn)

                  sql_condition=${sql_condition}' '${line}

                  setNum=1                     #設(shè)置為1,表示不是第一次出現(xiàn)

             elif [[ ${setNum} -eq 1 ]];then

                  sql_condition=${sql_condition}' and '${line}

             fi

        fi

     done < $1   #把文件用while循環(huán)讀取每一行

     echo ${sql1}' '${sql_result}' '${sql_condition}';' >> ./execSQL.sql    #最后一行退出循環(huán),所以要打印最后一行

     echo "commit;" >> ./execSQL.sql

     replaceColumn $2

}

#腳本的入口,調(diào)用函數(shù)獲取內(nèi)容

if [ -e $1 ];then  #判斷第一個(gè)參數(shù)是否為文件

     getSQL $1 $2

else

    echo $1' is not a file!!'

fi

[root@localhost ~]# bash getSQL.sh '/tmp/1.txt' "./column.txt"

mysql> select * from testdb1.student;

+------+------+-------+-------+

| id   | name | class | score |

+------+------+-------+-------+

|    1 | a    | 1     |   100 |

|    2 | b    | 1     |   100 |

|    3 | c    | 2     |   100 |

|    4 | d    | 2     |   100 |

|    5 | e    | 3     |   100 |

|    6 | f    | 3     |   100 |

|    7 | g    | 4     |   100 |

|    8 | h    | 4     |   100 |

+------+------+-------+-------+

8 rows in set (0.00 sec)

[root@localhost ~]# mysql -uroot -p123456 -S /data/3306/soket/mysql.sock < /root/execSQL.sql

mysql: [Warning] Using a password on the command line interface can be insecure.

mysql> select * from testdb1.student;

+------+------+-------+-------+

| id   | name | class | score |

+------+------+-------+-------+

|    1 | a    | 1     |    45 |

|    2 | b    | 1     |    46 |

|    3 | c    | 2     |    89 |

|    4 | d    | 2     |    90 |

|    5 | e    | 3     |    67 |

|    6 | f    | 3     |    87 |

|    7 | g    | 4     |    77 |

|    8 | h    | 4     |    91 |

+------+------+-------+-------+

8 rows in set (0.00 sec)

感謝各位的閱讀!關(guān)于“mysql誤修改全表記錄怎么辦”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

分享題目:mysql誤修改全表記錄怎么辦
瀏覽路徑:http://muchs.cn/article28/pisjcp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、微信公眾號(hào)虛擬主機(jī)、域名注冊、搜索引擎優(yōu)化、網(wǎng)站營銷

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)