MySQL 資料對比
MySQL資料對比是常見的日常任務,使用pt-checksum 只能對比主從複製的資料,並且效率和使用上也不太方便。
因此寫了一個對比的工具mydiff,經過測試對比50張500萬的表,能在30s內完成。
要求,mysql 表必須有自增主鍵,對比期間不會加鎖。對於不是非常非常高併發的主從複製,也可以進行資料的對比。
可以非常方便的加入到日常的定時任務中。建議使用單獨的機器執行對比,並把結果儲存在單獨的資料庫例項中。
使用:在db.conf.json中指定需要對比的資料庫資訊。以及儲存結果的資料庫資訊即可。
CheckTables 可以指定全部對比,也可以指定對位元定的表
{ "SourceMySQLHost": "127.0.0.1", "SourceMySQLPort": 20996, "SourceMySQLDB": "test", "SourceMySQLUser": "msandbox", "SourceMySQLUserPassword": "msandbox", "TargetMySQLHost": "127.0.0.1", "TargetMySQLPort": 20996, "TargetMySQLDB": "test2", "TargetMySQLUser": "msandbox", "TargetMySQLUserPassword": "msandbox", "ChecksumHost": "127.0.0.1", "ChecksumPort": 20996, "ChecksumDB": "test", "ChecksumUser": "msandbox", "ChecksumPassword": "msandbox", "CheckTables":"*", "ChunkSize":2000, "CommitLimit":1000 --批次提交crc結果數量 }
測試輸出:
go run *.go 2022-01-13 09:49:33 INFO Read config: db.conf.json table not in target:[accounts accounts2 accounts3 accounts_del active_node crc_1 crc_2 crc_result cycle_count_task_assignment_rule_v2_tab dba_test experiment_config_tab inbound_mbn_tab node_health node_health_history rs sbtest5_bak t_checksum user_bak user_infos] table not in source:[sbtest5 user2] tables to be compare:[user] begin compare table:%s int db tables user test test2 Getting nothing in the write queue. Sleeping... diff values chunk len: 5 begin to find diff rows in table: test user compare diff.......... Table rows in test.user chunk_num:5854 crc is not same,check end_pk row Getting nothing in the write queue. Sleeping... begin to find diff rows in table: test user compare diff.......... Table rows in test.user chunk_num:7841 crc is not same,check end_pk row Getting nothing in the write queue. Sleeping... begin to find diff rows in table: test user compare diff.......... Table rows in test.user chunk_num:9275 crc is not same,check end_pk row Getting nothing in the write queue. Sleeping... begin to find diff rows in table: test user compare diff.......... Table rows in test.user chunk_num:11507 crc is not same,check end_pk row Getting nothing in the write queue. Sleeping... begin to find diff rows in table: test user compare diff.......... Table rows in test.user chunk_num:13173 crc is not same,check end_pk row Getting nothing in the write queue. Sleeping... table compare elapsed: 25.583540092s done
根據提示查詢對應的chunk,或者直接用sql查詢crc_result表,獲取不一樣的記錄
select * from crc_result where chunk_num=11507; +-------+--------+------------+-----------+----------+--------+----------+-----+---------------------+--------+ | id | dbname | table_name | chunk_num | start_pk | end_pk | crc | cnt | ctime | which | +-------+--------+------------+-----------+----------+--------+----------+-----+---------------------+--------+ | 59719 | test | user | 11507 | 81022 | 81024 | ac9e9b3 | 1 | 2022-01-13 09:49:58 | source | | 59720 | test2 | user | 11507 | 81022 | 81024 | 7573c5a8 | 2 | 2022-01-13 09:49:58 | target | +-------+--------+------------+-----------+----------+--------+----------+-----+---------------------+--------+
select * from crc_result where chunk_num=9275; +-------+--------+------------+-----------+----------+--------+----------+-----+---------------------+--------+ | id | dbname | table_name | chunk_num | start_pk | end_pk | crc | cnt | ctime | which | +-------+--------+------------+-----------+----------+--------+----------+-----+---------------------+--------+ | 55255 | test | user | 9275 | 12457 | 12458 | a2e9ba83 | 1 | 2022-01-13 09:49:56 | source | | 55256 | test2 | user | 9275 | 12457 | 12458 | 67fab947 | 1 | 2022-01-13 09:49:56 | target | +-------+--------+------------+-----------+----------+--------+----------+-----+---------------------+--------+
第一個輸出中看到crc不一樣,cnt也不一樣,說明chunk中的記錄行數不一樣
第二個輸出中看到crc內容不一樣,對應的行內容不一樣
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/25719946/viewspace-2790185/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- MySQL 對比資料庫表結構MySql資料庫
- MySql和SQL Server資料型別 對比MySqlServer資料型別
- Oracle和MySQL資料庫CTAS等操作對比OracleMySql資料庫
- MySQL和Oracle對比學習之資料字典後設資料MySqlOracle
- 【原創】MySQL和PostgreSQL 匯入資料對比 薦MySql
- MySQL資料庫資料一致性比對的方案的探討MySql資料庫
- ClickHouse 與 MySQL 資料庫適用場景對比總結MySql資料庫
- 雲時代,MySQL到ClickHouse資料同步產品對比推薦MySql
- 資料庫對比系列之三(PG事務與MySQL事務)資料庫MySql
- 三款開源關係型資料庫對比:MySQL、PostgreSQL、SQLiteXP資料庫MySqlSQLite
- TSPython操作MySQL MongoDB Oracle三大資料庫深入對比oeePythonMySqlMongoDBOracle大資料資料庫
- Oracle cols_as_rows 比對資料Oracle
- MYSQL引擎的鎖對比MySql
- 5大主流方案對比:MySQL千億級資料線上平滑擴容實戰MySql
- 關於ORACLE 和MYSQL INNODB 觸發髒資料寫的機制對比OracleMySql
- 漫談“資料拆分層次對比”
- Redux 非同步資料流方案對比Redux非同步
- 關聯式資料庫與文件資料庫對比資料庫
- TIDB和MySQL效能對比TiDBMySql
- MySQL高可用架構對比MySql架構
- MYSQL如何比對版本號字串MySql字串
- MySQL 半同步 與Raft對比MySqlRaft
- mysql對資料庫表建索引MySql資料庫索引
- MySQL大量資料入庫的效能比較MySql
- 小程式與Vue對比·資料繫結Vue
- 手寫一個業務資料比對庫
- 資料庫選型比對 Oracle vs sqlserver資料庫OracleSQLServer
- 幾款分散式資料庫的對比分散式資料庫
- i美股:中外線上旅遊資料對比
- 8 種 NoSQL 資料庫系統對比SQL資料庫
- 8種NoSQL資料庫系統對比SQL資料庫
- 三個流行MySQL分支的對比MySql
- BI工具對比|Smartbi與億信ABI兩款BI資料看板軟體對比
- 【MySQL】資料庫字元校對規則MySql資料庫字元
- MySQL 一種比較經濟的資料庫MySql資料庫
- 大資料教程系列之Kafka和activemq對比大資料KafkaMQ
- PostgreSQL 資料庫結構(DDL)比對工具 pgquarrelSQL資料庫
- 主備資料庫狀態手工比對(一)資料庫