0

I have setup a spring-boot docker container in an EC2 instance(EC2_IP) and I have a MySQL hosted in a different VM. I verified that that the mysql host(MYSQL_IP) is accessible from the EC2 instance hosting the docker container.

I am passing the spring.datasource.url parameters using the docker environment variables during docker run.

The spring boot app fails complaining with an error message access denied to user db_user@EC2_IP. This is the part I am unable to understand and fix. I don't understand why its trying to connect to the EC2_IP instead of db_user@MYSQL_IP.

I did a docker inspect and I verified that the environment variable for spring.datasoure.url is passed correctly and it is db_user@MYSQL_IP:3306.

I have spent hours trying to fix this problem, but no luck. Any help is appreciated.

To Clarify based on the comments, I have the datasource configured correctly.

Inside docker inspect, value of Args:

-Dspring.datasource.url=jdbc:mysql://MYSQL_IP:3306/test

Also, I checked if I pass some invalid IP (eg) some random text, then it throws an error saying host is invalid (This confirms, it is taking the host I pass in). However, if I configure to an external IP, it seems to resolve to the Host IP address (EC2_IP).

2
  • Are you able to go inside your sprint-boot docker container and perform a telnet sqlhost 3306? Commented Aug 18, 2017 at 6:42
  • what does the spring.datasource.url look like , with password and user masked out ? Commented Aug 18, 2017 at 6:45

3 Answers 3

2

You need to set both

spring.datasource.url=jdbc:mysql://localhost/test
spring.datasource.username=dbuser
spring.datasource.password=dbpass

as specified here

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

Comments

1

Finally figured the problem, its an issue with my DB user permissions. It turned out to be an issue with my mysql user permissions. It wasn't an issue with the docker or springboot.

Access denied for user 'test'@'localhost' (using password: YES) except root user

If only I focussed on the error message clearly and understood what it said, I would have not wasted everyone's time.

1 Comment

I am having the same issue, can you tell which exact answer solved your issue?
1

The issue seems to be in spring.datasource.url; it's not supposed to be of the format:

db_user@db_host:port

but rather of:

driver_class:db_type://db_host:db_port/db_name

for example:

jdbc\:mysql\://localhost\:3306/test

Note the escape backslash \ before each colon :.

If using the default mysql port, you can leave out the :3306 part:

jdbc\:mysql\://localhost/test

For reference, check the official documentation here.

A similar working scenario would be:

1- In application.properties:

spring.datasource.url=jdbc\:mysql\://${DATABASE_HOST}\:${DATABASE_PORT}/${DATABASE_NAME}
spring.datasource.name=${DATABASE_USER}
spring.datasource.password=${DATABASE_PASSWORD}

2- When running docker run:

docker run -p 8080:8080 image_name_or_id \
  -e DATABASE_HOST='MYSQL_IP' \
  -e DATABASE_USER='db_user' \
  -e DATABASE_PASSWORD='db_pass_for_db_user' \
  -e DATABASE_NAME='db_name' \
  -e DATABASE_PORT='3306'

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.