国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費(fèi)電子書(shū)等14項(xiàng)超值服

開(kāi)通VIP
mysql 利用binlog增量備份,還原實(shí)例

一,什么是增量備份

增量備份,就是將新增加的數(shù)據(jù)進(jìn)行備份。假如你一個(gè)數(shù)據(jù)庫(kù),有10G的數(shù)據(jù),每天會(huì)增加10M的數(shù)據(jù),數(shù)據(jù)庫(kù)每天都要備份一次,這么多數(shù)據(jù)是不是都要備份呢?還是只要備份增加的數(shù)據(jù)呢,很顯然,我只要備份增加的數(shù)據(jù)。這樣減少服務(wù)器的負(fù)擔(dān)。


二,啟用binlog

vi my.cnf

log-bin=/var/lib/mysql/mysql-bin.log,如果是這樣的話log-bin=mysql-bin.log默認(rèn)在datadir目錄下面

[root@BlackGhost mysql]# ls |grep mysql-bin
mysql-bin.000001
mysql-bin.000002
mysql-bin.000003
mysql-bin.000004
mysql-bin.000005
mysql-bin.000006
mysql-bin.index

啟動(dòng)后會(huì)產(chǎn)生mysql-bin這樣的文件,每啟動(dòng)一次,就會(huì)增加一個(gè)或者多個(gè)。

mysql-bin.000002這樣文件存放的是數(shù)據(jù)庫(kù)每天增加的數(shù)據(jù),所有數(shù)據(jù)庫(kù)的數(shù)據(jù)增量都在這里面。

三,查看mysql-bin.000002這樣的文件里面到底是什么東西

