在迁移数据库的时候做一些必要的验证还是很有用的,比如说迁移前后的数据条数是否一致,数据是否一致,这个时候怎么办呢,验证条数还好说,要是验证数据是否一致呢,对于重要的数据当然要每条都不会有差错,随机抽样验证肯定是不行的,万一遗漏了就麻烦了,而且两张表不再同一台服务器上。这个时候该怎么办呢,有一种方法:
上面这种方法是同时想出来的,也还不错,但我觉得还有改进的余地:
我的想法是这样:
第二种方法的好处就是输出文件会在一定范围缩小,比对方便,但是也有缺点,不能像第一种方法一样直接通过关键字段定位不同数据的位置。
下面是第二种方法效果和的具体代码实现:
'localhost',
'port' => '3306',
'user' => 'root',
'pswd' => '123456',
'charset' => 'utf8',
'tables' => array(
'lagou.pos',
'lagou.pos_innodb',
),
);
//验证格式
if(!$link = mysql_connect($dbinfos['host'].":".$dbinfos['port'],$dbinfos['user'], $dbinfos['pswd']))
{
die("connect to [{$host}@{$port}] failed!!");
}
if(!mysql_query("set names {$dbinfos['charset']}"))
{
die("set charset error : ".mysql_error());
}
foreach ($dbinfos['tables'] as $table)
{
if($is_count)
{
$sql = "select count(*) as nums from {$table}";
$ret = mysql_query($sql);
if(!$ret)
{
die("error : ".mysql_error());
}
$ret = mysql_fetch_array($ret, MYSQL_ASSOC);
echo "{$table} : {$ret['nums']}\n";
}
if($is_md5)
{
$path = $is_md5.DIRECTORY_SEPARATOR.$table;
$sql = "select * from {$table}";
$ret = mysql_query($sql);
$flag = 0;
$fields = '';
while ($_ret = mysql_fetch_array($ret, MYSQL_NUM)) {
$flag++;
while($_ret)
{
$fields .= array_pop($_ret);
}
if($flag % $conbine_num == 0)
{
file_put_contents($path, md5($fields)."\n", FILE_APPEND);
$fields = '';
}
}
if($flag % $conbine_num != 0 && $flag > 0)
{
file_put_contents($path, md5($fields)."\n", FILE_APPEND);
}
echo "save to file info : ".realpath($path)."\n";
}
}