dp518158 2015-06-16 08: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 09: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!

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

报告相同问题?

悬赏问题

  • ¥15 加热反应炉PLC控制系统设计(相关搜索:梯形图)
  • ¥15 python 用Dorc包报错,我的写法和网上教的是一样的但是它显示无效参数,是什么问题
  • ¥15 指定IP电脑的访问设置
  • ¥30 matlab ode45 未发现警告,但是运行出错
  • ¥15 vscode platformio
  • ¥15 代写uni代码,app唤醒
  • ¥15 全志t113i启动qt应用程序提示internal error
  • ¥15 ensp可以看看嘛.
  • ¥80 51单片机C语言代码解决单片机为AT89C52是清翔单片机
  • ¥60 优博讯DT50高通安卓11系统刷完机自动进去fastboot模式
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部