Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql和PHP-从不同的服务器更新数据库_Php_Mysql - Fatal编程技术网

Mysql和PHP-从不同的服务器更新数据库

Mysql和PHP-从不同的服务器更新数据库,php,mysql,Php,Mysql,我需要从主机“B”连接到主机“a”上的数据库 我读过关于如何做这件事的书,但我找不到正确的方法。所以我写了这个: $servername = "118.140.84.78"; //host"A" ip $username = "lpq"; $password = "*****"; $dbname = "cc"; $cc = new PDO("mysql:host=$servername;dbname=$dbname", "$username", "$password"); $cc->se

我需要从主机“B”连接到主机“a”上的数据库

我读过关于如何做这件事的书,但我找不到正确的方法。所以我写了这个:

$servername = "118.140.84.78"; //host"A" ip
$username = "lpq";
$password = "*****";
$dbname = "cc";

$cc = new PDO("mysql:host=$servername;dbname=$dbname", "$username", "$password");
$cc->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
但当我尝试连接时,出现了以下错误:

捕获异常:SQLSTATE[HY000][2013]与MySQL的连接丢失 服务器位于“正在读取初始通信数据包”,系统错误:0

我的数据库的权限如下所示:

那里的ip是主机“B”的ip


有什么想法吗?

解决此问题的一个方法可能不是建立从服务器B到服务器a上MySQL数据库的直接连接,而是在服务器a上实现一个安全API,在本地运行所有数据库查询,并让服务器B使用该API运行查询

例如,在服务器A上,您可以保存在
api.php
中,类似于:

<?php
/* Configuration */
define( 'DB_HOSTNAME', '127.0.0.1' );
define( 'DB_USERNAME', 'dbuser' );
define( 'DB_PASSWORD', 'dbpass' );
define( 'DB_DATABASE', 'dbname' );

/* Process JSON request */
$aRequest = (array)json_decode( file_get_contents( "php://input" ));
if( isset( $aRequest['query'] ) && isset( $aRequest['params'] )) {

  /* Connect to database and run requested query */    
  try {
    $oPDO = new PDO( sprintf( 'mysql:host=%s;dbname=%s', DB_HOSTNAME, DB_DATABASE ),
      DB_USERNAME, DB_PASSWORD );
    $oPDO->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
    $hStatement = $oPDO->prepare( (string)$aRequest['query'] );
    $hStatement->setFetchMode( PDO::FETCH_ASSOC );
    $hStatement->execute( (array)$aRequest['params'] );
    $aResponse = array( 'success' => true, 'data' => (array)$hStatement->fetchAll());
  } catch( PDOException $oError ) {
    $aResponse = array( 'success' => false, 'error' => (string)$oError->errorInfo[2] );
  }
} else {
  $aResponse = array( 'success' => false, 'error' => 'Invalid request' );
}

/* Process JSON response */
header( 'Content-Type: application/json' );
echo( json_encode( $aResponse ));
<?php
/* Configuration */
define( 'PATH_API', 'http://118.140.84.78/api.php' );

class RemotePDO {
  private $sURL = '';

  function __construct( $sURL ) {
    $this->sURL = $sURL;
  }

  function exec( $sQuery, $aParams ) {
    $sRequest = json_encode( array( 'query' => $sQuery, 'params' => $aParams ));
    $aHttpOptions = array( 'http' => array( 'header' => 
      "Content-Type: application/json", 'method' => 'POST', 'content' => $sRequest ));
    $oHttpContext = stream_context_create( $aHttpOptions );
    return json_decode( @file_get_contents( $this->sURL, false, $oHttpContext ));
  }
}

/* Testing */
$sSQL = "SELECT * FROM orders WHERE order_id=:order_id";
$aParams = array( ':order_id' => 1 );
$oRemotePDO = new RemotePDO( PATH_API );
print_r( $oRemotePDO->exec( $sSQL, $aParams ));

可能是。该IP上的连接是否打开,我的意思是该IP中没有问题。请检查它是否可能是您要连接的端口。您可以这样设置PDO上的端口:
$PDO=new-PDO('mysql:host=118.140.84.78;port=$newPortNumber;dbname=$dbname','$username','$password')我也尝试过使用端口80,但甚至都不起作用:(嘿,伙计,谢谢你的帮助,虽然不是aswer,但仍在工作,所以将非常有用!!