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!

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

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?