自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

MySQL大表備份的簡單方法

數(shù)據(jù)庫 MySQL
MySQL數(shù)據(jù)庫的表備份應(yīng)該說是我們最常用的操作之一了,但是面對(duì)超過4G以上的表,備份的策略就要我們仔細(xì)去思考了。

MySQL大表備份是一個(gè)我們常見的問題,下面就為您介紹一個(gè)MySQL大表備份的簡單方法,希望對(duì)您學(xué)習(xí)MySQL大表備份方面能有所幫助。

這里所說的大表是超過4G以上的表,我目前見到過***為60多G的單表,對(duì)于這種表每天一個(gè)全備可以說是一件很痛苦的事。
那么有沒有辦法,可以實(shí)現(xiàn)一個(gè)全備加增量的備份呢。
答案當(dāng)然是有的。

在常規(guī)環(huán)境直可以用全備加binlog一同保存。
這種環(huán)境大多可以用一個(gè)Slave上進(jìn)行備份操作。

思路:
先停止Slave的同步,刷新buffer,對(duì)于Innodb 如果想直接拷貝還需要把innodb_max_dirty_pages_pct這個(gè)值置為零,然后在執(zhí)行一次flush tables;
就可以cp了。如果是Dump出來可以這這樣做。

這個(gè)方案目前來看也是比較***的,但一個(gè)并發(fā)力度大的應(yīng)用一天的Binlog有可能能達(dá)到50G-60G,這樣的系統(tǒng)開Binlog可以說是對(duì)系統(tǒng)的IO性能及整體性能都有早影響。

另一種方案就是基于表的上數(shù)據(jù)的羅輯變化進(jìn)行備份。
主體思想:全備加邏輯備份。
邏輯備份:當(dāng)有數(shù)據(jù)插入時(shí),利用觸發(fā)器同時(shí)寫入另一個(gè)表,當(dāng)數(shù)據(jù)更新時(shí),我們同時(shí)記錄一下,更新后的數(shù)據(jù)情況到另一個(gè)表。
當(dāng)有刪除操作時(shí),只需要記錄一下,刪除的主建ID就行。

例子:
要備份的表:

  1. CREATE TABLE `wubx` (  `id` int(11) NOT NULL auto_increment,   
  2. `user_id` int(11) NOT NULL default '0',   
  3. `friend_id` int(11) NOT NULL default '0',   
  4. `dir_id` int(11) NOT NULL default '0',   
  5. `created` int(11) NOT NULL default '0',   
  6. UNIQUE KEY `id` (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;  

對(duì)于這個(gè)表我們需要建一個(gè)記錄有新數(shù)據(jù)變化的表為:

  1. mysql> create table wubx_ii like wubx;  
  2. Query OK, 0 rows affected (0.00 sec)  
  3.  
  4. mysql> create table wubx_uu like wubx;  
  5. Query OK, 0 rows affected (0.00 sec)  
  6. mysql> create table wubx_dd ( id int(11));  
  7. Query OK, 0 rows affected (0.00 sec)  
  8.  

建立相應(yīng)的觸發(fā)程器

  1. 記錄insert的操作:  
  2. delimiter //  
  3. create trigger wubx_ii after insert on wubx for each row begin insert into wubx_ii set id=new.id,user_id=new.user_id,friend_id=new.friend_id,dir_id=new.dir_id,created=new.created; end//  
  4. 記錄update的操作:  
  5. create trigger wubx_uu after update on wubx for each row begin replace into wubx_uu set id=new.id,user_id=new.user_id,friend_id=new.friend_id,dir_id=new.dir_id,created=new.created; end//  
  6. 記錄刪除的操作:  
  7. create trigger wubx_dd after delete on wubx for each row begin insert into wubx_dd values(old.id); end//  
  8.  
  9. delimiter ;  
  10.  

操作:
先備份原始表wubx里的數(shù)據(jù):
進(jìn)行:

  1. insert into wubx values(”,1,10,1,1198464252);  
  2. insert into wubx values(”,1,11,1,1198464252);  
  3. insert into wubx values(”,1,2,1,1198464252);  
  4. insert into wubx values(”,2,10,1,1198464252);  
  5. insert into wubx values(”,2,12,1,1198464252);  
  6. insert into wubx values(”,3,12,1,1198464252);  
  7. update wubx set dir_id=5 where user_id=3;  
  8. update wubx set dir_id=4 where user_id=3;  
  9. delete from wubx where user_id=2 and friend_id=12;  

現(xiàn)在要實(shí)現(xiàn)增量備份:
取出insert的操作:

  1. mysql -e ” select concat(‘replace into wubx set id=’,id,’,user_id=’,user_id,’,friend_id=’,friend_id,’,dir_id=’,dir_id,’,created=’,created,’;') from wubx_ii;”>>backup_ii.sql  

取出update的操作:

  1. mysql -e ” select concat(‘update wubx set user_id=’,user_id,’,friend_id=’,friend_id,’,dir_id=’,dir_id,’,created=’,created,’ where id=’,id,’;') from wubx_uu;”>>backup_uu.sql  

取出delete的操作:

  1. mysql -e “select concat(‘delete from wubx where id=’,id,’;') from wubx_dd”>>backup_dd.sql  

這樣利用這些邏輯的備份加是完畢備份恢復(fù)到當(dāng)前恢復(fù)點(diǎn)就很容易了。這里不演示。

這個(gè)操作***用一個(gè)程序完成,當(dāng)取完羅輯備份后,做一個(gè)標(biāo)記點(diǎn)去清楚備份完的數(shù)據(jù),以保證,邏輯記錄表里的數(shù)據(jù)量比較少是正確的。
 


 

【編輯推薦】

MySQL分表處理的實(shí)現(xiàn)方法

MySQL授權(quán)表使用示例

MySQL多表刪除的實(shí)現(xiàn)

MySQL獨(dú)立表空間的優(yōu)缺點(diǎn)

MySQL MyISAM表結(jié)構(gòu)的恢復(fù)

責(zé)任編輯:段燃 來源: 互聯(lián)網(wǎng)
相關(guān)推薦

2011-04-06 09:09:17

MySQL數(shù)據(jù)庫備份

2010-05-17 10:24:44

MySQL數(shù)據(jù)庫

2009-08-19 13:06:37

C#操作注冊(cè)表

2010-08-06 13:23:58

NFS配置

2010-07-20 14:07:31

更改TELNET端口

2010-06-08 17:46:31

OpenSUSE安裝

2010-03-15 14:10:34

ubuntu系統(tǒng)

2010-09-30 14:01:38

2009-08-12 16:47:36

C#轉(zhuǎn)換農(nóng)歷

2009-08-24 14:14:56

C#導(dǎo)入Excel表

2011-05-18 13:37:26

LINQ

2010-03-03 13:56:43

Linux ubant

2009-02-05 14:48:51

跳出循環(huán)多層循環(huán)編程

2023-03-16 08:14:57

2011-03-02 10:01:15

2011-03-01 09:53:18

卸載PureFTPd

2010-08-25 20:37:05

DHCP服務(wù)器

2023-08-26 21:06:25

2010-08-27 10:40:58

Linux DHCP服

2011-05-07 10:07:41

Windows 8激活
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)