远程连接到mysql服务器 - PHP

时间:2013-08-14 18:49:30

标签: php mysql windows

我有以下类,用于建立php MySQL连接($ipaddress完全有效,仅用于隐藏实际的IP地址)

class DbConn {

    private $host;
    private $user;
    private $password;
    private $database;
    private $link;
    private $db;

    public function __construct($ipaddress) {
        $this->host = $ipaddress;
        $this->user = 'myuser';
        $this->password = 'mypwd';
        $this->database = 'mydb';
        try {
            $this->link = mysql_connect($this->host, $this->user, $this->password);
            if (!$this->link) {
                throw new Exception('Database connection error');
            }
            $this->db = mysql_select_db($this->database, $this->link);
            if (!$this->db) {
                throw new Exception('Cant use database: '. mysql_error());
            }
        } catch (Exception $e) {
            print_r($e);
            die('Could not connect: ' . mysql_error());
        }
    }
}

当我在$ipaddress主机(localhost)上运行它时,它运行得很好......现在我正在尝试在远程机器上运行它,所以我可以远程连接到MySQL服务器。但是,我收到以下错误:

Warning: mysql_connect(): A connection attempt failed because the connected par
y did not properly respond after a period of time, or established connection fa
led because connected host has failed to respond.
 in C:\performance\browser_perf\DbConn.php on line 18

我正在尝试连接的当前计算机是Windows 8 X86计算机,并且MySQL服务器已安装在Windows 7 X64_86计算机中。我有以下问题:

  • 我是否必须在远程服务器(Win 8)中安装MySQL,或者php接口可以为我处理该连接?
  • 如何断言IP地址是否正确?我使用ipconfig
  • 查看了它

3 个答案:

答案 0 :(得分:2)

PHP有一个客户端,它与服务器交互,你不需要在“客户端”机器上安装MySQL服务器。检查端口3306是否响应。应该使用单个 telnet IPofWin7MySQLServer 3306

答案 1 :(得分:0)

您需要在目标计算机(Win7)上运行的MYSQL服务器

答案 2 :(得分:0)

在MySQL服务器上的my.cnf中,检查bind-address设置是否设置为127.0.0.1。如果是将其更改为0.0.0.0。