[root@BlackGhost mysql]# mysqlbinlog   /var/lib/mysql/mysql-bin.000002 > /tmp/add.sql

  1. [root@BlackGhost mysql]# cat /tmp/add.sql   // 下面是根據(jù)mysql-bin生成的文件(部分內(nèi)容)  
  2. /*!40019 SET @@session.max_insert_delayed_threads=0*/;  
  3. /*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;  
  4. DELIMITER /*!*/;  
  5. # at 4  
  6. #100929 21:23:52 server id 1  end_log_pos 106     Start: binlog v 4, server v 5.1.50-log created 100929 21:23:52 at startup  
  7. # Warning: this binlog was not closed properly. Most probably mysqld crashed writing it.  
  8. ROLLBACK/*!*/;  
  9. BINLOG ' 
  10. 6D2jTA8BAAAAZgAAAGoAAAABAAQANS4xLjUwLWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA 
  11. AAAAAAAAAAAAAAAAAADoPaNMEzgNAAgAEgAEBAQEEgAAUwAEGggAAAAICAgC 
  12. '/*!*/;  
  13. # at 106  
  14. #100929 21:29:35 server id 1  end_log_pos 134     Intvar  
  15. SET INSERT_ID=16/*!*/;  
  16. # at 134  
  17. #100929 21:29:35 server id 1  end_log_pos 237     Query    thread_id=1    exec_time=0    error_code=0  
  18. use test/*!*/;           //這里是test數(shù)據(jù)庫(kù)  
  19. SET TIMESTAMP=1285766975/*!*/;  
  20. SET @@session.pseudo_thread_id=1/*!*/;  
  21. SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=1, @@session.unique_checks=1/*!*/;  
  22. SET @@session.sql_mode=0/*!*/;  
  23. SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/;  
  24. /*!\C utf8 *//*!*/;  
  25. SET @@session.character_set_client=33,@@session.collation_connection=33,@@session.collation_server=33/*!*/;  
  26. SET @@session.lc_time_names=0/*!*/;  
  27. SET @@session.collation_database=DEFAULT/*!*/;  
  28. insert into aa(name)values('cccccccccc')  
  29. /*!*/;  
  30. # at 237  
  31. #100929 21:32:21 server id 1  end_log_pos 265     Intvar  
  32. SET INSERT_ID=12/*!*/;  
  33. # at 265  
  34. #100929 21:32:21 server id 1  end_log_pos 370     Query    thread_id=1    exec_time=0    error_code=0  
  35. SET TIMESTAMP=1285767141/*!*/;  
  36. insert into user(name)values('cccccccccc')  
  37. /*!*/;  
  38. # at 370  
  39. #100929 21:35:25 server id 1  end_log_pos 440     Query    thread_id=1    exec_time=0    error_code=0  
  40. SET TIMESTAMP=1285767325/*!*/;  
  41. BEGIN  
  42. /*!*/;  
  43. # at 440  
  44. #100929 21:35:25 server id 1  end_log_pos 468     Intvar  
  45. SET INSERT_ID=45/*!*/;  
  46. # at 468  
  47. #100929 21:35:25 server id 1  end_log_pos 573     Query    thread_id=1    exec_time=0    error_code=0  
  48. use blog/*!*/;             //這里是blog數(shù)據(jù)庫(kù)  
  49. SET TIMESTAMP=1285767325/*!*/;  
  50. insert into city(CityName)values('asdf')  
  51. /*!*/;  
  52. # at 573  
  53. #100929 21:35:25 server id 1  end_log_pos 600     Xid = 205  
  54. COMMIT/*!*/;  
  55. DELIMITER ;  
  56. End of log file  
  57. ROLLBACK /* added by mysqlbinlog */;  
  58. /*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;  

下面還有一個(gè)重要索引文件就是mysql-bin.index

  1. [root@BlackGhost mysql]# cat mysql-bin.index  
  2. ./mysql-bin.000001  
  3. ./mysql-bin.000002  
  4. ./mysql-bin.000003  
  5. ./mysql-bin.000004  
  6. ./mysql-bin.000005  
  7. ./mysql-bin.000006  

四,增量備份和增量還原

1,增量備份

既然我們知道了,mysql里面新增加的數(shù)據(jù)在mysql-bin這樣的文件里面,我們只要把mysql-bin這樣的文件進(jìn)行備份就可以了。

cp /var/lib/mysql/mysql-bin* /data/mysql_newbak/

2,增量還原,講幾個(gè)常用的,比較有用的

a),根據(jù)時(shí)間來(lái)還原 --start-date,--stop-date

[root@BlackGhost mysql]# /usr/local/mysql/bin/mysqlbinlog --start-date="2010-09-29 18:00:00" --stop-date="2010-09-29 23:00:00" /var/lib/mysql/mysql-bin.000002 |mysql -u root -p

根據(jù)條件看一下數(shù)據(jù)

  1. [root@BlackGhost mysql]# /usr/local/mysql/bin/mysqlbinlog  --start-date="2010-09-29 18:00:00"  
  2. --stop-date="2010-09-29 23:00:00"  /var/lib/mysql/mysql-bin.000002  
  3. //下面是部分內(nèi)容,其實(shí)也就是一些對(duì)數(shù)據(jù)進(jìn)行操作的sql語(yǔ)句  
  4. # at 237  
  5. #100929 21:32:21 server id 1  end_log_pos 265     Intvar  
  6. SET INSERT_ID=12/*!*/;  
  7. # at 265  
  8. #100929 21:32:21 server id 1  end_log_pos 370     Query    thread_id=1    exec_time=0    error_code=0  
  9. SET TIMESTAMP=1285767141/*!*/;  
  10. insert into user(name)values('cccccccccc')  
  11. /*!*/;  
  12. # at 370  
  13. #100929 21:35:25 server id 1  end_log_pos 440     Query    thread_id=1    exec_time=0    error_code=0  
  14. SET TIMESTAMP=1285767325/*!*/;  
  15. BEGIN  
  16. /*!*/;  
  17. # at 440  
  18. #100929 21:35:25 server id 1  end_log_pos 468     Intvar  
  19. SET INSERT_ID=45/*!*/;  
  20. # at 468  
  21. #100929 21:35:25 server id 1  end_log_pos 573     Query    thread_id=1    exec_time=0    error_code=0  

b),根據(jù)起始位置來(lái)還原,--start-position,--stop-position

[root@BlackGhost mysql]# /usr/local/mysql/bin/mysqlbinlog --start-position=370 --stop-position=440  /var/lib/mysql/mysql-bin.000002 |mysql -u root -p

//查看插入的內(nèi)容,根a)中是一樣的

