通过Docker连接到localhost上的MySQL服务器 [英] Connecting to MySQL Server on localhost through Docker

查看:596
本文介绍了通过Docker连接到localhost上的MySQL服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我通常可以通过运行带有 - add-host = localbox:192.168.59.3 的容器来通过Docker联系我的本地主机。 ping localbox 工作正常。问题是,我似乎无法甚至得到MySQL服务器的响应。 mysql -h localbox ,从Docker容器外部工作正常,只需得到我 ERROR 2003(HY000):无法连接到MySQL服务器在'localbox'(111)内。



我已经完成了授予所有特权*。* TO'root'@'%'通过'密码'识别与GRANT OPTION; / code>



我已将$ code> bind-address = 0.0.0.0 添加到/ etc / my .CNF。这些都没有帮助。什么给了?



上下文:我在OS X Yosemite上通过boot2docker运行所有这些。

解决方案

所以,原来这是一个非常有疑问的设计决定的自制软件的错误。您可以通过运行推荐的 launchctl load -w〜/ Library / LaunchAgents / homebrew.mxcl.mysql.plist 来启动homebrew中的mysql-server。但是,当检查这个文件时,你会发现bind-address是硬编码的!

 < array> 
< string> / usr / local / opt / mysql / bin / mysqld_safe< / string>
< string> - bind-address = 127.0.0.1< / string>
< string> - datadir = / usr / local / var / mysql< / string>
< / array>

所以,无论你在任何你的my.cnf文件中做什么,它总是被绑定到127.0.0.1,您将无法从容器查询。我的修复只是直接编辑这个文件不提供绑定地址,所以我们可以让/etc/my.cnf为我们做。或者,虽然我不推荐它,您可以直接在此文件中更改绑定地址。


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!

  <array>
    <string>/usr/local/opt/mysql/bin/mysqld_safe</string>
    <string>--bind-address=127.0.0.1</string>
    <string>--datadir=/usr/local/var/mysql</string>
  </array>

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.

这篇关于通过Docker连接到localhost上的MySQL服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