MySQL · 特性分析 · 數據一樣checksum不一樣
背景
有一個特殊環境需要進行人肉遷移數據,對比了表裏的數據一模一樣,但是無論如何checksum就是不一致,那麼問題出在哪裏呢?
問題排查
數據是否一致
眼睛都把屏幕盯穿了,也沒發現不一致的數據。
導出數據的方式
checksum還是不一致,所以這個原因排除。
MySQL版本
嗯,這個確實不一樣,源端是5.5,目的端是5.6,但是這個是checksum函數不一樣嗎?還是表的結構變了?谘詢了內核的同學,說checksum的源代碼沒變啊,接下來那應該是表結構變了?通過查手冊發現:
The checksum value depends on the table row format. If the row format changes, the
checksum also changes. For example, the storage format for temporal types such as
TIME, DATETIME, and TIMESTAMP changes in MySQL 5.6 prior to MySQL 5.6.5, so if a
5.5 table is upgraded to MySQL 5.6, the checksum value may change.
既然這樣了,那我們就來驗證下是不是因為datetime的問題。
嗯,確實是datetime的格式導致的。
總結
這個問題總結來說是因為MySQL5.5和5.6的時間存儲格式有變化,導致了checksum不一樣。
這個問題知道原因後覺得非常簡單,但是排查起來卻不是那麼簡單的。遇到問題不要慌,理出要查的1,2,3,然後用排除法一步一步驗證就能知道問題在哪裏了。
附一詳細步驟如下:
源端:
1.備份結構和數據
mysqldump -h127.0.0.1 -P源端口 -u root --default-character-set=utf8 -B drcdb>src_lingluo.sql
2.拷貝文件
scp src_lingluo.sql lingluo.sss@目的端:/tmp
目的端:
3.把數據導進去
mysql>set names utf8;source /tmp/src_lingluo.sql;
4.在兩邊取checksum
sh get_checksum.sh
5.目的端:
scp table_checksum.txt lingluo.sss@目的端:/tmp
6.vimdiff 對比文件
如果文件內容一樣的話,說明數據一樣
附二get_checksum.sh
#!/bin/sh
port=$1
table_list='`mysql -h127.0.0.1 -P${port} -u root -A -N << EOF | tail -n +3
use db_name;show tables
EOF`'
for i in ${table_list}
do
table_cs='`mysql -h127.0.0.1 -P${port} -u root -A -N -D db_name -e \"checksum
table ${i};\"`'
echo ${table_cs} >> table_checksum.txt
done
最後更新:2017-10-21 09:04:46