[root@BlackGhost mysql]# /usr/local/mysql/bin/mysqlbinlog --start-position=370 --stop-position=440  /var/lib/mysql/mysql-bin.000002

--start-position=370 --stop-position=440 這里面數(shù)字從哪兒來(lái)的呢?
# at 370
#100929 21:35:25 server id 1  end_log_pos 440 Query    thread_id=1    exec_time=0    error_code=0
SET TIMESTAMP=1285767325/*!*/;

上面的紅色加粗的就是,一個(gè)是start-position,一個(gè)是stop-position

c),根據(jù)數(shù)據(jù)庫(kù)名來(lái)進(jìn)行還原 -d

在這里是小寫(xiě)的d,請(qǐng)不要把它和mysqldump中的-D搞混了。哈哈。

[root@BlackGhost mysql]# /usr/local/mysql/bin/mysqlbinlog -d test  /var/lib/mysql/mysql-bin.000002

查看內(nèi)容,請(qǐng)參考a)

d),根據(jù)數(shù)據(jù)庫(kù)所在IP來(lái)分-h(huán)

[root@BlackGhost mysql]# /usr/local/mysql/bin/mysqlbinlog -h 192.1681.102  /var/lib/mysql/mysql-bin.000002

查看內(nèi)容,請(qǐng)參考a)

e),根據(jù)數(shù)據(jù)庫(kù)所占用的端口來(lái)分-P

有的時(shí)候,我們的mysql用的不一定是3306端口,注意是大寫(xiě)的P

[root@BlackGhost mysql]# /usr/local/mysql/bin/mysqlbinlog -p 13306  /var/lib/mysql/mysql-bin.000002

查看內(nèi)容,請(qǐng)參考a)

f),根據(jù)數(shù)據(jù)庫(kù)serverid來(lái)還原--server-id

在數(shù)據(jù)庫(kù)的配置文件中,都有一個(gè)serverid并且同一集群中serverid是不能相同的。

[root@BlackGhost mysql]# /usr/local/mysql/bin/mysqlbinlog --server-id=1  /var/lib/mysql/mysql-bin.000002

查看內(nèi)容,請(qǐng)參考a)

注意:上面的幾個(gè)例子,我都是一個(gè)一個(gè)說(shuō)的,其實(shí)可以排列組合的。例如

[root@BlackGhost mysql]# /usr/local/mysql/bin/mysqlbinlog --start-position="2010-09-29 18:00:00" -d test -h 127.0.0.1 /var/lib/mysql/mysql-bin.000002 |mysql -u root -p

五,后續(xù)

增量備份的時(shí)候,有一點(diǎn)讓人不爽,就是mysql-bin這樣的文件,每啟動(dòng)一次mysql就會(huì)增加一些,如果你不去管他的話,時(shí)間長(zhǎng)了,他會(huì)把你的磁盤(pán)占滿。

./mysqldump --flush-logs -u root  myblog > /tmp/myblog.sql

備份myblog數(shù)據(jù)庫(kù),清除增量備份里面的有關(guān)myblog的數(shù)據(jù)

./mysqldump --flush-logs -u root  --all-databases > /tmp/alldatabase.sql

備份所有數(shù)據(jù)庫(kù),清除增量備份

mysql-bin.index的起索引作用,因?yàn)樵隽康臄?shù)據(jù)不一定在一個(gè)mysql-bin000這樣的文件里面,這個(gè)時(shí)候,我們就要根據(jù)mysql-bin.index來(lái)找mysql-bin這樣的增量文件了。

如果mysql里面做了這樣的配置binlog-do-db=test1,增量備份里面只會(huì)有test1這個(gè)數(shù)據(jù)庫(kù)的數(shù)據(jù)

本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
mysql日志操作
Mysql 日常備份和增量備份腳本
Mysql 備份與恢復(fù)
Mysql binlog備份數(shù)據(jù)及恢復(fù)數(shù)據(jù),學(xué)會(huì)這個(gè),我在也不怕刪庫(kù)跑路啦~
mysqlbinlog 恢復(fù)mysql數(shù)據(jù)
Windows下MySQL日志基本的查看以及導(dǎo)入導(dǎo)出用法教程
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服