1

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')
2
  • 3
    Try "6603:3306". Commented Mar 1, 2018 at 21:41
  • How? I have tried expose: -"6603:3306" but that is invalid (gives the error services.mysql.expose is invalid: should be of the format 'PORT[/PROTOCOL]') And I tried using ports: - "6603:3306" but that didn't change the original result (connection refused). Commented Mar 2, 2018 at 7:19

2 Answers 2

0

Try to specify another port for MySQL by modifying its my.cnf file.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.