您好我编写的代码可以将数据库表从一个服务器复制到另一个服务器但是每个表的记录都没有复制如何编写可以将表和每个记录从一个数据库服务器复制到另一个服务器的函数?
这是我的示例代码:
<?php
$dbNewDB = 'newdb';
$dbNewUser = 'newroot';
$dbNewUserPswd = 'newpass';
$dbConnect = mysql_connect('localhost', 'root', 'mypassword') or die('Couldn\'t connect to MySql:'.mysql_error());
$dbNewConnect = mysql_connect('localhost', $dbNewUser, $dbNewUserPswd) or die('Couldn\'t connect to MySql:'.mysql_error());
$sqlStatement = "SHOW TABLES FROM olddb";
$result = mysql_query($sqlStatement,$dbConnect) or die('Unable to get tables: '.mysql_error());
while($row = mysql_fetch_row($result))
{
//Drop table if exist
$sqlStatement = "DROP TABLE IF EXISTS " . $dbNewDB . "." . $row[0];
mysql_query($sqlStatement,$dbNewConnect) or die("Failed to delete: " . mysql_error());
//Create new table
$sqlStatement = "CREATE TABLE " . $dbNewDB . "." . $row[0] . " LIKE olddb." . $row[0];
echo "$sqlStatement [" . __METHOD__ . "]";
mysql_query($sqlStatement,$dbNewConnect)or die("Failed to create: ". mysql_error());
//Insert data
$sqlStatement = "INSERT INTO " . $dbNewDB . "." . $row[0] . " SELECT * FROM " . $dbNewDB . "." . $row[0];
echo "$sqlStatement [" . __METHOD__ . "]";
mysql_query($sqlStatement,$dbNewConnect)or die("Table copy failed: ".mysql_error());
echo "$row[0] copy done. [" . __METHOD__ . "]";
}
mysql_free_result($result);
mysql_close($dbConnect);
mysql_close($dbNewConnect);
?>
我的代码已经正常运行所有我想修复以复制每个表的记录。 有什么想法吗?还是帮忙?
谢谢!
答案 0 :(得分:5)
您可以像下面这样以SQL格式转储整个数据库:
mysqldump --user=root --password=whatever --databases dbtest --opt --quote-names --complete-insert > testbkup.sql
然后你可以像这样导入它:
mysql -u root -p whatever dbtest < testbkup.sql
(注意:user = root,password = whatever,dbtest是你的数据库。)
只是说。
答案 1 :(得分:4)
我发现这些脚本有效,你可以试试这些:
<?php
$connect2 = mysql_connect("localhost", "root", "");
$database1 = "test1"; // destination database
mysql_select_db($database1, $connect2);
set_time_limit(0);
$database = 'dev_loribonn'; //original database
$connect = mysql_connect("localhost", "root", "");
mysql_select_db($database, $connect);
$tables = mysql_query("SHOW TABLES FROM $database");
while ($line = mysql_fetch_row($tables)) {
$tab = $line[0];
mysql_query("DROP TABLE IF EXISTS $database1.$tab");
mysql_query("CREATE TABLE $database1.$tab LIKE $database.$tab") or die(mysql_error());
mysql_query("INSERT INTO $database1.$tab SELECT * FROM $database.$tab");
echo "Table: <b>" . $line[0] . " </b>Done<br>";
}
?>
答案 2 :(得分:2)
请注意,如果您只是将整个表从一个数据库复制到另一个数据库,则可以在一个SQL查询中完成所有操作。
CREATE TABLE `backup_db.backup_table` SELECT * FROM `live_db.live_table`;
我已经使用类似的东西进行备份,但我将整个数据库转储到备份数据库
编辑:Woops,没有看到多个数据库连接。
答案 3 :(得分:0)
您的插入语句会关闭。尝试使用来自dbNewDB的值插入$ dbNewDB。您必须转向旧数据库。下面我正在为插入物构建两个叮咬。 $ string1 ='(col1name,col2name ...,)'$ string2 ='(val1-1,val1-2,...),(val2-1,val2-2,...),...' for“INSERT INTO table $ string1 VALUES $ string2”
//Insert data
$sql2 = "SELECT * FROM " . $row[0];
$r = mysql_query($sql, $bConnect);
$string1 = '('; $arr = array();
while ($irow = mysql_fetch_assoc($r)) {$arr[] = $irow;}
foreach($irow as $k=>$v)
{
$string1 .= "$k,";
}
$string1 = substr($string1, 0, -1) //lose last comma
$string1 .= ')';
$string2 = array_reduce($f, $arr);
$string2 = substr($string2, 0, -1) //lose last comma
$sqlStatement = "INSERT INTO " . $dbNewDB . "." . $row[0] . " $string1 VALUES $string2";
echo "$sqlStatement [" . __METHOD__ . "]";
mysql_query($sqlStatement,$dbNewConnect)or die("Table copy failed: ".mysql_error());
echo "$row[0] copy done. [" . __METHOD__ . "]";
在其他地方声明$ f
$f = function($b, $x) {$a = ' ('; foreach($x as $v) {$a .= "'$v',";} $a = substr($a, 0, -1); $a .= ')'; return "$b $a,";}
答案 4 :(得分:0)
您可以使用sql yog将表传输到不同的主机,而无需在php中编写代码。