从其他服务器上的数据库更新数据库

时间:2014-06-27 10:00:29

标签: mysql database triggers updating

是否有可能从服务器1上的数据库获取/更新(例如,每小时一次更新)字段并将它们放入服务器2中?

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

您可以在服务器1上使用服务器脚本,并使用每小时的cron作业从服务器2调用它。

答案 1 :(得分:0)

<?PHP
date_default_timezone_set('Asia/Calcutta');
$today = date('Y-m-d H:i:s');
//remember to change the password to whatever you set
//it to in mysql instance configuration

//first parameter is server name, 2nd username 'root', 3rd is password

$rst = @mysql_connect("localhost","user1","password1","dbname1");
$rst1 = @mysql_connect("localhost","user2","password2","dbname2");


if (!$rst){
     echo( "<p>Unable to connect to database manager.</p>");
       die('Could not connect: ' . mysql_error());
     exit();
} else {
  //echo("<p>Successfully Connected to MySQL Database Manager!</p>");
}

if (! @mysql_select_db("dbname1") ){
     echo( "<p>Unable to  connect database...</p>");
     exit();
} else {
  //echo("<p>Successfully Connected to Database 'MYSQL'!</p>");
}


if (!$rst1){
     echo( "<p>Unable to connect to database manager.</p>");
       die('Could not connect: ' . mysql_error());
     exit();
} else {
 // echo("<p>Successfully Connected to MySQL Database Manager!</p>");
}

if (! @mysql_select_db("dbname2") ){
     echo( "<p>Unable to  connect database...</p>");
     exit();
} else {
 // echo("<p>Successfully Connected to Database 'MYSQL'!</p>");
}
?>

<?php   
$count=0; 
$rst1 = @mysql_connect("localhost","user2","password2","dbname2");
if (!$rst1){
     echo( "<p>Unable to connect to database manager.</p>");
       die('Could not connect: ' . mysql_error());
     exit();
} else {
 // echo("<p>Successfully Connected to MySQL Database Manager!</p>");
}

if (! @mysql_select_db("dbname2") ){
     echo( "<p>Unable to  connect database...</p>");
     exit();
} else {
 // echo("<p>Successfully Connected to Database 'MYSQL'!</p>");
}

$SQLDEL = mysql_query("TRUNCATE TABLE dbname2.table2");

$rst = @mysql_connect("localhost","user1","password1","dbname1");
if (!$rst){
     echo( "<p>Unable to connect to database manager.</p>");
       die('Could not connect: ' . mysql_error());
     exit();
} else {
  //echo("<p>Successfully Connected to MySQL Database Manager!</p>");
}

if (! @mysql_select_db("dbname1") ){
     echo( "<p>Unable to  connect database...</p>");
     exit();
} else {
  //echo("<p>Successfully Connected to Database 'MYSQL'!</p>");
}
$crfselect=mysql_query("select * from dbname1.tablename");
$crfselectsql=mysql_fetch_array($crfselect);
$rst1 = @mysql_connect("localhost","user2","password2","dbname2");
if (!$rst1){
     echo( "<p>Unable to connect to database manager.</p>");
       die('Could not connect: ' . mysql_error());
     exit();
} else {
 // echo("<p>Successfully Connected to MySQL Database Manager!</p>");
}

if (! @mysql_select_db("dbname2") ){
     echo( "<p>Unable to  connect database...</p>");
     exit();
} else {
 // echo("<p>Successfully Connected to Database 'MYSQL'!</p>");
}
while($crfselectsql)
{
$inserttrustfind=mysql_query("insert into dbname2.tablename set //Your fields");
$count++;
$crfselectsql=mysql_fetch_array($crfselect);    
}
if($inserttrustfind)
{
    $rst = @mysql_connect("localhost","user1","password1","dbname1");
if (!$rst){
     echo( "<p>Unable to connect to database manager.</p>");
       die('Could not connect: ' . mysql_error());
     exit();
} else {
  //echo("<p>Successfully Connected to MySQL Database Manager!</p>");
}

if (! @mysql_select_db("dbanme1") ){
     echo( "<p>Unable to  connect database...</p>");
     exit();
} else {
  //echo("<p>Successfully Connected to Database 'MYSQL'!</p>");
}

// TO KEEP COPY DATE IN DATABASE
$insetsqyn = mysql_query("insert into dbname1.databasesynch set synctime='$today'");
}
if($insetsqyn)
    {
    echo "<script>location.href='checker_punching.php'</script>";
    exit();
    }
    else
    {
    }

?>

您可以单击按钮调用此代码,或者如果您使用Linux服务器或调度程序,如果您使用的是Windows服务器,则可以通过cron作业调用此页面