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:
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>


dbas a hostname in the connection string. That would only work if the Java application also runs via the samedocker-compose. Seeing your docker-compose yaml, your connection string might need to bejdbc:mysql://localhost:6033/app_db?useSSL=false.localhostwith port6033instead of3306, since according to your dockerfile, you're publishing port3306of the MySQL container to port6033on the host.