简体   繁体   中英

Springboot MySQL + Docker Access Denied Error

i've been struggling for a while trying to troubleshoot how to fix this error.

I have a Springboot and MySQL project im trying to dockerize, my MySQL container starts up fine and when checking the logs using docker container logs mysql-standalone , i don't get errors.

The issue is when I try docker container logs springboot-docker (My springboot image), I get this error:

java.sql.SQLSyntaxErrorException: Access denied for user 'demo'@'%' to database 'docker_demo'
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120) ~[mysql-connector-java-8.0.27.jar!/:8.0.27]
    at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122) ~[mysql-connector-java-8.0.27.jar!/:8.0.27]
    at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:828) ~[mysql-connector-java-8.0.27.jar!/:8.0.27]
    at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:448) ~[mysql-connector-java-8.0.27.jar!/:8.0.27]
    at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:241) ~[mysql-connector-java-8.0.27.jar!/:8.0.27]
    at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:198) ~[mysql-connector-java-8.0.27.jar!/:8.0.27]
    at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:138) ~[HikariCP-4.0.3.jar!/:na]
    at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:364) ~[HikariCP-4.0.3.jar!/:na]
    at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:206) ~[HikariCP-4.0.3.jar!/:na]
    at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:476) ~[HikariCP-4.0.3.jar!/:na]
    at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:561) ~[HikariCP-4.0.3.jar!/:na]
    at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:115) ~[HikariCP-4.0.3.jar!/:na]
    at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112) ~[HikariCP-4.0.3.jar!/:na]

I have a demo MySQL instance I created instead of using the root instance.

I have tried logging into MySQL shell as root and Granting access to all databases within my demo MySQL instance but I still get the same error.

GRANT ALL ON *.* TO 'demo'@'localhost'; Query OK, 0 rows affected (0.02 sec)


Dockerfile

1 FROM adoptopenjdk/openjdk11:alpine-jre
  2 ADD target/docker-0.0.1-SNAPSHOT.jar springboot-mysql-docker.jar
  3 EXPOSE 8089
  4 ENTRYPOINT ["java","-jar","springboot-mysql-docker.jar"]
~                                                                

Docker images

Docker 镜像


Docker ps -a

码头工人ps -a


I've had plenty of errors but have seemed to fixed them, it seems like this is the last issue I need to fix to finally be able to run my project in a container.

It seems the problem is with your grant statement. You are granting permissions to demo user using the statement GRANT ALL ON *.* TO 'demo'@'localhost'; So the privileges are applicable to demo user only when that user is connected through locally ie. connecting from the application on the same machine, as specified by @'localhost'

update your grant statement like this, GRANT ALL ON *.* TO 'demo'@'%';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM