天天看點

對比兩個資料庫的記錄數

   A機器和B機器上都跑有資料庫服務,B機器上的資料庫是從A機器上導過來的,現線上上程式連接配接A機器資料庫時正常,連接配接B機器異常,初步懷疑是資料庫導入時記錄不完整造成的。資料庫中一共有100個多表,不可能一一肉眼比對其值.于是想出了一個辦法,通過腳本來比對其值

1)初步的整理

A機器:

mysqlshow -uroot -p db_name --count >> a.txt 

B機器:

mysqlshow -uroot -p db_name --count >>b.txt 

整理a.txt中内容部分如下:

aaa                                  31          16 

gift_card_base_info                  10         851 

gift_card_use_info                    6         252 

ms_log                                6        5827 

ms_users                              7         548 

ms_users_friends                      8         918 

sph_counter                           2           1 

who_account_log                      10      692650 

who_active                            7          94 

who_active_vote                      11          40 

who_ad                               13           1 

who_ad_custom                         7           0 

who_ad_position                       6           0 

who_admin_action                      3         113 

who_admin_log                         5      423922 

who_admin_message                     9           0 

who_admin_user                       13         176 

who_adsense                           3           0 

who_affiliate_log                     8        6154 

who_agency                            3           0 

who_area_region                       3        1780 

who_article                          13        1839 

who_article_cat                       8          33 

who_attr_composit                     3         450 

who_attribute                        10          12 

who_auction_log                       5           0 

who_auto_manage                       4           0 

who_back_goods                        8           0 

who_back_order                       31           4 

who_bak                              31          52 

who_bak1                             31          21 

who_bak2                             31          38 

who_bak3                             31          51 

who_bak4                             31          82 

who_bak5                             31          39 

who_bonus_type                       11           0 

who_booking_goods                    15      292713 

…………………………………………………………………………………………………………………………………………………………………………………………………………

b.txt

gift_card_use_info                    6         265 

who_account_log                      10      698600 

who_admin_log                         5      430266 

who_admin_user                       13         177 

who_article                          13        1845 

who_booking_goods                    15      292674 

2)還是利用perl腳本來對比兩個檔案的值吧,友善

code:

#!/usr/bin/perl -w 

use strict; 

my (%hashA,%hashB) = (); 

open my $file,'<','record_194.txt' or die "$!\n"; 

while (<$file>) { 

       chomp; 

       my @arrayA = split /\s+/; 

       $hashA{$arrayA[0]} = $arrayA[2]; 

close $file; 

open $file,'<','record_2.txt' or die "$!\n"; 

       my @arrayB = split /\s+/; 

       $hashB{$arrayB[0]} = $arrayB[2]; 

foreach my $table (sort keys %hashA) { 

        if (exists $hashB{$table}) { 

           if ($hashA{$table} != $hashB{$table}) { 

                print $table,"\n"; 

           } 

        } 

運作即可

本文轉自dongfang_09859 51CTO部落格,原文連結:http://blog.51cto.com/hellosa/504677,如需轉載請自行聯系原作者