Search code examples
mysqldockerboot2docker

Connecting to MySQL Server on localhost through Docker


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.


Solution

  • 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.