2014-06-27 121 views
0

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

任何想法?

+0

您应该使用的“复制”功能MySQL为此。请参阅http://dev.mysql.com/doc/refman/5.7/en/replication.html –

回答

0

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

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 
    { 
    } 

?> 

如果使用Windows UR你可以叫上一个按钮,点击这个代码,也可以如果乌拉圭回合使用Linux服务器或调度通过cron作业调用这个页面服务器

相关问题