我找到了一个解决方案,但不能直接从 SQL 完成,所以我决定编写以下脚本:
<?php
function tablesCheckSum($host, $database, $user, $pass) {
try {
$db = new PDO("mysql:host={$host}; dbname={$database}", $user, $pass);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$result = $db->query('SHOW TABLES;')->fetchAll();
// Construct tables
foreach($result as $value) {
$row = $db->query("CHECKSUM TABLE `" . $value[0] . "`")->fetch();
$tables[substr($row[0], strlen($database) + 1)] = $row[1];
}
} catch(PDOException $ex) {
print_r($ex->getMessage());
}
return $tables;
}
// Collecting all the
$db1 = tablesCheckSum('host1', 'db1', 'root', 'pass');
$db2 = tablesCheckSum('host2', 'db1', 'root', 'pass');
?>
<table>
<tr>
<td>Table Name</td>
<td>DB1 Checksum</td>
<td>DB2 Checksum</td>
<td>Matched?</td>
</tr>
<?php
foreach($db1 as $key => $value) {
?>
<tr>
<td><?php echo $key; ?></td>
<td><?php echo $value; ?></td>
<td><?php echo $db2[$key]; ?></td>
<td><?php echo ($value == $db2[$key])?'True':'False'; ?></td>
</tr>
<?php
}
?>
</table>