A机器和B机器上都跑有数据库服务,B机器上的数据库是从A机器上导过来的,现在线上程序连接A机器数据库时正常,连接B机器异常,初步怀疑是数据库导入时记录不完整造成的。数据库中一共有100个多表,不可能一一肉眼比对其值.于是想出了一个办法,通过脚本来比对其值

1)初步的整理

A机器:

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

B机器:

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

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

  1. aaa                                  31          16 
  2. gift_card_base_info                  10         851 
  3. gift_card_use_info                    6         252 
  4. ms_log                                6        5827 
  5. ms_users                              7         548 
  6. ms_users_friends                      8         918 
  7. sph_counter                           2           1 
  8. who_account_log                      10      692650 
  9. who_active                            7          94 
  10. who_active_vote                      11          40 
  11. who_ad                               13           1 
  12. who_ad_custom                         7           0 
  13. who_ad_position                       6           0 
  14. who_admin_action                      3         113 
  15. who_admin_log                         5      423922 
  16. who_admin_message                     9           0 
  17. who_admin_user                       13         176 
  18. who_adsense                           3           0 
  19. who_affiliate_log                     8        6154 
  20. who_agency                            3           0 
  21. who_area_region                       3        1780 
  22. who_article                          13        1839 
  23. who_article_cat                       8          33 
  24. who_attr_composit                     3         450 
  25. who_attribute                        10          12 
  26. who_auction_log                       5           0 
  27. who_auto_manage                       4           0 
  28. who_back_goods                        8           0 
  29. who_back_order                       31           4 
  30. who_bak                              31          52 
  31. who_bak1                             31          21 
  32. who_bak2                             31          38 
  33. who_bak3                             31          51 
  34. who_bak4                             31          82 
  35. who_bak5                             31          39 
  36. who_bonus_type                       11           0 
  37. who_booking_goods                    15      292713 
  38. …………………………………………………………………………………………………………………………………………………………………………………………………………

b.txt

  1. aaa                                  31          16 
  2. gift_card_base_info                  10         851 
  3. gift_card_use_info                    6         265 
  4. ms_log                                6        5827 
  5. ms_users                              7         548 
  6. ms_users_friends                      8         918 
  7. sph_counter                           2           1 
  8. who_account_log                      10      698600 
  9. who_active                            7          94 
  10. who_active_vote                      11          40 
  11. who_ad                               13           1 
  12. who_ad_custom                         7           0 
  13. who_ad_position                       6           0 
  14. who_admin_action                      3         113 
  15. who_admin_log                         5      430266 
  16. who_admin_message                     9           0 
  17. who_admin_user                       13         177 
  18. who_adsense                           3           0 
  19. who_affiliate_log                     8        6154 
  20. who_agency                            3           0 
  21. who_area_region                       3        1780 
  22. who_article                          13        1845 
  23. who_article_cat                       8          33 
  24. who_attr_composit                     3         450 
  25. who_attribute                        10          12 
  26. who_auction_log                       5           0 
  27. who_auto_manage                       4           0 
  28. who_back_goods                        8           0 
  29. who_back_order                       31           4 
  30. who_bak                              31          52 
  31. who_bak1                             31          21 
  32. who_bak2                             31          38 
  33. who_bak3                             31          51 
  34. who_bak4                             31          82 
  35. who_bak5                             31          39 
  36. who_bonus_type                       11           0 
  37. who_booking_goods                    15      292674 
  38. …………………………………………………………………………………………………………………………………………………………………………………………………………

2)还是利用perl脚本来对比两个文件的值吧,方便

code:

  1. #!/usr/bin/perl -w 
  2.  
  3. use strict; 
  4.  
  5. my (%hashA,%hashB) = (); 
  6.  
  7. open my $file,'<','record_194.txt' or die "$!\n"
  8.  
  9. while (<$file>) { 
  10.        chomp; 
  11.        my @arrayA = split /\s+/; 
  12.        $hashA{$arrayA[0]} = $arrayA[2]; 
  13.  
  14. close $file
  15.  
  16. open $file,'<','record_2.txt' or die "$!\n"
  17.  
  18. while (<$file>) { 
  19.        chomp; 
  20.        my @arrayB = split /\s+/; 
  21.        $hashB{$arrayB[0]} = $arrayB[2]; 
  22.  
  23. close $file
  24.  
  25. foreach my $table (sort keys %hashA) { 
  26.  
  27.         if (exists $hashB{$table}) { 
  28.  
  29.            if ($hashA{$table} != $hashB{$table}) { 
  30.                 print $table,"\n"
  31.            } 
  32.  
  33.         } 
  34.  

运行即可