vs如何连接docker上的mysql,通过Docker连接到localhost上的MySQL服务器

在Docker容器中无法连接到本地MySQL服务器,尽管可以ping通localbox。问题在于homebrew启动的mysql-server服务绑定了127.0.0.1,导致从容器内部无法访问。解决方案是修改homebrew的配置文件,移除bind-address或者直接更改其为0.0.0.0,允许外部连接。
摘要由CSDN通过智能技术生成

So, I'm able to generally contact my localhost through Docker by running a container with --add-host=localbox:192.168.59.3. ping localbox works just fine. Problem is, I can't seem to be able to even get a response from MySQL Server. mysql -h localbox, which works fine from outside of the docker container, just gets me ERROR 2003 (HY000): Can't connect to MySQL server on 'localbox' (111) from within.

I've done GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'password' WITH GRANT OPTION;

I've added bind-address = 0.0.0.0 into /etc/my.cnf. None of this helps. What gives?

Context: I'm running all of this through boot2docker on OS X Yosemite.

解决方案

So, turns out this is homebrew's fault with a really questionable design decision. You start-up mysql-server in homebrew by running the recommended launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist. But then, when examining this file, you'll find the bind-address is hardcoded!

/usr/local/opt/mysql/bin/mysqld_safe

--bind-address=127.0.0.1

--datadir=/usr/local/var/mysql

So, no matter what you do in any of your my.cnf files, it will always be bound to 127.0.0.1, and you'll never be able to query from a container. My fix is just editing this file directly not to provide a bind address so we can let /etc/my.cnf do it for us. Alternatively, though I wouldn't recommend it, you can just change the bind-address directly in this file.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值