两台不同远程机器上的两个MySQL数据库之间的差异(mysqldiff用法说明)

两台不同远程机器上的两个MySQL数据库之间的差异(mysqldiff用法说明),mysql,database,Mysql,Database,我只是想找出两台不同远程机器上的两个mysql数据库之间的差异,我知道mysqldiff可以用于这种用途,我正在使用它。但问题是,我可以找到我(或本地)机器上的数据库与远程数据库之间的差异,但是,如果我们想要找到差异的两个数据库都在两个不同的远程机器上,这是不可能的(或者我不知道如何使用它)。谁能告诉我如何做到这一点 提前感谢这里有一个脚本,我用来比较两个不同的MySQL数据库。要比较两个远程数据库,只需将$db1_host从localhost更改为另一个远程服务器的IP地址 <?php

我只是想找出两台不同远程机器上的两个mysql数据库之间的差异,我知道mysqldiff可以用于这种用途,我正在使用它。但问题是,我可以找到我(或本地)机器上的数据库与远程数据库之间的差异,但是,如果我们想要找到差异的两个数据库都在两个不同的远程机器上,这是不可能的(或者我不知道如何使用它)。谁能告诉我如何做到这一点


提前感谢

这里有一个脚本,我用来比较两个不同的MySQL数据库。要比较两个远程数据库,只需将
$db1_host
localhost
更改为另一个远程服务器的IP地址

<?php
//------------------------------------------------------------------------------
// Define the variables we'll be using.
//------------------------------------------------------------------------------
$db1_con = NULL;
$db1_constraints = array();
$db1_dbname = 'db1';
$db1_host = 'localhost';
$db1_password = 'password1';
$db1_tables = array();
$db1_username = 'username1';

$db2_con = NULL;
$db2_constraints = array();
$db2_dbname = 'db2';
$db2_host = '123.123.123.123';
$db2_password = 'password2';
$db2_tables = array();
$db2_username = 'username2';

//------------------------------------------------------------------------------
// Connect to the databases.
//------------------------------------------------------------------------------
try{
    $db1_con = new PDO("mysql:host=$db1_host;dbname=information_schema", $db1_username, $db1_password);
    $db1_con->setAttribute(PDO::ATTR_EMULATE_PREPARES, FALSE); // Try to use the driver's native prepared statements.
    $db1_con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // Let's use exceptions so we can try/catch errors.
}catch(PDOException $e){
    echo "<p>Connection failed for $db1_host: " . $e->getMessage() . '</p>';
    exit;
}

try{
    $db2_con = new PDO("mysql:host=$db2_host;dbname=information_schema", $db2_username, $db2_password);
    $db2_con->setAttribute(PDO::ATTR_EMULATE_PREPARES, FALSE); // Try to use the driver's native prepared statements.
    $db2_con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // Let's use exceptions so we can try/catch errors.
}catch(PDOException $e){
    echo "<p>Connection failed for $db2_host: " . $e->getMessage() . '</p>';
    exit;
}

