【问题标题】:connect to online database from localhost in php在 php 中从 localhost 连接到在线数据库
【发布时间】:2015-05-17 15:09:33
【问题描述】:

我有两个数据库

其中一个在本地服务器上,另一个在线。

我想通过 PHP 代码在 localhost 中连接两者。

如何为此目的定义此参数?本地服务器是否需要特殊配置?

//database that is local
    define( "HOST", 'localhost' ) ;
    define( "DBUSER", 'root' ) ; 
    define( "DBNAME", 'name_db' ) ;  
    define( "DBPASS", '' ); 


      //database that is online
        define( "HOSTPM", 'localhost' ) ;
    define( "DBUSERPM", 'username' ) ; 
    define( "DBNAMEPM", 'name2_db' ) ; 
    define( "DBPASSPM", 'password' );  

$db = new  PDO('mysql:host='. HOST .';dbname='. DBNAME . ';charset=utf8', DBUSER, DBPASS);
        $dbpm = new  PDO('mysql:host='. HOSTPM .';dbname='. DBNAMEPM . ';charset=utf8', DBUSERPM, DBPASSPM);
        if (!$db) {
                die('Could not connect: ' . mysqli_error());
            }
        if (!$dbpm) {
           die('Could not connect: ' . mysqli_error());
       }

【问题讨论】:

  • localhost 是您当前的机器。如果您需要访问远程资源,请通过 IP 或主机名连接到它。
  • 好的!我通过 IP 和主机名测试了远程连接,但得到了类似这样的错误 Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2002] 连接尝试失败,因为连接方在一段时间后没有正确响应,或建立连接失败,因为连接的主机没有响应
  • 在来 SO 之前你们会阅读文档吗?

标签: php connection


【解决方案1】:

默认情况下你没有远程访问mysql服务器的权限

检查该答案以修改权限:connect to mysql server remotely

GRANT ALL ON database.* TO user@ipaddress IDENTIFIED BY 'password';

您应该使用以下命令强制重新加载授权表:

刷新特权;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-31
    • 2014-07-14
    • 2015-02-18
    • 2017-07-06
    • 1970-01-01
    • 2011-05-29
    • 2018-10-10
    相关资源
    最近更新 更多