Hibernate能够连接到mysql,但是Spring JPA无法连接

时间:2018-12-09 13:49:03

标签: mysql spring hibernate spring-boot

我正在开发一个已经可以与hibernate一起使用的基本应用程序,并希望添加spring JPA功能以使其更简单。

当我使用休眠进行测试时,我可以无任何问题地连接到mysql数据库。但是,当我从Spring Boot测试应用程序时,数据库访问失败。我在hibernate.cfg.xml和application.properties中都提供了相同的凭据。但不确定为什么弹簧访问会失败。

我尝试将特权授予用户。但仍然无法正常工作。

# sudo certbot --apache -d products-catalog.nilov-sergey-demo-apps.tk -d www.products-catalog.nilov-sergey-demo-apps.tk 
Saving debug log to /var/log/letsencrypt/letsencrypt.log 
Plugins selected: Authenticator apache, Installer apache 
Obtaining a new certificate 
Performing the following challenges: 
http-01 challenge for products-catalog.nilov-sergey-demo-apps.tk 
http-01 challenge for www.products-catalog.nilov-sergey-demo-apps.tk 
Waiting for verification... 
Cleaning up challenges 
Failed authorization procedure. www.products-catalog.nilov-sergey-demo-apps.tk (http-01): urn:ietf:params:acme:error:dns :: DNS problem: NXDOMAIN looking up 
A for www.products-catalog.nilov-sergey-demo-apps.tk 

IMPORTANT NOTES: 
 - The following errors were reported by the server: 

   Domain: www.products-catalog.nilov-sergey-demo-apps.tk 
   Type:   None 
   Detail: DNS problem: NXDOMAIN looking up A for 
   www.products-catalog.nilov-sergey-demo-apps.tk

hibernate.cfg.xml

    GRANT ALL PRIVILEGES ON *.* TO 'logesh'@'localhost' WITH GRANT OPTION;

application.properties

    <hibernate-configuration>
    <session-factory>
    <property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/devops</property>
    <property name="hibernate.connection.username">logesh</property>
    <property name="hibernate.connection.password">logesh</property>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
    <property name="hbm2ddl.auto">create</property>
    <property name="hibernate.show_sql">true</property>

以下是我在春季靴中遇到的错误:

    spring.datasource.url=jdbc:mysql://localhost:3306/devops
    spring.datasource.username=logesh
    spring.datasource.password=logesh   
    spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
    spring.jpa.database-platform = org.hibernate.dialect.MySQL5Dialect
    spring.jpa.generate-ddl=true
    spring.jpa.hibernate.ddl-auto = create

以下是MySQL常规日志,在该日志中我们可以看到Spring Boot登录失败。随后,当尝试使用休眠模式时,便可以登录。

    java.sql.SQLException: Access denied for user 'logesh'@'localhost' (using password: YES)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129) ~[mysql-connector-java-8.0.13.jar:8.0.13]
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97) ~[mysql-connector-java-8.0.13.jar:8.0.13]
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122) ~[mysql-connector-java-8.0.13.jar:8.0.13]
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:835) ~[mysql-connector-java-8.0.13.jar:8.0.13]
at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:455) ~[mysql-connector-java-8.0.13.jar:8.0.13]
at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:240) ~[mysql-connector-java-8.0.13.jar:8.0.13]
at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:207) ~[mysql-connector-java-8.0.13.jar:8.0.13]
at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:136) ~[HikariCP-3.2.0.jar:na]
at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:369) ~[HikariCP-3.2.0.jar:na]
at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:198) ~[HikariCP-3.2.0.jar:na]
at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:467) [HikariCP-3.2.0.jar:na]
at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:541) [HikariCP-3.2.0.jar:na]

我被困在这里,将不胜感激。

回复评论:

    2018-12-09T12:48:52.494621Z   150 Connect   logesh@localhost on devops using SSL/TLS
    2018-12-09T12:48:52.494773Z   150 Connect   Access denied for user 'logesh'@'localhost' (using password: YES)
    2018-12-09T12:48:54.243711Z   151 Connect   logesh@localhost on devops using SSL/TLS
    2018-12-09T12:48:54.243881Z   151 Connect   Access denied for user 'logesh'@'localhost' (using password: YES)
    2018-12-09T12:48:56.328126Z   152 Connect   logesh@localhost on devops using SSL/TLS
    2018-12-09T12:48:56.328291Z   152 Connect   Access denied for user 'logesh'@'localhost' (using password: YES)
    2018-12-09T13:10:46.485311Z   153 Connect   root@localhost on devops using SSL/TLS
    2018-12-09T13:10:46.486314Z   153 Query select @@version_comment limit 1
    2018-12-09T13:11:07.220308Z   153 Quit  
    2018-12-09T13:12:52.763898Z   154 Connect   logesh@localhost on devops using SSL/TLS
    2018-12-09T13:12:52.764643Z   154 Query select @@version_comment limit 1
    2018-12-09T13:13:15.186763Z   154 Quit  
    2018-12-09T13:27:57.857455Z   155 Connect   logesh@localhost on devops using SSL/TLS
    2018-12-09T13:27:57.861872Z   155 Query /* mysql-connector-java-8.0.13 (Revision: 66459e9d39c8fd09767992bc592acd2053279be6) */SELECT  @@session.auto_increment_increment AS auto_increment_increment, @@character_set_client AS character_set_client, @@character_set_connection AS character_set_connection, @@character_set_results AS character_set_results, @@character_set_server AS character_set_server, @@collation_server AS collation_server, @@collation_connection AS collation_connection, @@init_connect AS init_connect, @@interactive_timeout AS interactive_timeout, @@license AS license, @@lower_case_table_names AS lower_case_table_names, @@max_allowed_packet AS max_allowed_packet, @@net_write_timeout AS net_write_timeout, @@sql_mode AS sql_mode, @@system_time_zone AS system_time_zone, @@time_zone AS time_zone, @@transaction_isolation AS transaction_isolation, @@wait_timeout AS wait_timeout
    2018-12-09T13:27:57.877332Z   155 Query SET NAMES utf8mb4

