doupu2722 2016-12-09 20:53
浏览 14

使用PHP从我的网站连接到另一台计算机中托管的MySQL数据库

I got this problem, let's see if some one here can give me a hand with it.

I got a website hosted in a computer, with its own database, and I can access it from any computer using the domain pointing to it or its public address.

Using PHP I'm running a little script (on my website) that connects to another database and brings some information; the thing is that this other database is hosted in a computer within a VPN (using Hamachi) where the client doesn't have access but the server does.

If I connect from the server using

Mysql -h (IP_ADDRESS_ON_THE_VPN) -u user -p

It connects perfectly, so no firewall problem / connection problem.

But if I open the PHP script from a client, it says:

"SQLSTATE[HY000] [2002] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. "

in other words: timeout...

Another test I made is connect the client to the VPN, and that way works perfectly!, strage... doesn't PHP run on the server? What't the best approach here?

Thanks for you time.

EDIT: @Martin Cup Here's some of the code I use to make the connection, I´m almost sure that there´s not a problem in the code since it works if I add the client to the network.

 try{
           $DB_CTE = new PDO('mysql:host='.$SRV.';port='.$PUERTO.';dbname='.$BASE,$USU,$PASS, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
       } catch (PDOException $e) {
           echo 'Error: '  . $e->getMessage() . '<br><br>HOST:'.$SRV.'<br><br>Data CNX:' . $SRV ."(".$BASE.")".$PUERTO;
           exit;
       }
       $sql = 'SELECT * FROM ' .$BASE. '.xxx ORDER BY FEC_EMI LIMIT 10 ';
       try{
           $query = $DB_CTE->prepare($sql);;
           $query->execute();
           $list = $query->fetchAll();
           $xxx = array();
           foreach ($list as $rs) {
                echo "VTA: ".$rs['ID_FCV']."T_T: ".$rs['T_T']."
";
           }
       } catch (PDOException $e) {
            echo 'Error: '  . $e->getMessage() . '<br><br>SQL:' . $sql;
            exit;
       }

@Hackerman I have root access to the database, and all host are avalible 'root'@'%', I'm sure of it since connect to it from serval computers. What I want to do now is to publish some info from that computer in the web (connecting to that database), I also can't stablish one IP address per client since it will be public and I don't know how many and from where clients will be connecting. Hope this makes sense.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 多址通信方式的抗噪声性能和系统容量对比
    • ¥15 winform的chart曲线生成时有凸起
    • ¥15 msix packaging tool打包问题
    • ¥15 finalshell节点的搭建代码和那个端口代码教程
    • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
    • ¥15 Centos / PETSc / PETGEM
    • ¥15 centos7.9 IPv6端口telnet和端口监控问题
    • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
    • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
    • ¥20 海浪数据 南海地区海况数据,波浪数据