Access denied for user '电脑用户名'@'localhost'
之前没有碰到这个问题,但是这次从gitee上面拉取代码运行,发现存在bug
错误描述
java.sql.SQLException: Access denied for user '10134'@'localhost' (using password: YES)
文件信息
db.properties 注意:我已经设置成root,但是不生效
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/ssmstu?useUnicode=true&characterEncoding=utf8&useSSL=true
username=root
password=root
spring-mapper.xml(部分)
<context:property-placeholder location="classpath:db.properties" />
<!-- 2.连接池-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${driver}" />
<property name="jdbcUrl" value="${url}"/>
<property name="user" value="${username}" />
<property name="password" value="${password}"/>
</bean>
原因
<context location="classpath:jdbc.properties">
少了一条属性system-properties-mode="FALLBACK",
系统默认为system-properties-mode="ENVIRONMENT" 意思就是从系统环境中去读取,把电脑名当做mysql的用户名,
修改之后,运行就可以成功。
可能和spring底层org.springframework.beans.factory.config.PropertyPlaceholderConfigurer读取properties实现有关,默认从系统中读取真实用户名(username)。
操作
第一种
spring-mapper.xml中的value="${username}"
直接改成value="root"
第二种
将db.properties 和 spring-mapper.xml 都进行修改,不用username,比如可以使用mysql.uername
标识
第三种
使用原因说到的方法,spring-mapper.xml 添加属性。