dockerfile mysql例子,docker-compose运行两个mysql实例

I want to run two instances of mysql using docker-compose.

I'm running MySQL in a Docker container and I have another Docker container running a python script to access the MySQL database.

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.

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

And it is being accessed like this:

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.

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

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值