是否有可能使DB像克隆一样?

时间:2012-04-24 18:09:15

标签: php mysql database iis-7 joomla

我有一个数据库(我们称之为A

我需要信息在多个DB中(B,C,D需要完全相同的信息)

我可以制作能够每隔(X)小时自动更新所有这些内容的东西吗?

我需要这个才能使用IIS 7 / php同步2个joomla组件。

尝试(第一次工作得很好,但是如果有人改变了,那么因为这个组件中没有主要内容而变得很快):

获取我的数据

$mysqli = @new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_WORLD);

/* Vérification de la connexion */
if (mysqli_connect_errno()) {
    die("&error=".mysqli_connect_error()."&");
    exit();
}


$query = "  SELECT  table_name
            FROM    information_schema.tables
            WHERE   table_name LIKE '%XXXXXXX%'";

$arrTableList   = array();
$allInArray     = array();
if ($result = $mysqli->query($query)) {

    /* Récupère un tableau d'objets */
    while ($obj = $result->fetch_object()) {
        $arrTableList[] = $obj->table_name;
    }

    /* free result set */
    $result->close();
}

foreach($arrTableList as $key => $value){

    $query = "  SELECT  *
                FROM    $value";

    if ($result = $mysqli->query($query)) {

        /* Récupère un tableau d'objets */
        while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
            $allInArray[$value][] = $row;
        }

        /* free result set */
        $result->close();
    }

}

插入功能

function toSwich($newDb, $tmpTag){
    global $mysqli;
    global $allInArray;
    global $tag1;

    $mysqli->select_db($newDb);

    foreach($allInArray as $table => $tableArrayData){

        $table  =   str_replace ($tag1, $tmpTag, $table);
        foreach($tableArrayData as $uselessKey => $valueArray){
            $keyList    = "";
            $valueList  = "";
            $query = "SELECT COUNT(*) FROM $table WHERE ";
            foreach($valueArray as $key => $value){
                $key        =    $mysqli->real_escape_string($key);
                $value      =    $mysqli->real_escape_string($value);

                $keyList    .=  $key . ",";
                $valueList  .=  "'" . $value . "',";

                $query      .=  " $key = '$value' AND";
            }

            $query      =   substr($query,0,-3);
            $valueList  =   substr($valueList,0,-1);
            $keyList    =   substr($keyList,0,-1);

            if(!$result = $mysqli->query($query)){
                print_r($mysqli->error);
                echo "<br />";
                print_r($query);
                echo "<br />";
                exit();

            }
            $row = $result->fetch_row();
            if((int)$row[0] == 0){
                if(!$mysqli->real_query("INSERT INTO $table ($keyList) VALUES ($valueList)")){
                    print_r($mysqli->error);
                    exit();
                }
            }
        }
    }

}

1 个答案:

答案 0 :(得分:1)

配置主从复制模式,我将不断同步数据库

这是一个如何配置复制的示例:3 Jump Steps to Configure Master Slave Replication in Mysql