如何将mysql工作台连接到Docker中运行mysql? [英] How to connect mysql workbench to running mysql inside docker?

查看:285
本文介绍了如何将mysql工作台连接到Docker中运行mysql?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Docker容器中使用mysql服务器,并能在docker内访问。如何在我的本地(主机)上运行的mysql工作台中创建连接。

解决方案

2 docker相关条件:




    首先,您的码头运行必须将mysql端口映射到主机端口:

      docker run -p host :container 




(例如: docker run -d -p 3306:3306 tutum / mysql




  • 如果您在VM中使用docker(docker-machine,使用boot2docker),则需要使用主机映射的 docker-machine ip< VMname> 的ip端口。

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




如果需要使用 localhost ,您需要在VirtualBox级别进行某些端口转发

  VBoxManage controlvmboot2docker-vmnatpf1tcp-port3306,tcp ,, 3306,,3306
VBoxManage controlvmboot2docker-v mnatpf1udp-port3306,udp ,, 3306 ,, $ 3306

controlvm 如果虚拟机正在运行, modifyvm 是VM已停止)
(替换 boot2docker -vm 通过您的vm的名称:见 docker-machine ls






2 mysql相关条件:




  • nkratzke / EasyMySQL / Dockerfile ,您需要启用远程访问:

     #启用远程访问(默认仅为localhost,我们更改此
    #否则我们的数据库不能从容器外部访问)
    运行sed -i -es / ^ bind-address\s * = \s * 127.0.0.1 / bind-address = 0.0.0.0/ /etc/mysql/my.cnf


  • 您需要在startig创建用户数据库在您的码头图像。

    Se e例如 nkratzke / EasyMySQL /start-database.sh ,由 Dockerfile CMD

      usr / sbin / mysqld& 
    sleep 5
    echo创建用户
    echoCREATE USER'$ user'IDENTIFIED BY'$ password'| mysql --default-character-set = utf8
    echoREVOKE ALL PRIVILEGES ON *。* FROM'$ user'@'%'; FLUSH PRIVILEGES| mysql --default-character-set = utf8
    echoGRANT SELECT ON *。* TO'$ user'@'%'; FLUSH PRIVILEGES| mysql --default-character-set = utf8
    echofinished



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

解决方案

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"
    

这篇关于如何将mysql工作台连接到Docker中运行mysql?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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