在docker容器中连接到MySQL(Windows / VirtualBox上的Vagrant)

时间:2016-06-09 17:10:06

标签: mysql docker vagrant mysql-workbench docker-container

我正在尝试使用Vagrant和Docker在Windows上创建虚拟化开发环境(和很多人一样)。我遇到的问题是我无法连接(或者我不明白如何)从我的Windows笔记本电脑上运行的MySQL Workbench到Boot2Docker中Docker容器中的MySQL数据库。这就是我可视化连接的方式:

MySQL工作台 - > 3306 - > Boot2Docker - > 3306 - > Docker - > MySQL的

但是我无法从MySQLWorkbench连接到数据库。我已尝试使用Boot2Docker框的私钥“.vagrant \ machines \ dockerhost \ virtualbox \ id”连接到3306上的Boot2Docker主机10.0.2.15和tcp over ssh

我做错了什么/我误解了什么。

我的Vagrantfile:     ENV ['VAGRANT_DEFAULT_PROVIDER'] ='docker'

DOCKER_HOST_NAME = "dockerhost"
DOCKER_HOST_VAGRANTFILE = "./host/Vagrantfile"

Vagrant.configure("2") do |config|

  config.vm.network "forwarded_port", guest: 3306, host: 3306

  config.vm.define "mysql" do |v|
    v.vm.provider "docker" do |d|
      d.image = "mysql"
      d.env = {
        :MYSQL_ROOT_PASSWORD => "root",
        :MYSQL_DATABASE     => "dockertest",
        :MYSQL_USER         => "dockertest",
        :MYSQL_PASSWORD     => "docker"
      }
      d.ports =["3306:3306"]
      d.remains_running = "true"
      d.vagrant_machine = "#{DOCKER_HOST_NAME}"
      d.vagrant_vagrantfile = "#{DOCKER_HOST_VAGRANTFILE}"
    end
  end
end

我的主机/ Vagrantfile(描述我的Boot2docker主机)是:

FORWARD_DOCKER_PORTS='true'
Vagrant.configure(2) do |config|


  config.vm.provision "docker"

  # The following line terminates all ssh connections. Therefore
  # Vagrant will be forced to reconnect.
  # That's a workaround to have the docker command in the PATH

  #Clear any existing ssh connections
  ####NOTE: ps aux seems to give variable results depending on run -> process number can be ####first #or second causing provision to fail!!!
  config.vm.provision "clear-ssh", type: "shell", inline:
        "ps aux | grep 'sshd:' | awk '{print $1}'  | xargs kill"
#        "ps aux | grep 'sshd:' | awk '{print $2}' | xargs kill"


  config.vm.define "dockerhost"
  config.vm.box = "dduportal/boot2docker"
  config.vm.network "forwarded_port",guest: 8080, host: 8080


  config.vm.provider "virtualbox" do |vb|
          vb.name = "dockerhost"

  end

end

1 个答案:

答案 0 :(得分:1)

解决。我怀疑端口没有在主机和docker主机之间转发。解决方案是移动端口转发配置行:

config.vm.network "forwarded_port", guest: 3306, host: 3306

进入docker主机Vagrant文​​件。然后,Windows主机上的MySQL Workbench可以在localhost:3306上连接。