我只能执行已经尝试过的第三条命令。

编辑: 这个问题不是Java JDBC Access denied for user的重复,因为我在发布这个问题之前已经尝试了所有推荐的解决方案。

2 个答案:

答案 0 :(得分:1)

用户名后面似乎有一些空格。
您能否检查属性文件中用户名或密码后面是否有空格。如果logesh后面有空格,请将其删除。

答案 1 :(得分:0)

您正在使用哪个版本的Spring Boot和MySQL?我创建了一个最小的应用程序,并且运行正常(源代码为https://github.com/behrangsa/stackoverflow-q53692999)。

还要注意,MySQL的当前JDBC驱动程序类名称为com.mysql.cj.jdbc.Driver

运行MySQL

↪ docker run -P --name some-mysql -e MYSQL_ROOT_PASSWORD=logesh -d mysql:8                                                                                                   00:52:54
cc395e45faa5d1cc3fdff3eeb35a6491202b75862ae5a519901c10fe02a80403
↪ docker ps                                                                                                                                                                  00:53:08
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                               NAMES
cc395e45faa5        mysql:8             "docker-entrypoint.s…"   3 seconds ago       Up 2 seconds        0.0.0.0:32773->3306/tcp, 0.0.0.0:32772->33060/tcp   some-mysql

创建数据库和用户

CREATE DATABASE devops;
CREATE USER 'logesh' IDENTIFIED BY 'logesh';
GRANT ALL ON devops.* TO 'logesh';

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.stackoverflow</groupId>
    <artifactId>q53692999</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>q53692999</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <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>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

配置属性

spring.datasource.url=jdbc:mysql://localhost:32773/devops
spring.datasource.username=logesh
spring.datasource.password=logesh

运行应用程序:

     .   ____          _            __ _ _
    /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
   ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
    \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
     '  |____| .__|_| |_|_| |_\__, | / / / /
    =========|_|==============|___/=/_/_/_/
    :: Spring Boot ::        (v2.1.1.RELEASE)

   2018-12-17 00:51:56.267  INFO 16686 --- [           main] com.stackoverflow.q53692999.Main         : Starting Main on apadana with PID 16686 (~/Documents/projects/q53692999/target/classes started by foo in ~/Documents/projects/q53692999)
   2018-12-17 00:51:56.269  INFO 16686 --- [           main] com.stackoverflow.q53692999.Main         : No active profile set, falling back to default profiles: default
   2018-12-17 00:51:56.553  INFO 16686 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode.
   2018-12-17 00:51:56.568  INFO 16686 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 11ms. Found 0 repository interfaces.
   2018-12-17 00:51:56.777  INFO 16686 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
   2018-12-17 00:51:56.965  INFO 16686 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
   2018-12-17 00:51:56.994  INFO 16686 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [
    name: default
    ...]
   2018-12-17 00:51:57.022  INFO 16686 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate Core {5.3.7.Final}
   2018-12-17 00:51:57.023  INFO 16686 --- [           main] org.hibernate.cfg.Environment            : HHH000206: hibernate.properties not found
   2018-12-17 00:51:57.084  INFO 16686 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.0.4.Final}
   2018-12-17 00:51:57.142  INFO 16686 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
   2018-12-17 00:51:57.444  INFO 16686 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
   2018-12-17 00:51:57.526  INFO 16686 --- [           main] com.stackoverflow.q53692999.Main         : Started Main in 1.446 seconds (JVM running for 1.746)
   2018-12-17 00:51:57.529  INFO 16686 --- [       Thread-5] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
   2018-12-17 00:51:57.531  INFO 16686 --- [       Thread-5] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
   2018-12-17 00:51:57.534  INFO 16686 --- [       Thread-5] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.

   Process finished with exit code 0