0

I am having problems connecting to a MySQL server throught my Spring Boot application. I have a docker-compose file that unites a MySQL and PhpMyAdmin images, that runs perfectly fine on my local machine.

Here is the docker-compose file:

version: '3'
 
services:
  db:
    image: mysql:latest
    container_name: db
    environment:
      MYSQL_ROOT_PASSWORD: my_secret_password
      MYSQL_DATABASE: app_db
      MYSQL_USER: db_user
      MYSQL_PASSWORD: db_user_pass
    ports:
      - "6033:3306"
    volumes:
      - dbdata:/var/lib/mysql

  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    container_name: pma
    links:
      - db
    environment:
      PMA_HOST: db
      PMA_PORT: 3306
      PMA_ARBITRARY: 1
    restart: always
    ports:
      - 8081:80
      
volumes:
  dbdata:

And here is my application.properties file:

enter image description here

I also tried using 'localhost' instead of 'db' and tried with both passwords but to no success.Here are my dependencies in the pom.xml file:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>
4
  • You're attempting to use db as a hostname in the connection string. That would only work if the Java application also runs via the same docker-compose. Seeing your docker-compose yaml, your connection string might need to be jdbc:mysql://localhost:6033/app_db?useSSL=false. Commented Dec 13, 2020 at 16:45
  • I tried using both 'db' and 'localhost' but nothing seemed to work Commented Dec 13, 2020 at 17:02
  • I think you should try to use localhost with port 6033 instead of 3306, since according to your dockerfile, you're publishing port 3306 of the MySQL container to port 6033 on the host. Commented Dec 13, 2020 at 17:07
  • tried that as well Commented Dec 13, 2020 at 18:24

1 Answer 1

2

Changing the port to 6033 and the host to 'localhost' managed to change the error. In the end I was able to connect to the MySQL server with 'root' as a username:

enter image description here

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

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.