PHP复制mysql架构和数据

时间:2014-03-12 10:17:44

标签: php mysql schema data-migration

我使用一个小脚本来自动化服务器之间的模式传输,但现在我需要修改它以包含数据。我对PHP不太好,所以我请求你的帮助。 这是我到目前为止(我从其他来源得到的部分,而不是我的):

/*********** GRAB OLD SCHEMA ***********/ $db1 = mysql_connect($DB_SRC_HOST,$DB_SRC_USER,$DB_SRC_PASS) or die(mysql_error()); mysql_select_db($DB_SRC_NAME, $db1) or die(mysql_error());

$result = mysql_query("SHOW TABLES;",$db1) or die(mysql_error());
$buf="set foreign_key_checks = 0;\n";
$constraints='';
while($row = mysql_fetch_array($result))
{
        $result2 = mysql_query("SHOW CREATE TABLE ".$row[0].";",$db1) or die(mysql_error());
        $res = mysql_fetch_array($result2);
        if(preg_match("/[ ]*CONSTRAINT[ ]+.*\n/",$res[1],$matches))
        {
                $res[1] = preg_replace("/,\n[ ]*CONSTRAINT[ ]+.*\n/","\n",$res[1]);
                $constraints.="ALTER TABLE ".$row[0]." ADD ".trim($matches[0]).";\n";
        }
        $buf.=$res[1].";\n";
}
$buf.=$constraints;
$buf.="set foreign_key_checks = 1";

/**************** CREATE NEW DB WITH OLD SCHEMA ****************/
$db2 = mysql_connect($DB_DST_HOST,$DB_DST_USER,$DB_DST_PASS) or die(mysql_error());
$sql = 'CREATE DATABASE '.$DB_DST_NAME;
if(!mysql_query($sql, $db2)) die(mysql_error());
mysql_select_db($DB_DST_NAME, $db2) or die(mysql_error());
$queries = explode(';',$buf);
foreach($queries as $query)
        if(!mysql_query($query, $db2)) die(mysql_error());

$result = mysql_query("SHOW TABLES;",$db1) or die(mysql_error()); $buf="set foreign_key_checks = 0;\n"; $constraints=''; while($row = mysql_fetch_array($result)) { $result2 = mysql_query("SHOW CREATE TABLE ".$row[0].";",$db1) or die(mysql_error()); $res = mysql_fetch_array($result2); if(preg_match("/[ ]*CONSTRAINT[ ]+.*\n/",$res[1],$matches)) { $res[1] = preg_replace("/,\n[ ]*CONSTRAINT[ ]+.*\n/","\n",$res[1]); $constraints.="ALTER TABLE ".$row[0]." ADD ".trim($matches[0]).";\n"; } $buf.=$res[1].";\n"; } $buf.=$constraints; $buf.="set foreign_key_checks = 1"; /**************** CREATE NEW DB WITH OLD SCHEMA ****************/ $db2 = mysql_connect($DB_DST_HOST,$DB_DST_USER,$DB_DST_PASS) or die(mysql_error()); $sql = 'CREATE DATABASE '.$DB_DST_NAME; if(!mysql_query($sql, $db2)) die(mysql_error()); mysql_select_db($DB_DST_NAME, $db2) or die(mysql_error()); $queries = explode(';',$buf); foreach($queries as $query) if(!mysql_query($query, $db2)) die(mysql_error());

如何修改现有代码以包含数据?

谢谢!

2 个答案:

答案 0 :(得分:0)

如果你需要传输数据/和/架构,为什么不做一个mysqldump,只是读回整个东西?这个脚本本应该做什么呢?

答案 1 :(得分:0)

经过大量的链接和教程后,我设法提出了一些简单的方法!这是代码:

$connectDST = mysql_connect($DB_DST_HOST, $DB_DST_USER, $DB_DST_PASS);
mysql_select_db($DB_DST_NAME, $connectDST);
set_time_limit(0);

$connectSRC = mysql_connect($DB_SRC_HOST, $DB_SRC_USER, $DB_SRC_PASS);

mysql_select_db($DB_SRC_NAME, $connectSRC);

$tables = mysql_query("SHOW TABLES FROM $DB_SRC_NAME");

while ($line = mysql_fetch_row($tables)) {
    $tab = $line[0];
    mysql_query("DROP TABLE IF EXISTS $DB_DST_NAME.$tab") or die('Couldn\'t drop table:'.mysql_error());
    mysql_query("CREATE TABLE $DB_DST_NAME.$tab LIKE $DB_SRC_NAME.$tab") or die(mysql_error()) or die('Couldn\'t create table:'.mysql_error());
    mysql_query("INSERT INTO $DB_DST_NAME.$tab SELECT * FROM $DB_SRC_NAME.$tab") or die('Couldn\'t insert data:'.mysql_error());
    echo "Table: <b>" . $line[0] . " </b>Done<br>";
}

我试图避免exec,因为有些主机不允许它......