如何将mysql工作台连接到Docker中运行mysql? [英] How to connect mysql workbench to running mysql inside docker?
问题描述
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 instancenkratzke/EasyMySQL/start-database.sh
, which is called by theDockerfile 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屋!