title: Spring Boot unable to communicate with MySQL DB in docker environment
body: <p>I am trying to run a springboot microservice docker image. It fetches the DB connection properties from a Config server. However it is unable to connect to the container.</p>
  <p><strong>Error :</strong></p>
  <pre><code>JdbcEnvironmentInitiator - HHH000342: Could not obtain connection to query metadata : Communications link failure
  The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
  </code></pre>
  <p><strong>Exception :</strong></p>
  <pre><code>at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174) ~[mysql-connector-java-8.0.21.jar!/:8.0.21]
  Caused by :
  at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_242]
  2020-09-28 11:39:32.782 [ : ] WARN  [task-1] o.h.e.j.s.SqlExceptionHelper - SQL Error: 0, SQLState: 08S01
   1 --- [         task-1] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 0, SQLState: 08S01
  2020-09-28 11:39:32.791 [ : ] ERROR [task-1] o.h.e.j.s.SqlExceptionHelper - Communications link failure
  
  The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
   1 --- [         task-1] o.h.engine.jdbc.spi.SqlExceptionHelper   : Communications link failure
  
  The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
  </code></pre>
  <p><strong>Properties located at Config Server</strong></p>
  <pre><code>shopping-service.datasource.url: jdbc:mysql://A.B.C.D:3306/shoppingCartDB
  shopping-service.datasource.username: root
  shopping-service.datasource.password: root
  </code></pre>
  <p><code>A.B.C.D</code> is my docker host IP.</p>
  <p><strong>application.yaml</strong></p>
  <pre><code>datasource:
      url: ${shopping-service.datasource.url}
      username: ${shopping-service.datasource.username}
      password: ${shopping-service.datasource.password}
      #driver-class-name: com.mysql.jdbc.Driver
    jpa:
      generate-ddl: true
      properties:
        hibernate:
          dialect: org.hibernate.dialect.MySQL8Dialect
          show_sql: true
          ddl-auto: create-drop
    profiles:
      active: dev
  </code></pre>
  <p>I have pulled the MySQL 8.0 docker image from docker hub.</p>
  <pre><code>docker pull mysql/mysql-server:8.0
  docker run --name=mysql-container -d mysql/mysql-server:8.0
  </code></pre>
  <p>Changed the password to &quot;root&quot;.</p>
  <pre><code>mysql&gt; ALTER USER 'root'@'localhost' IDENTIFIED BY 'root';
  Query OK, 0 rows affected (0.02 sec)
  </code></pre>
  <p>Created the database :</p>
  <pre><code>mysql&gt; create DATABASE shoppingCartDB;
  Query OK, 1 row affected (0.00 sec)
  
  mysql&gt; exit
  </code></pre>
  <p><strong>Docker RUN</strong></p>
  <pre><code>docker run -p 5000:5000 shoppingms:latest --env shopping-service.configserverurl=http://A.B.C.D:8888 --env shopping-service.eureka.url=http://A.B.C.D:4444/eureka
  </code></pre>
  <p>It is able to fetch the properties from config server. I checked the logs. Error came from this line :</p>
  <pre><code> com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
  </code></pre>
  <p>In my local environment I am able to use the locally installed MySQL Db with the same connection parameters. However in docker I am getting the exceptions. Where I am going wrong can anyone please help me out.</p>
  <p><strong>UPDATE :</strong></p>
  <p>After exposing the MySQL container to port 3306, the error stands at now :</p>
  <pre><code>2020-09-28 12:07:43.942 [ : ] WARN  [task-1] o.h.e.j.e.i.JdbcEnvironmentInitiator - HHH000342: Could not obtain connection to query metadata : null,  message from server: &quot;Host 'Some-IP' is not allowed to connect to this MySQL server&quot;
       1 --- [         task-1] o.h.e.j.e.i.JdbcEnvironmentInitiator     : HHH000342: Could not obtain connection to query metadata : null,  message from server: &quot;Host 'Some-IP' is not allowed to connect to this MySQL server&quot;
  
  2020-09-28 12:07:49.219 [ : ] WARN  [task-1] o.h.e.j.s.SqlExceptionHelper - SQL Error: 1130, SQLState: HY000
   1 --- [         task-1] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 1130, SQLState: HY000
  2020-09-28 12:07:49.225 [ : ] ERROR [task-1] o.h.e.j.s.SqlExceptionHelper - null,  message from server: &quot;Host 'Some-IP' is not allowed to connect to this MySQL server&quot;
   1 --- [         task-1] o.h.engine.jdbc.spi.SqlExceptionHelper   : null,  message from server: &quot;Host 'Some-IP' is not allowed to connect to this MySQL server&quot;
  </code></pre>