How to connect mysql workbench to running mysql inside docker?

匿名 (未验证) 提交于 2019-12-03 02:13:02

问题:

I am using mysql server inside docker container and able to access inside docker. How to create connection in mysql workbench running on my local(Host Machine).

回答1:

2 docker-related conditions:

  • first, your docker run must map the mysql port to an host port:

    docker run -p host:container 

(for instance: docker run -d -p 3306:3306 tutum/mysql)

  • second, if you are using docker in a VM (docker-machine, with boot2docker), you need to use the ip of docker-machine ip <VMname>, with the host mapped port.

    http://$(docker-machine ip <VMname>):hostPort 

If you need to use localhost, you would need to do some port forwarding at the VirtualBox level:

VBoxManage controlvm "boot2docker-vm" natpf1 "tcp-port3306,tcp,,3306,,3306" VBoxManage controlvm "boot2docker-vm" natpf1 "udp-port3306,udp,,3306,,$3306" 

(controlvm if the VM is running, modifyvm is the VM is stopped) (replace "boot2docker-vm" by the name of your vm: see docker-machine ls)


2 mysql-related conditions:

  • As illustrated in nkratzke/EasyMySQL/Dockerfile, you need to enable remote access:

    # Enable remote access (default is localhost only, we change this # otherwise our database would not be reachable from outside the container) RUN sed -i -e"s/^bind-address\s*=\s*127.0.0.1/bind-address = 0.0.0.0/" /etc/mysql/my.cnf 
  • You need to create users when startig your database in your docker image.
    See for instance nkratzke/EasyMySQL/start-database.sh, which is called by the Dockerfile CMD:

    /usr/sbin/mysqld & sleep 5 echo "Creating user" echo "CREATE USER '$user' IDENTIFIED BY '$password'" | mysql --default-character-set=utf8 echo "REVOKE ALL PRIVILEGES ON *.* FROM '$user'@'%'; FLUSH PRIVILEGES" | mysql --default-character-set=utf8 echo "GRANT SELECT ON *.* TO '$user'@'%'; FLUSH PRIVILEGES" | mysql --default-character-set=utf8 echo "finished" 


回答2:

You have to do few configuration in you docker container. Please follow the following steps.

  1. Specify mysql configuration block in your docker-compose.yml. I have following mysql block under services object in my docker-compose.yml file.

    services:     db:         image: mysql         volumes:             - "./.data/db:/var/lib/mysql"         environment:             MYSQL_ROOT_PASSWORD: root             MYSQL_DATABASE: mydb             MYSQL_USER: user             MYSQL_PASSWORD: pass         ports:             42333:3306 
  2. Restart docker container and run following commands to get to the bash shell in the mysql container

    Inside the container, to connect to mysql command line type,

    mysql -u root -p 

    Use MYSQL_ROOT_PASSWORD as specified in the docker-compose.yml . Execute following commands to create new user.

    create user 'user'@'%' identified by 'pass'; grant all privileges on *.* to 'user'@'%' with grant option; flush privileges; 

    The percent sign (%) means all ip's. Restart the docker container.

  3. In your MySQL Workbench provide the connection details. Use MYSQL_PASSWORD as specified in your docker-compose.yml file.

You should now be able to connect to your mysql container.



回答3:

I got solution for this by setting field value in Hostname: 127.0.0.1 (Localhost), port by default 3306 with your creds.



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!