简体   繁体   English

在docker中连接到mysql服务器

[英]connect to mysql server in docker

If I run mysql server in first docker container: 如果我在第一个Docker容器中运行mysql服务器:

docker run --name mysqlserver -e MYSQL_ROOT_PASSWORD=pass -d mysql:5.5

and link second docker container with first container: 并将第二个docker容器与第一个容器链接:

docker run --name myapp --link mysqlserver:mysql -d vladimir/app

and if in second docker container I have python app, where I just want to connect to the database in first container, how to determine what host to use? 如果在第二个Docker容器中我有python应用程序,而我只想连接到第一个容器中的数据库,那么如何确定要使用的主机?

I am not too familiar with mysql. 我对mysql不太熟悉。 Looking at the mysql documentation it seems like this might be the sort of command you want: 查看mysql文档,看来这可能是您想要的命令:

import _mysql
db=_mysql.connect(host="mysql",port=3306,passwd="...",db="...")

when you did the --link in your apps run, you declared that you are linking the EXPOSEd ports for the container named mysqlserver to the container to be named myapp. 当您在应用程序中运行--link时,声明您正在将名为mysqlserver的容器的EXPOSEd端口链接到名为myapp的容器。 Docker updates the /etc/hosts file. Docker更新/ etc / hosts文件。 So, if you cat the /etc/hosts file you should see a line that looks like: 因此,如果对/ etc / hosts文件进行分类,您应该会看到如下所示的行:

172.17.0.26 mysql 5c960433e26a mysqlserver

so this is why the reference in your python to host 'mysql' works. 所以这就是为什么您的python中引用托管“ mysql”的作品的原因。 The default port is 3306. So, that should connect you. 默认端口是3306。因此,应该可以连接到您。 Use this host and port for whatever mysql connection package you are using from python. 将此主机和端口用于您从python使用的任何mysql连接包。

This is how I connect with other objects, on their well known ports. 这就是我与其他对象(在其众所周知的端口上)连接的方式。 You can also use environment variables, but, I think this might be being phased out...dunno... 您也可以使用环境变量,但是,我认为这可能正在逐步淘汰中。

root@d23d8be2a6fa:/# env | grep MYSQL
MYSQL_ENV_MYSQL_ROOT_PASSWORD=pass
MYSQL_PORT_3306_TCP_PORT=3306
MYSQL_PORT_3306_TCP=tcp://172.17.0.32:3306
MYSQL_ENV_MYSQL_VERSION=5.5.42
MYSQL_NAME=/myapp/mysql
MYSQL_PORT_3306_TCP_PROTO=tcp
MYSQL_PORT_3306_TCP_ADDR=172.17.0.32
MYSQL_ENV_MYSQL_MAJOR=5.5
MYSQL_PORT=tcp://172.17.0.32:3306

so you could modify your connect line to use the environment: 因此您可以修改连接线路以使用环境:

import os
myhost = os.environ.get('MYSQL_PORT_3306_TCP_ADDR')
myport = int(os.environ.get('MYSQL_PORT_3306_TCP_PORT'))
import _mysql
db=_mysql.connect(host=myhost,port=myport,passwd="...",db="...")

Either way. 无论哪种方式。 This is discovery. 这是发现。 There are more complicated methods as well, using consul, etcd, skydns, registrator. 还有更多更复杂的方法,使用领事,etcd,skydns,注册器。 I like the skydns/registrator pairing, registrator watches your docker daemon, when new containers are created it inserts dns records into etcd, which skydns can use to mimic a dns server. 我喜欢skydns / registrator配对,注册者监视您的docker守护进程,当创建新容器时,它将dns记录插入etcd中,skydns可以用来模仿dns服务器。 Using this technique you get ip and port as well. 使用此技术,您还将获得ip和port。

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

相关问题 如何在docker容器中连接外部mysql服务器 - How to connect external mysql server in docker container mysql 错误:2003:无法连接到“%-.100s:%u”上的 MySQL 服务器(docker) - mysql error:2003: Can't connect to MySQL server on '%-.100s:%u' (docker) Docker MYSQL [2003] 无法连接到 MySQL 服务器(111 连接被拒绝) - Docker MYSQL [2003] Can't connect to MySQL server (111 Connection refused) 在docker中使用Mysql无法在\\'db \\'Django-Restframework上连接到MySQL服务器 - 'Can\'t connect to MySQL server on \'db\' Django-Restframework with Mysql in docker 将 python 脚本连接到 docker 中的 mysql - Connect python script to mysql in docker 连接到托管在远程服务器上的Docker - connect to docker hosted on remote server Mariadb docker容器无法使用Python连接到主机上的MySQL服务器(111连接被拒绝) - Mariadb docker container Can't connect to MySQL server on host (111 Connection refused) with Python 在Docker上使用Django时出错-“无法在'127.0.0.1'(111)上连接到MySQL服务器”) - Error using Django with Docker - “Can't connect to MySQL server on '127.0.0.1' (111)”) Python 无法使用 docker 连接到 mysql - Python can't connect to mysql using docker 运行 python 和 mysql 并出现 docker 错误 - 无法连接 - running python and mysql with docker error - cannot connect
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM