【问题标题】:o.a.tomcat.jdbc.pool.ConnectionPool : Unable to create initial connections of pool. Spring Boot + MySqlo.a.tomcat.jdbc.pool.ConnectionPool :无法创建池的初始连接。 Spring Boot + MySql
【发布时间】:2018-10-23 12:26:41
【问题描述】:

我想借助 MySql WorkBranch 将 Spring Boot 应用程序与数据库连接起来

o.a.tomcat.jdbc.pool.ConnectionPool      : Unable to create initial connections of pool.

是我得到的例外

pom.xml

<dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.27</version>
        </dependency>

application.properties

spring.thymeleaf.mode=LEGACYHTML5
#toa gore mora
# ===============================
# = DATA SOURCE
# ===============================

# Set here configurations for the database connection

# Connection url for the database "netgloo_blog"
spring.datasource.url = jdbc:mysql://localhost:3306/OnlineBanking

# Username and secret
spring.datasource.username = root
spring.datasource.password =

# Keep the connection alive if idle for a long time (needed in production)
spring.datasource.tomcat.max-active=1

# ===============================
# = JPA / HIBERNATE
# ===============================

# Use spring.jpa.properties.* for Hibernate native properties (the prefix is
# stripped before adding them to the entity manager).

# Show or not log for each sql query
spring.jpa.show-sql = true

# Hibernate ddl auto (create, create-drop, update): with "update" the database
# schema will be automatically updated accordingly to java entities found in
# the project
spring.jpa.hibernate.ddl-auto = update

# Allows Hibernate to generate SQL optimized for a particular DBMS
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

在 MySql Workbranch 中设置的服务器是 image:https://i.stack.imgur.com/F3G4s.png

我得到这个错误

2018-05-13 22:04:38.234 ERROR 6176 --- [           main] o.a.tomcat.jdbc.pool.ConnectionPool      : Unable to create initial connections of pool.

com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_162]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_162]
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_162]
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_162]
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411) ~[mysql-connector-java-5.1.27.jar:na]

有人可以帮助将spring boot与数据库连接起来吗 谢谢

【问题讨论】:

  • mysql 端的一切都正确吗?您可以使用 mysql 客户端连接到您的数据库(mysql -u root -h dbhost OnlineBanking)吗?您可能需要为 root@% 用户设置 root 密码。 (mysql 中的用户名始终是用户名 + 主机的组合,在这种情况下,% = 任意)。数据库是否与应用程序位于同一主机上?如果没有,您需要设置 mysql 配置以侦听 127.0.0.1 (=default) 之外的其他 ip,以启用与数据库的远程网络。
  • 当我键入mysql -u root -h hostname在线营业我得到错误主机'hostanme'不允许连接到这个mysql服务器,我的bind-address没有默认值,所以它可能应该倾听所有端口。你知道我该如何解决这个问题非常感谢@StijnHaezebrouck
  • 查看@Daniele 的回答如何解决这个问题:-)。

标签: java mysql spring spring-boot mysql-workbench


【解决方案1】:

假设您在开发环境中并且安全对您来说不是问题,您可以使用以下 SQL 命令创建登录

CREATE USER 'jeffrey'@'*' IDENTIFIED BY 'password';
GRANT ALL ON * TO 'jeffrey'@'*';

另见

https://dev.mysql.com/doc/refman/8.0/en/create-user.htmlhttps://dev.mysql.com/doc/refman/8.0/en/grant.html

【讨论】:

  • 我如何与我通过 application.properties 创建的用户连接并为新创建的用户设置这些变量? spring.datasource.username=newuser spring.datasource.password=newuserpassword 谢谢@Daniele
  • 是的,这就是答案,非常感谢@Daniele
  • 嗯,如果您使用的是 spring-boot 项目,那么您只需编辑您的 application.properties 文件吗?见docs.spring.io/spring-boot/docs/current/reference/html/…
猜你喜欢
  • 2017-02-02
  • 1970-01-01
  • 2019-10-14
  • 1970-01-01
  • 2018-08-08
  • 1970-01-01
  • 2019-06-19
  • 2015-03-31
  • 1970-01-01
相关资源
最近更新 更多