2014-06-27 119 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作業調用這個頁面服務器

相關問題