简体   繁体   English

使用PHP将表内容从localserver传输到远程服务器DB的代码

[英]Code for transfering the table content from localserver to remote server DB using PHP

<?php
$dbhost1 = "10.0.11.211";
$dbuser1 = "root";
$dbpassword1 = "";
$db1 = "product";
$connection1 = mysqli_connect($dbhost1, $dbuser1, $dbpassword1) or die (mysqli_error());
mysqli_select_db($connection1, $db1);

//MySQL Server 2
$dbhost2 = "localhost";
$dbuser2 = "root";
$dbpassword2 = "";
$db2 = "product";
$connection2 = mysqli_connect($dbhost2, $dbuser2, $dbpassword2) or die (mysqli_error());
mysqli_select_db($connection2, $db2);
$sql = "insert into 10.0.11.211.product.inserting select * from localhost.product.inserting";
$result = mysqli_query($connection1, $sql);
if (!$result) {
    echo "connection error";
}
?>

How can I correct this code? 如何更正此代码? Actually I connected to servers at time. 实际上我当时连接到服务器。

You can setup federated tables , which is basically linking a table on one server to a table on another. 您可以设置联合表 ,基本上是将一台服务器上的表链接到另一台服务器上的表。 Then use the federation to do your data transfers. 然后,使用联合进行数据传输。

First, you must have a table on the remote server that you want to access by using a FEDERATED table. 首先,您必须在远程服务器上具有要使用FEDERATED表访问的表。 Suppose that the remote table is in the federated database and is defined like this: 假设远程表位于联合数据库中,并且定义如下:

You need to change database host and database/table name as per your details 您需要根据您的详细信息更改数据库主机和数据库/表名称

CREATE TABLE test_table (
    id     INT(20) NOT NULL AUTO_INCREMENT,
    name   VARCHAR(32) NOT NULL DEFAULT '',
    other  INT(20) NOT NULL DEFAULT '0',
    PRIMARY KEY  (id),
    INDEX name (name),
    INDEX other_key (other)
)
ENGINE=MyISAM
DEFAULT CHARSET=latin1;
Next, create a FEDERATED table on the local server for accessing the remote table:
CREATE TABLE federated_table (
    id     INT(20) NOT NULL AUTO_INCREMENT,
    name   VARCHAR(32) NOT NULL DEFAULT '',
    other  INT(20) NOT NULL DEFAULT '0',
    PRIMARY KEY  (id),
    INDEX name (name),
    INDEX other_key (other)
)
ENGINE=FEDERATED
DEFAULT CHARSET=latin1
CONNECTION='mysql://root@10.0.11.211:9306/federated/test_table';

Then you can query it like any other table. 然后,您可以像查询其他任何表一样查询它。

There are however a decent number of limitations you should read about including the remote password being stored in plain text. 但是,您应该了解很多限制 ,包括以纯文本格式存储的远程密码。 If this was a temporary setup purely for a once off copy, and the server isn't available to the public you have already minimised most of the risk associated with it though. 如果这是仅用于一次复制的临时设置,并且服务器不对公众开放,则您已经将与之相关的大多数风险降至最低。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM