dp518158 2015-06-16 16:03
浏览 90
已采纳

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

I am using below code to connect MySQL database in 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!");
}

I want to direct connect with remote MySQL host. Server is on azure virtual machine. How can I do such a configurations?

I already opened 3306 port in azure portal for MySQL. I don't know how to use it in here without ssh tunnel.

Thanks.

  • 写回答

1条回答 默认 最新

  • douque9982 2015-06-16 17:01
    关注

    1) change my.conf (whatever your mysql conf file is called). And set bind-address to 0.0.0.0 as it is prob 127.0.0.1

    2) stop/restart mysql daemon

    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) add database user perhaps coming in from diff ip addr

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

    4) grants

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

    5) firewall

    For 3 and 4, let's say the mysql server is on aws ec2 and i am sitting at a public library using sqlyog or workbench. That prog will alert me if connect failure stating something like connection failed for user 'fred7'@'gfs6.nyc.comcastbusiness.net'. So it is pretty obvious how to do 3 and 4 then.

    Good luck!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 Python安装cvxpy库出问题
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥15 python天天向上类似问题,但没有清零
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题