简体   繁体   English

无法连接到在Vagrant VM中作为Docker容器安装的MySql

[英]Can't Connect To MySql Which is Installed As Docker Container Inside Vagrant VM

I try to set up local VPS with vagrant, and got docker installed and setup in this vm. 我尝试用无业游民设置本地VPS,并在此虚拟​​机中安装并设置了docker。

here is my Vagrantfile 这是我的Vagrantfile

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

    config.vm.box = "xenial64"

    config.vm.network :forwarded_port, guest: 80, host: 8080

    config.vm.provision "docker"

    config.vm.provision :shell, :path => "install.sh"

    config.vm.synced_folder ".", "/vagrant", :mount_options => ["dmode=777", "fmode=777"]

    config.vm.provider "virtualbox" do |vb|
        vb.memory = "1024"
        vb.cpus = 2

        vb.customize ["setextradata", :id, "VBoxInternal2/SharedFoldersEnableSymlinksCreate/vagrant", "1"]

    end  
end

and after i vagrant up with this file, i ssh into it then install docker-compose and ran my docker-compose.yml ( here is my lemp stack source ) and everything work fine. 在我对该文件进行修改之后,我将其ssh放入其中,然后安装docker-compose并运行了docker-compose.yml( 这是我的lemp堆栈源代码 ),一切正常。 I tested with index.php i even ran opencart and it works. 我用index.php测试过,甚至运行过opencart,它也能正常工作。

But, the problem is this : i can't connect mysql database from mySql WorkBench or SqlYog as usual by using this crendential : 但是,问题是这样的:我无法像往常一样使用此凭据从mySql WorkBench或SqlYog连接mysql数据库:

SSH Tunnel :
SSH Host Address : 127.0.0.1
Username : vagrant
SSH Port : 2222
Password : vagrant

MySQL :
Mysql Host Address : localhost
Port : 3306
username : root
password : root

i tried to change the mySql host address into 172.18.0.2 which is the ip address of mysql container inside docker when do docker inspect <container id> and does not work. 我试图将mySql主机地址更改为172.18.0.2,这是当docker docker inspect <container id>时docker内部mysql容器的ip地址,并且不起作用。

and here is my docker-compose.yml : 这是我的docker-compose.yml:

version: "2"
services:
  nginx:
    image: nginx
    restart: always
    ports:
      - 8080:80
    volumes:
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
      - ./nginx/log:/var/log/nginx
    volumes_from:
      - fpm
    depends_on:
      - fpm
      - db
    links:
      - db
  fpm:
    build: ./fpm
    restart: always
    volumes:
      - ./app/public:/var/app/public
      - ./app/storage:/var/app/storage
    links:
      - db
  db:
    image: mysql:5.7
    ports:
      - 3306:3306
    environment:
      - MYSQL_ROOT_PASSWORD=root
      - MYSQL_DATABASE=application
    restart: always

can someone please help me ? 有人可以帮帮我吗 ? thank you 谢谢

Now i am answering my own question again.. for development purpose seems i don't need to ssh into vagrant, 现在,我再次回答自己的问题..出于开发目的,我不需要陷入无聊的状态,

let's say i have this setting in my docker-compose.yml : 假设我在docker-compose.yml中具有此设置:

dl_db: image: mysql:5.7 ports: - 3306:3306 environment: - MYSQL_ROOT_PASSWORD=root - MYSQL_DATABASE=oemjih restart: always

then i put this line in my vagrantfile config.vm.network :forwarded_port, guest: 3306, host: 3306 and from my sqlyog panel i just accessing the db via tcp/ip with port:3306 as i mapped in as host port in my vagrantfile without ssh : MySql Host Address: localhost username : root password : root port : 3306 然后我将这一行放在我的vagrantfile config.vm.network :forwarded_port, guest: 3306, host: 3306然后从我的sqlyog面板中,我通过tcp / ip使用port:3306访问数据库,因为我映射为我的host端口不带ssh的vagrantfile: MySql Host Address: localhost username : root password : root port : 3306

and i already put it here : https://github.com/vidihermes/Docker-LEMP-Stack 而且我已经把它放在这里了: https : //github.com/vidihermes/Docker-LEMP-Stack

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM