我有一个非常基本的Spring Boot应用程序,旨在使用JPA / MySQL:
...
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
...
但是,我的MySQL数据库是Docker容器:
docker run --name mysql -e MYSQL_ROOT_PASSWORD=mypass123 -d mysql:8.0.1
下面是我的application.properties文件,用于配置数据库连接:
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://localhost:3306/db_example
spring.datasource.username=springuser
spring.datasource.password=ThePassword
我已经这样设置了数据库和用户:
> docker exec -it mysql bash -l
root@cef13b77d8c6:/# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 8.0.1-dmr MySQL Community Server (GPL)
...
mysql> create database db_example;
Query OK, 1 row affected (0.07 sec)
mysql> create user 'springuser'@'%' identified by 'ThePassword';
Query OK, 0 rows affected (0.06 sec)
mysql> grant all on db_example.* to 'springuser'@'%';
Query OK, 0 rows affected (0.11 sec)
就绪...:)
运行JAR时,我得到Access denied to user 'springuser'@'localhost' (using password: YES)
:
> mvn clean package
...
> java -jar .\target\accessing-data-mysql-0.0.1-SNAPSHOT.jar
...
2019-12-19 11:28:29.659 INFO 42768 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2019-12-19 11:28:29.685 INFO 42768 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2019-12-19 11:28:29.686 INFO 42768 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.22]
2019-12-19 11:28:29.783 INFO 42768 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2019-12-19 11:28:29.783 INFO 42768 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1424 ms
2019-12-19 11:28:29.926 INFO 42768 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2019-12-19 11:28:31.512 ERROR 42768 --- [ main] com.zaxxer.hikari.pool.HikariPool : HikariPool-1 - Exception during pool initialization.
java.sql.SQLException: Access denied for user 'springuser'@'localhost' (using password: YES)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129) ~[mysql-connector-java-8.0.17.jar!/:8.0.17]
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97) ~[mysql-connector-java-8.0.17.jar!/:8.0.17]
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122) ~[mysql-connector-java-8.0.17.jar!/:8.0.17]
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:827) ~[mysql-connector-java-8.0.17.jar!/:8.0.17]
at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:447) ~[mysql-connector-java-8.0.17.jar!/:8.0.17]
...
我可以与此用户一起访问数据库:
> docker exec -it mysql bash -l
root@cef13b77d8c6:/# mysql -uspringuser -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 8.0.1-dmr MySQL Community Server (GPL)
...
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| db_example |
| information_schema |
+--------------------+
2 rows in set (0.00 sec)
看来我的用户存在并且可以通过这种方式访问数据库,而不能从Spring Boot应用程序访问。有什么我想念的吗?我不确定要尝试在Docker容器上连接MySQL的事实是否需要在Spring Boot应用程序中进行额外配置,而不是我遵循的教程所建议的内容,还是我是否需要使容器更多?实例化时可见” ...任何帮助将不胜感激。