连接到ubuntu服务器中的mysql数据库

时间:2015-06-16 16:03:57

标签: php mysql ubuntu azure virtual-machine

我使用以下代码连接MySQL中的PHP数据库。

try {
    shell_exec("ssh -f -L 3307:127.0.0.1:3306 ronak@server_ip sleep 60 >> logfile");
    $this->_conn = $this->dbh = new PDO('mysql:host=127.0.0.1;dbname=my_db', DB_USER, DB_PASS);
    $this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    die("Couldn't connect to database. Please try again!");
}

我想直接连接远程MySQL主机。服务器位于azure虚拟机上。我该怎么做这样的配置?

我已经在MySQL的azure portal中打开了3306端口。我不知道如何在没有ssh隧道的情况下使用它。

感谢。

1 个答案:

答案 0 :(得分:1)

1)更改my.conf(无论你调用mysql conf文件)。并将bind-address设置为0.0.0.0,因为它是概率127.0.0.1

2)停止/重启mysql守护进程

Connections now are not limited to those from localhost (what you are when you ssh). The default is localhost for obvious security reason until dev guy tweaks it

3)添加可能来自diff ip addr的数据库用户

CREATE USER 'fred7'@'192.168.2.7' IDENTIFIED BY 'my_password';

4)拨款

GRANT ALL PRIVILEGES ON test7db.* TO 'fred7'@'192.168.2.7';

5)防火墙

对于3和4,让我们说mysql服务器在aws ec2上,我使用sqlyog或workbench坐在公共图书馆。如果连接失败表明connection failed for user 'fred7'@'gfs6.nyc.comcastbusiness.net'之类的东西,那个编程将提醒我。所以很明显如何做3和4呢。

祝你好运!