很多朋友都对数据库的到数据库的导入感到很麻烦,特别是对不支持的MYSQL远程连接的空间感到很苦恼,我手上有一个阳光工作室写的数据库的到数据库的导入程序,并告诉大家一个简单的方法解决不支持的MYSQL远程连接的空间的苦恼,以下为三个程序文件:
#######################index.php###########################
<? include("config.php");
?> <html>
<head> <title>MYSQL数据库备份工具</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style> <!--
body.main{background-color:#E6F2FF;}
font{font-size: 12px;} p {font-size:12px;}
a { text-decoration: none; color: rgb(40,40,180) }
a:hover {color:#FF9900;text-decoration:none}
table {font-size:12px;} td {font-size:12px;}
td.tcolor{background-color:#8CC6FF;}
td.qcolor{background-color:#C1E0FF;}
table.list{background-color:#FFFFFF}
font.topcolor{color:red;} font.high{line-height:14pt}
.input2 {BORDER-BOTTOM: #000000 1px solid; BORDER-LEFT: #000000 1px solid; BORDER-RIGHT: #000000 1px solid; BORDER-TOP: #000000 1px solid}
.stbtm{height:22;border:1 solid #9a9999; font-size:9pt;background-color:#f8f8f8}
--> </style> </head>
<body class=main>
<table width="100%" border="0">
<tr>
<td class=tcolor align="center" height="25"> </td>
</tr> <tr>
<td align="center" valign="top">
<script language="JavaScript"> <!--//
function load() { m.host1.value="<? echo $Servers[1]['host']; ?>"; m.port1.value="<? echo $Servers[1]['port']; ?>"; m.user1.value="<? echo $Servers[1]['user']; ?>"; m.pass1.value="<? echo $Servers[1]['pass']; ?>"; m.db1.value="<? echo $Servers[1]['db']; ?>"; m.host2.value="<? echo $Servers[2]['host']; ?>"; m.port2.value="<? echo $Servers[2]['port']; ?>"; m.user2.value="<? echo $Servers[2]['user']; ?>"; m.pass2.value="<? echo $Servers[2]['pass']; ?>"; }
function down()
{ m.host1.value="<? echo $Servers[2]['host']; ?>"; m.port1.value="<? echo $Servers[2]['port']; ?>"; m.user1.value="<? echo $Servers[2]['user']; ?>"; m.pass1.value="<? echo $Servers[2]['pass']; ?>"; m.db1.value="<? echo $Servers[2]['db']; ?>"; m.host2.value="<? echo $Servers[1]['host']; ?>"; m.port2.value="<? echo $Servers[1]['port']; ?>"; m.user2.value="<? echo $Servers[1]['user']; ?>"; m.pass2.value="<? echo $Servers[1]['pass']; ?>"; }
function check(obj)
{ VP= true;
if (obj.host1.value.length<1) {
alert("来源主机地址不能为空!");
obj.host1.focus();
VP= false; return;
}
if (obj.host2.value.length<1) {
alert("目标主机地址不能为空!");
obj.host2.focus();
VP= false; return;
}
if (obj.host1.value==obj.host2.value) {
alert("来源主机地址和目标主机地址不能相同,不然可能会破坏数据!");
obj.host1.focus();
VP= false; return;
} return true;
} //--> </script> <br>
<font size="5"><b><font color="#3366FF">阳光MYSQL数据库备份工具</font></b></font><br>
<br>
<form name=m method=post action=run.php onSubmit="return VP">
<table width="60%" border="0" cellpadding="2">
<tr>
<td colspan="2" class=qcolor>来源数据库设置</td>
</tr> <tr>
<td width="80">
<div align="right">主机地址: </div>
</td> <td>
<input type="text" class=input2 name="host1" size="50" maxlength="80" value="<? echo $Servers[1]['host']; ?>"> </td>
</tr> <tr>
<td width="80">
<div align="right">主机端口: </div>
</td> <td>
<input type="text" class=input2 name="port1" size="50" value="<? echo $Servers[1]['port']; ?>" maxlength="20"> </td>
</tr> <tr>
<td width="80">
<div align="right">用 户 名: </div>
</td> <td>
<input type="text" class=input2 name="user1" size="50" maxlength="80" value="<? echo $Servers[1]['user']; ?>"> </td>
</tr> <tr>
<td width="80">
<div align="right">用户密码: </div>
</td> <td>
<input type="password" class=input2 name="pass1" size="50" value="<? echo $Servers[1]['pass']; ?>" maxlength="20"> </td>
</tr> <tr>
<td width="80">
<div align="right">数据库名: </div>
</td> <td>
<input type="text" class=input2 name="db1" size="50" value="<? echo $Servers[1]['db'];?>"> </td>
</tr> <tr>
<td width="80">
<div align="right">数据表名: </div>
</td> <td>
<input type="text" class=input2 name="table1" size="50" maxlength="80">
</td> </tr> <tr>
<td colspan="2" class=qcolor>目标数据库设置</td>
</tr> <tr>
<td width="80">
<div align="right">主机地址: </div>
</td> <td>
<input type="text" class=input2 name="host2" size="50" maxlength="80" value="<? echo $Servers[2]['host']; ?>"> </td>
</tr> <tr>
<td width="80">
<div align="right">主机端口: </div>
</td> <td>
<input type="text" class=input2 name="port2" size="50" value="<? echo $Servers[2]['port']; ?>" maxlength="20"> </td>
</tr> <tr>
<td width="80">
<div align="right">用 户 名: </div>
</td> <td>
<input type="text" class=input2 name="user2" size="50" maxlength="80" value="<? echo $Servers[2]['user']; ?>"> </td>
</tr> <tr>
<td width="80">
<div align="right">用户密码: </div>
</td> <td>
<input type="password" class=input2 name="pass2" size="50" value="<? echo $Servers[2]['pass']; ?>" maxlength="20"> </td>
</tr> <tr>
<td class=qcolor colspan="2">任务类型:
<input type="radio" name="upload" value="true" onClick="load()" checked>
上传
<input type="radio" name="upload" value="false" onclick="down()">
下载 运行方式:
<input type="radio" name="new" value="全部更新" checked>
全部更新
<input type="radio" name="new" value="追加记录">
追加记录 </td> </tr>
</table>
<input type="submit" name="run" value=" 执 行 " class=stbtm onClick="check(this.form)">
<input type="reset" name="clear" value=" 重 置 " class=stbtm>
</form> </td> </tr>
<tr>
<td class=tcolor align="center" height="25"> </td>
</tr> </table>
<table width="100%"> <tr>
<td align="right"><a href="#top"><font color="maroon">TOP↑</font></a> </td>
</tr> </table>
<hr size="1" color="#008080" width="100%">
<div align="center">
<p align="center"> </p>
</div> </body> </html>
#######################config.php###########################
<? //本地数据库
$Servers[1]['host'] = 'localhost';
$Servers[1]['port'] = '3306'; $Servers[1]['user'] = ''; $Servers[1]['pass'] = ''; $Servers[1]['db'] = '';
//远程数据库 $Servers[2]['host'] = ''; $Servers[2]['port'] = ''; $Servers[2]['user'] = ''; $Servers[2]['pass'] = ''; $Servers[2]['db'] = ''; ?>
#######################run.php###########################
<html> <head>
<title>阳光MYSQL数据库备份工具--运行中……</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style> <!--
body.main{background-color:#E6F2FF;font-size: 12px;}
font{font-size: 12px;} p {font-size:12px;}
a { text-decoration: none; color: rgb(40,40,180) }
a:hover {color:#FF9900;text-decoration:none} -->
</style> </head>
<body class=main> <?
@set_time_limit(60000);
if ($upload="true") echo "正在执行下载操作,操作方式:".$new."……<br>"; else echo "正在执行上传操作……<br>";
echo "来源主机地址:$host1<br>";
echo "目标主机地址:$host2<br><br>"; echo "正在收集来源和目标数据库信息……<br>"; if ($port1!="") $host1=$host1.":".$port1;
if ($port2!="") $host2=$host2.":".$port2;
$con1 = mysql_connect($host1,$user1,$pass1); $con2 = mysql_connect($host2,$user2,$pass2);
echo "来源和目标数据库连接成功……<br>";
if ($db1=="") { $dbs = mysql_list_dbs($con1); $num_dbs = mysql_numrows($dbs); for($i=0; $i<$num_dbs; $i++) $db1=$db1.mysql_dbname($dbs, $i)."|"; $db1=substr($db1,0, strlen($db1)-1); }
$dblist=explode("|",$db1);
$num = count($dblist);
for($i=0; $i<$num; $i++) {
$tables = mysql_list_tables($dblist[$i],$con1); $num_tables = @mysql_numrows($tables);
if ($table1=="") { for($j=0; $j<$num_tables; $j++) $table1=$table1.mysql_tablename($tables, $j)."|"; $table1=substr($table1,0, strlen($table1)-1); }
$tablelist=explode("|",$table1);
echo "<li>正在操作来源数据库:".$dblist[$i]." 共有".$num_tables."个数据表<br>";
$dbs = mysql_list_dbs($con2); $num_dbs = mysql_numrows($dbs); for($t=0; $t<$num_dbs; $t++) {
$havedbs=false; if (mysql_dbname($dbs, $t)==$dblist[$i]) {$havedbs=true; break;}}
if (!$havedbs) {echo "<li>正在操作目标数据库:".$dblist[$i]." 不存在,正在创建数据库……<br>"; mysql_create_db($dblist[$i],$con2); echo "<li>目标数据库:".$dblist[$i]." 创建成功……<br>";
}
echo "<ol type=A>";
$num_tables = count($tablelist); for($j=0; $j<$num_tables; $j++) { echo "<li>正在操作来源数据表:".$tablelist[$j]; $pcount=count_records($con1,$dblist[$i],$tablelist[$j]);
echo " 共有".$pcount."条记录<br>";
$tables2 = mysql_list_tables($dblist[$i],$con2); $num2 = @mysql_numrows($tables);
for($t=0; $t<$num2; $t++) { $havetable=false;
if (mysql_tablename($tables2, $t)==$tablelist[$j]) {$havetable=true; break;}}
if (!$havetable) {echo "目标数据表:".$tablelist[$j]."不存在,正在创建数据表及结构……<br>"; $sql=get_table_def($con1,$dblist[$i], $tablelist[$j]); mysql_db_query($dblist[$i],$sql,$con2);
echo "目标数据表:".$tablelist[$j]."创建成功……<br>"; }
else { if ($pcount>0 && $new=="全部更新")
{ echo "目标数据表已存在<br>正在清空目标数据表 $tablelist[$j] 原有数据……<br>";
$sql="delete from $tablelist[$j]"; mysql_db_query($dblist[$i],$sql,$con2);} }
echo "正在读入来源数据表 $tablelist[$j] 数据……<br>";
if ($new=="全部更新") { if ($pcount>0) put_table_content($con1,$con2,$dblist[$i],$tablelist[$j],0,$pcount); } else {echo "目标数据表:".$tablelist[$j]; $pcount2=count_records($con2,$dblist[$i],$tablelist[$j]);
echo " 共有".$pcount2."条记录<br>";
if ($pcount>$pcount2) put_table_content($con1,$con2,$dblist[$i],$tablelist[$j],$pcount2,$pcount); else echo "目标数据表记录数大于或等于来源数据表记录数,无须追加……<br>"; } if ($pcount==0) echo "来源数据表记录数为零,无须追加……";
} echo "</ol>"; }
echo "此次任务完成!";
function get_table_def($con,$db, $table) {
$schema_create = ""; $schema_create .= "CREATE TABLE $table ("; $result = mysql_db_query($db,"SHOW FIELDS FROM $table",$con); while($row = mysql_fetch_array($result)) {
$schema_create .= " $row[Field] $row[Type]";
if(isset($row["Default"]) && (!empty($row["Default"]) || $row["Default"] == "0"))
$schema_create .= " DEFAULT '$row[Default]'";
if($row["Null"] != "YES")
$schema_create .= " NOT NULL";
if($row["Extra"] != "")
$schema_create .= " $row[Extra]";
$schema_create .= ","; }
$schema_create = ereg_replace(","."$", "", $schema_create);
$result = mysql_db_query($db, "SHOW KEYS FROM $table",$con); while($row = mysql_fetch_array($result)) {
$kname=$row['Key_name'];
if(($kname != "PRIMARY") && ($row['Non_unique'] == 0))
$kname="UNIQUE|$kname";
if(!isset($index[$kname]))
$index[$kname] = array();
$index[$kname][] = $row['Column_name'];
}
while(list($x, $columns) = @each($index))
{
$schema_create .= ",";
if($x == "PRIMARY")
$schema_create .= " PRIMARY KEY (" . implode($columns, ", ") . ")";
elseif (substr($x,0,6) == "UNIQUE")
$schema_create .= " UNIQUE ".substr($x,7)." (" . implode($columns, ", ") . ")";
else
$schema_create .= " KEY $x (" . implode($columns, ", ") . ")";
}
$schema_create .= ")";
return ($schema_create); }
function put_table_content($con1,$con2,$db,$table,$start,$end) { echo "<ol>";
$r=0; while($start<$end)
{ $result = mysql_db_query($db, "SELECT * FROM $table LIMIT $start,10",$con1); $i = 0;
while($row = mysql_fetch_row($result)) {
$table_list = "(";
for($j=0; $j<mysql_num_fields($result);$j++)
$table_list .= mysql_field_name($result,$j).", ";
$table_list = substr($table_list,0,-2);
$table_list .= ")";
$schema_insert = "INSERT INTO $table VALUES (";
for($j=0; $j<mysql_num_fields($result);$j++)
{
if(!isset($row[$j]))
$schema_insert .= " NULL,";
elseif($row[$j] != "")
$schema_insert .= " '".addslashes($row[$j])."',";
else
$schema_insert .= " '',";
}
$schema_insert = ereg_replace(",$", "", $schema_insert);
$schema_insert .= ")";
mysql_db_query($db,$schema_insert,$con2);
$t=$i+1+$start;
echo "<li>记录".$t."追加成功</li><br>";
$i++;
}
$r++; $start+=10; }
echo "</ol>"; }
function count_records ($con,$db,$table) { $result = mysql_db_query($db, "select count(*) as num from $table",$con); $num = mysql_result($result,0,"num");
return $num; }
?> </body>
</html>
首先请大家在自己的机子上开一个MYSQL帐号,支持远程连接,将上面的三个文件上传到OSO(或其它)的申请的发布目录,运行http://xxx.oso.com.cn/index.php 你将看到index.php页面,请大家看下面,有下载和上传选择,默认为上传,请大家选择下载,因为这个时候OSO(或其它)的主机已变成了逻辑上的localhost,在来源数据库设置中添上你自己的机子的IP,主机端口(一般为3306)和你为自己开设的远程连接帐号和密码以及你要操作的数据库的表,在目标数据库设置的主机地址添上:localhost 用户名和密码填写OSO(或其它)给你的操作数据库的用户名和密码,点击执行,一切OK!
|
|