'Spring Boot JDBC microservices cannot connect to MySQL in docker

I have 2 Spring Boot microservices that need to connect to a MySQL service which itself is deployed in Docker. I'm using Docker Compose but for some reason they cannot reach the DB. I have used the service name in the connection string but still no connection.

Here is the docker-compose.yml file:

version: "3.8"
services:
  mysqldb:
    container_name: mysqldb
    image: mysql:8
    restart: unless-stopped
    env_file: ./.env
    environment:
      - MYSQL_ROOT_PASSWORD=eryrty45fgd3DE
      - MYSQL_DATABASE=bank_db
      - MYSQL_USER=java_user
      - MYSQL_PASSWORD=dd0953
    ports:
      - 3307:3306
    volumes:
      - db:/var/lib/mysql
      - ./scripts/schema.sql:/docker-entrypoint-initdb.d/1.sql
  accounts-service:
    container_name: accounts-service
    depends_on:
      - mysqldb
    build: 
        context: ./accounts-service
        dockerfile: Dockerfile
    image: accounts-service:latest
    restart: on-failure
    env_file: ./.env
    ports:
      - 8000:8000
    environment:
      - MYSQLDB_URL=jdbc:mysql://mysqldb:3306/bank_db?useSSL=false
      - MYSQLDB_USER=java_user
      - MYSQL_PASSWORD=dd0953
      - NOTIFY_SERVICE_URL=http://notify-service:8002
    volumes:
      - servs:/tmp
    stdin_open: true
    tty: true
  transactions-service:
    container_name: transactions-service
    depends_on:
      - mysqldb
      - accounts-service
      - notify-service
    build: 
        context: ./transaction-service
        dockerfile: Dockerfile
    image: transactions-service:latest
    restart: on-failure
    env_file: ./.env
    ports:
      - 8001:8001
    environment:
      - MYSQLDB_URL=jdbc:mysql://mysqldb:3306/bank_db?useSSL=false
      - MYSQLDB_USER=java_user
      - MYSQL_PASSWORD=dd0953
      - NOTIFY_SERVICE_URL=http://notify-service:8002
      - ACCOUNT_SERVICE_URL=http://accounts-service:8000
    volumes:
      - servs:/tmp
    stdin_open: true
    tty: true
  notify-service:
    container_name: notify-service
    build: 
        context: ./notification-service
        dockerfile: Dockerfile
    image: notify-service:latest
    restart: on-failure
    ports:
      - 8002:8002
    volumes:
      - servs:/tmp
    stdin_open: true
    tty: true
volumes:
  db:
  servs:

Here is my connection string:

    spring.datasource.url:jdbc:mysql://mysqldb:3306/bank_db?useSSL=false

The stacktrace:

Caused by: java.net.ConnectException: Connection refused
accounts-service        |   at java.base/sun.nio.ch.Net.pollConnect(Native Method) ~[na:na]
accounts-service        |   at java.base/sun.nio.ch.Net.pollConnectNow(Net.java:672) ~[na:na]
accounts-service        |   at java.base/sun.nio.ch.NioSocketImpl.timedFinishConnect(NioSocketImpl.java:542) ~[na:na]
accounts-service        |   at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:597) ~[na:na]
accounts-service        |   at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:327) ~[na:na]
accounts-service        |   at java.base/java.net.Socket.connect(Socket.java:633) ~[na:na]
accounts-service        |   at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:156) ~[mysql-connector-java-8.0.28.jar!/:8.0.28]
accounts-service        |   at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:63) ~[mysql-connector-java-8.0.28.jar!/:8.0.28]
accounts-service        |   ... 127 common frames omitted


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source