docker-compose运行两个mysql实例 [英] docker-compose run two instance of mysql
问题描述
我想使用docker-compose运行两个mysql实例。
I want to run two instances of mysql using docker-compose.
我正在Docker容器中运行MySQL,并且我有另一个运行python脚本的Docker容器
I'm running MySQL in a Docker container and I have another Docker container running a python script to access the MySQL database.
一个在端口3306上正常工作。为了使两个都能正常工作,我想我会在另一个端口上运行另一个。但是,当我将其更改为其他端口(例如6603)时,却出现以下错误:
One works fine on port 3306. In order to get two working, I thought I would just run the other one on a different port. But when I change it to a different port (e.g. 6603), but when I do, I get the below error:
mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on 'mysql:6603' (111 Connection refused)
我已经阅读了所有问题,因此我发现这似乎很相关,但是所有解决方案均无效。我认为可以解决此问题的方法是更改一行或两行配置,但是到目前为止,我已经花了很多时间,所以我们将不胜感激。
I have read every question on s.o. I can find that seems relevant but none of the solutions work. I feel certain the fix will involve changing a line or two of configuration but I've spent many hours on this so far so any help would be greatly appreciated.
下面是docker-compose脚本(如果将6603替换为3306,可以正常工作)。
The docker-compose script is below (works fine if 6603 is replaced with 3306).
server:
build:
context: ../
dockerfile: Docker/ServerDockerfile
ports:
- "8080:8080"
links:
- mysql:mysql
volumes:
- ../py:/app
tty: true
mysql:
image: mysql
expose:
- "6603"
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: project
volumes:
- ./MySQL:/docker-entrypoint-initdb.d
- ./MySQL/data:/var/lib/mysql
正在像这样被访问:
cnx = mysql.connector.connect(user='root', password='password',
host='mysql',
port="6603",
database='project')
推荐答案
最终找到了很多工作方式。最简洁的方法是为每个应用程序创建一个网络并将容器连接到该网络。
Have eventually found a couple of ways that work. The neatest one is for each app to create a network and connect the containers to it.
如果每个应用程序使用不同的网络,则mysql可以在该Docker网络上的3306上运行,并且可以从app1和mysql2:// 3306在mysql:// 3306上访问来自app2。 (假设您为应用程序2提供的mysql服务是mysql2)。
If each app uses a different network then mysql can run on 3306 on that Docker network and can be accessed on mysql://3306 from app1 and mysql2://3306 from app2. (Assuming you name you give the mysql service for app 2 is mysql2).
带有新行的Docker文件如下:
The Docker file with the new lines is below:
server:
build:
context: ../
dockerfile: Docker/ServerDockerfile
ports:
- "8080:8080"
volumes:
- ../py:/app
tty: true
networks:
-net
mysql2:
image: mysql
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: project
volumes:
- ./MySQL:/docker-entrypoint-initdb.d
- ./MySQL/data:/var/lib/mysql
networks:
-net
networks:
net:
driver: bridge
第二个应用程序的Docker文件是相同的,除了名称不同(为了简单起见,我在每个后面都放了2)。
The Docker file for the second app is identical except the names are different (I put a 2 after each for simplicity).
server2:
build:
context: ../
dockerfile: Docker/ServerDockerfile
ports:
- "8081:8080"
volumes:
- ../py:/app
tty: true
networks:
-net2
mysql2:
image: mysql
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: project
volumes:
- ./MySQL:/docker-entrypoint-initdb.d
- ./MySQL/data:/var/lib/mysql
networks:
-net2
networks:
net2:
driver: bridge
这篇关于docker-compose运行两个mysql实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!