[英]Could not establish JDBC connection in Spring MVC
我想將我的 Spring MVC 應用程序連接到我的本地數據庫,但出現此錯誤
Unexpected error occurred in scheduled task.: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Unable to load authentication plugin 'caching_sha2_password'.)
但是當我將它與另一台服務器上的數據庫連接時,它連接成功。 這是我的 spring-database.xml 文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
">
<tx:annotation-driven proxy-target-class="true" transaction-manager="transactionManager" />
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<constructor-arg name="dataSource" ref="ds" ></constructor-arg>
</bean>
<bean id="ds" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/expense"></property>
<property name="username" value="root"></property>
<property name="password" value="shahrukh"></property>
<property name="removeAbandoned" value="true"/>
<property name="initialSize" value="20" />
<property name="maxActive" value="30" />
</bean>
<bean id="jt" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="ds"></property>
</bean>
<bean id="securitydao" class="com.expense.DaoImpl.SecurityDaoImpl">
<property name="template" ref="jt"></property>
</bean>
<bean id="commondao" class="com.expense.DaoImpl.CommonDaoImpl">
<property name="template" ref="jt"></property>
</bean>
<bean id="expdao" class="com.expense.DaoImpl.ExpenseDaoImpl">
<property name="template" ref="jt"></property>
</bean>
<bean id="actiondao" class="com.expense.DaoImpl.Level2DaoImpl">
<property name="template" ref="jt"></property>
</bean>
<bean id="accountdao" class="com.expense.DaoImpl.AccountDaoImpl">
<property name="template" ref="jt"></property>
</bean>
<bean id="crondao" class="com.expense.DaoImpl.CronDaoImpl">
<property name="template" ref="jt"></property>
</bean>
<bean id="dumpdao" class="com.expense.DaoImpl.DumpDaoImpl">
<property name="template" ref="jt"></property>
</bean>
<bean id="pettydao" class="com.expense.DaoImpl.PettyCashDaoImpl">
<property name="template" ref="jt"></property>
</bean>
<bean id="masterdao" class="com.expense.DaoImpl.MasterDaoImpl">
<property name="template" ref="jt"></property>
</bean>
<bean id="escalationNotificationDao" class="com.expense.DaoImpl.EscalationNotificationDaoImpl">
<property name="template" ref="jt"></property>
</bean>
<!--=================================================Service Layer Bean============================================= -->
<bean id="securityservice" class="com.expense.ServiceImpl.SecurityServiceImpl">
</bean>
<bean id="commonservice" class="com.expense.ServiceImpl.CommonServiceImpl">
</bean>
<bean id="expservice" class="com.expense.ServiceImpl.ExpenseServiceImpl">
</bean>
<bean id="actionservice" class="com.expense.ServiceImpl.Level2ServiceImpl">
</bean>
<bean id="accountservice" class="com.expense.ServiceImpl.AccountServiceImpl">
</bean>
<bean id="uploadfile" class="com.expense.FileUpload.FileUploadService"/>
<bean id="pettyuploadfile" class="com.expense.FileUpload.FileUploadServicePettyCash"/>
<bean id="pettyservice" class="com.expense.ServiceImpl.PettyCashServiceImpl"/>
<bean id="dumpservice" class="com.expense.ServiceImpl.Dump_ServiceImpl"/>
<bean id="masterservice" class="com.expense.ServiceImpl.MasterServiceImpl"/>
<bean id="pageaccess" class="com.expense.ServiceImpl.securityaccess_Impl"/>
<bean id="escalatedNotificationService" class="com.expense.ServiceImpl.EscalationNotificationServiceImpl"/>
</beans>
我的應用程序正常工作是我連接到其他服務器而不是本地主機
<property name="url" value="jdbc:mysql://10.10.1.161:3305/expense"></property>
我的環境變量設置正確,因為我可以通過 cmd、mysql mysql -uroot -p
訪問我的數據庫,請幫忙。
謝謝你
在不知道您的 MySQL 安裝版本的情況下,我認為這是版本 8 安裝。 正在運行的一個數據庫可能是較舊的安裝,其中使用了不同的身份驗證機制。
這個問題之前已經在這里討論過: How to resolve Unable to load authentication plugin 'caching_sha2_password' issue
我認為最好的辦法是更新您的連接器: https://dev.mysql.com/downloads/connector/j/
否則,您可以查看有關如何更改標識以允許 mysql_native_password 的手冊: https://dev.mysql.com/doc/refman/8.0/en/caching-sha2-pluggable-authentication.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.