if (NULL !== $db1_con && NULL !== $db2_con){
    echo "<h2>Column Analysis</h2>";
    $sql = 'SELECT * FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = ? ORDER BY TABLE_NAME, ORDINAL_POSITION';
    $statement1 = $db1_con->prepare($sql);
    $statement1->bindValue(1, $db1_dbname);

    $statement2 = $db2_con->prepare($sql);
    $statement2->bindValue(1, $db2_dbname);

    if (TRUE === $statement1->execute()){
        while ($row = $statement1->fetch(PDO::FETCH_ASSOC)){
            $db1_tables[$row['TABLE_NAME']][$row['COLUMN_NAME']] = array();
            foreach ($row AS $key => $value){
                $db1_tables[$row['TABLE_NAME']][$row['COLUMN_NAME']][$key] = $value;
            }
        }
    }

    if (TRUE === $statement2->execute()){
        while ($row = $statement2->fetch(PDO::FETCH_ASSOC)){
            $db2_tables[$row['TABLE_NAME']][$row['COLUMN_NAME']] = array();
            foreach ($row AS $key => $value){
                $db2_tables[$row['TABLE_NAME']][$row['COLUMN_NAME']][$key] = $value;
            }
        }
    }

    foreach ($db1_tables AS $table => $info){
        if (!isset($db2_tables[$table])){
            echo "<p>Table <strong>$table</strong> does not exist in the SECOND database!</p>";
        }else{
            foreach ($info AS $column => $data){
                if (!isset($db2_tables[$table][$column])){
                    echo "<p>Column <strong>$column</strong> does not exist in table <strong>$table</strong> in the SECOND database!</p>";
                }else{
                    if (count($data)){
                        foreach ($data AS $key => $value){
                            if ($db1_tables[$table][$column][$key] !== $db2_tables[$table][$column][$key]){
                                echo "<p>Column <strong>$column</strong> in table <strong>$table</strong> has differing characteristics for <strong>$key</strong> (". $db1_tables[$table][$column][$key] ." vs. ". $db2_tables[$table][$column][$key] .")</p>";
                            }
                        }
                    }
                }
            }
        }
    }

    foreach ($db2_tables AS $table => $info){
        if (!isset($db1_tables[$table])){
            echo "<p>Table <strong>$table</strong> does not exist in the FIRST database!</p>";
        }else{
            foreach ($info AS $column => $data){
                if (!isset($db1_tables[$table][$column])){
                    echo "<p>Column <strong>$column</strong> does not exist in table <strong>$table</strong> in the FIRST database!</p>";
                }else{
                    if (count($data)){
                        foreach ($data AS $key => $value){
                            if ($db2_tables[$table][$column][$key] !== $db1_tables[$table][$column][$key]){
                                echo "<p>Column <strong>$column</strong> in table <strong>$table</strong> has differing characteristics for <strong>$key</strong> (". $db2_tables[$table][$column][$key] ." vs. ". $db1_tables[$table][$column][$key] .")</p>";
                            }
                        }
                    }
                }
            }
        }
    }
    echo "<h2>Constraint Analysis</h2>";

    $sql = 'SELECT * FROM information_schema.KEY_COLUMN_USAGE WHERE TABLE_SCHEMA = ? ORDER BY TABLE_NAME, ORDINAL_POSITION';
    $statement1 = $db1_con->prepare($sql);
    $statement1->bindValue(1, $db1_dbname);

    $statement2 = $db2_con->prepare($sql);
    $statement2->bindValue(1, $db2_dbname);

    if (TRUE === $statement1->execute()){
        while ($row = $statement1->fetch(PDO::FETCH_ASSOC)){
            foreach ($row AS $key => $value){
                $db1_constraints[$row['TABLE_NAME']][$row['COLUMN_NAME']][$key] = $value;
            }
        }
    }

    if (TRUE === $statement2->execute()){
        while ($row = $statement2->fetch(PDO::FETCH_ASSOC)){
            foreach ($row AS $key => $value){
                $db2_constraints[$row['TABLE_NAME']][$row['COLUMN_NAME']][$key] = $value;
            }
        }
    }

    foreach ($db1_constraints AS $table => $info){
        foreach ($info AS $column => $data){
            if (isset($db2_constraints[$table][$column])){
                if (count($data)){
                    foreach ($data AS $key => $value){
                        if ('CONSTRAINT_NAME' !== $key && $db1_constraints[$table][$column][$key] !== $db2_constraints[$table][$column][$key]){
                            echo "<p>Column <strong>$column</strong> in table <strong>$table</strong> has differing characteristics for <strong>$key</strong> (". $db1_constraints[$table][$column][$key] ." vs. ". $db2_constraints[$table][$column][$key] .")</p>";
                        }
                    }
                }
            }else{
                echo "<p>Column <strong>$column</strong> in table <strong>$table</strong> is missing a constraint in the SECOND database!</p>";
            }
        }
    }

    foreach ($db2_constraints AS $table => $info){
        foreach ($info AS $column => $data){
            if (isset($db1_constraints[$table][$column])){
                if (count($data)){
                    foreach ($data AS $key => $value){
                        if ('CONSTRAINT_NAME' !== $key && $db2_constraints[$table][$column][$key] !== $db1_constraints[$table][$column][$key]){
                            echo "<p>Column <strong>$column</strong> in table <strong>$table</strong> has differing characteristics for <strong>$key</strong> (". $db2_constraints[$table][$column][$key] ." vs. ". $db1_constraints[$table][$column][$key] .")</p>";
                        }
                    }
                }
            }else{
                echo "<p>Column <strong>$column</strong> in table <strong>$table</strong> is missing a constraint in the FIRST database!</p>";
            }
        }
    }
}
?>

喜欢,伙计,但是你能帮我生成一些需要在其中一个数据库上运行的sql代码,这样我就可以使两个数据库完全相同了。Thanks@Chella-我使用phpMyAdmin来管理MySQL服务器,因此我使用它来手动更改表。如果您想从上面的脚本生成SQL,您将需要查看类似以下内容:
SHOW CREATE TABLE\u name
或可能的
description TABLE\u name
。可能也会有帮助。我可以做到,但为此我需要检查网页以找到差异,然后进入phpmyadmin生成表。这一切都有点冒险的过程对吧,不管怎样,我们都可以从上面的代码中找到差异对吧。。!?我们不能生成sql吗。你有没有为他们检查过mysqldiff的等效代码。