brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: CHECKSUM TABLE tells you whether two database copies of a table hold the same rows

Database
~/tips/tip-database-checksum-table-compare

CHECKSUM TABLE shop.orders;

Compares a table on two servers with one number instead of dumping and diffing it.

After a migration or a restore, I want proof that the table on the new server has the same contents as the old one. Row counts are a weak check, since a changed value keeps the count the same.

CHECKSUM TABLE shop.orders;
+-------------+------------+
| Table       | Checksum   |
+-------------+------------+
| shop.orders | 2564128487 |
+-------------+------------+

Run the same statement on both servers. Equal numbers mean the rows match; a different number means something differs, though it does not say which row. You can list several tables in one statement, separated by commas.

The checksum is calculated by reading every row, so on a large table it takes about as long as a full scan. Run it outside busy hours.

It only works as a comparison when the table definitions match too. The docs warn that the result can differ between server versions and with a different row format, so comparing MariaDB 10.3 against 11.4 may give different numbers for identical data. Compare like with like, or fall back to dumping both sides with one row per line and diffing them. A table that does not exist gives a NULL checksum and the statement still succeeds, which is easy to miss in a script.

Further reading: CHECKSUM TABLE

More Database tips mariadb -e 'CREATE DATABASE IF NOT EXISTS blog' mariadb-dump --no-create-info --insert-ignore shop orders > orders-rows.sql SELECT table_schema, table_name, services: services: All Database tips (53) All topics

Link