Access denied for user 'root'@'localhost' Could not obtain connection
某次在导入新项目时候出现异常:
1.
2017-11-13 10:50:55,437 [WARN]-[org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:204)] HHH000342: Could not obtain connection to query metadata : Access denied for user 'root'@'localhost' (using password: YES)
HHH000342: Could not obtain connection to query metadata : Access denied for user 'root'@'localhost' (using password: YES)
项目使用的框架是spring 和 jpa,连接池使用c3p0,
以上问题可能有以下原因造成:
1,驱动配置有误:driver=com.mysql.jdbc.Driver
2,数据库连接地址有误:url=jdbc:mysql://localhost:3306/test?3useUnicode=true&characterEncoding=utf8
3,密码或帐号有误:username=root password=root
4,数据库未启动或无权访问
5,项目未引入对应的驱动jar包mysql-connector-java-5.x.x-bin.jar
6,mysql root没有远程访问的权限,需要增加权限,增加权限的步骤如下:
进入mysql数据库:
grant all privileges on . to ‘root’@’%’ identified by ‘root’ with grant option;
flush privileges;
将以上步骤都做完后,发现还是没有解决问题。
然后重新查看配置文件,在applicationContext.xml中数据库连接池的配置是这样的:
<context:property-placeholder location="classpath:db_dev.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"/>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"/>
<property name="user" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
<property name="initialPoolSize" value="${jdbc.initPoolSize}"/>
<property name="maxPoolSize" value="${jdbc.maxPoolSize}"/>
</bean>
看到这个正常反应是配置应该都在properties里面了,可是修改properties也是出现一样的问题。
之后无意在resources文件夹下发现有个META-INF的文件夹,打开发现有个配置文件persistence.xml,里面的配置如下:
<?xml version="1.0"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
<persistence-unit name="persistenceUnit" transaction-type="RESOURCE_LOCAL">
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
<property name="hibernate.connection.driver_class" value="org.gjt.mm.mysqlDriver" />
<property name="hibernate.connection.username" value="root" />
<property name="hibernate.connection.password" value="!Cascade020" />
<property name="hibernate.connection.url" value="jdbc.mysql://localhost:3306/mmscm"/>
<property name="hibernate.max_fetch_depth" value="3" />
<property name="hibernate.show_sql" value="true" />
<!--<property name="hibernate.hbm2ddl.auto" value="create-drop"/>-->
</properties>
</persistence-unit>
</persistence>
看到这个<persistence-unit name="persistenceUnit"觉得有点眼熟,所以重新查看applicationContext.xml里面的配置,发现有一个这样的配置:
<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
id="entityManagerFactory">
<property name="persistenceUnitName" value="persistenceUnit" />
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
<property name="packagesToScan" value="com.csl.mmscm.model.*" />
</bean>
看到这里后,觉得数据库的配置很有可能是使用了persistence.xml里面的配置来连接数据库的,然后修改了里面的配置,重新运行Tomcat,然后发现报错没有了,打开首页,也能成功登录了,问题解决了。