0

I'm running application from docker container, that attempts to connect with mysql host

mysql host is localhost and my docker container running on localhost:8080

and here's my config.yml file

database:
  driverClass: com.mysql.cj.jdbc.Driver
  # TODO: if running locally without k8s, change the service url to your localhost
  url: jdbc:mysql://localhost:3306/locations?useSSL=false
  user: root
  password: root
  maxWaitForConnection: 1s
  validationQuery: "SELECT now()"
  validationQueryTimeout: 3s
  minSize: 8
  maxSize: 32
  checkConnectionWhileIdle: false
  evictionInterval: 10s
  minIdleTime: 1 minute
  checkConnectionOnBorrow: true

When i tried to access my app it always gives me the exception

ERROR [2018-02-08 18:16:56,508] org.apache.tomcat.jdbc.pool.ConnectionPool: Unable to create initial connections of pool.
! java.net.ConnectException: Connection refused (Connection refused)

I've tried many solution over google :D, but nothing works for me!

my java connector version:

compile group: 'mysql', name: 'mysql-connector-java', version: '8.0.9-rc'

Can anybody help! Also i'm running on ubuntu,

When i tried to run my app from the IDE, it works well, but from docker container gives the exception!

M1M6
  • 915
  • 3
  • 17
  • 33

1 Answers1

1

If your mysql is running as a docker container, then you may need to link your docker container with your mysql container:

docker run --link mysql_container_name:db your_application

You can then modify your jdbc url to say jdbc:mysql://db:3306/locations?useSSL=false. This is because you have linked your two docker containers together, and established db as a hostname within docker that points to your mysql container.

If your mysql database is not running in a docker container but on your local machine, then it gets a bit more complicated. If you are not on a linux machine, then your Docker containers are technically running in a virtual machine for Docker. localhost from the container's perspective is, therefore, the docker virtual machine and not your physical machine where MySQL is running.

If this is the case, an easy fix would be to use your machine's network IP address instead of localhost in your jdbc URL. Supposing your machine's IP address is 192.168.0.10:

jdbc:mysql://192.168.0.10:3306/locations?useSSL=false

brentjanderson
  • 316
  • 2
  • 4