Resolution of Access denied for user ' Lzj ' @ ' localhost ' problem

Source: Internet
Author: User
Tags aop

Using the SPRINGMVC + Mybatis + mysql-built architecture, the following error occurred during debugging:

HTTP Status 500-request processing failed; Nested exception is org.springframework.transaction.CannotCreateTransactionException:Could not open JDBC Connection forTransaction Nested exception is java.sql.SQLException:Access denied forUser ' Lzj ' @ ' localhost '(using Password:yes) type Exception reportmessage Request processing failed; nested Exception is org.springframework. Transaction. Cannotcreatetransactionexception:could not open JDBC Connection forTransaction Nested exception is java.sql.SQLException:Access denied forUser ' Lzj ' @ ' localhost '(using Password:yes) Description The server encountered an internal error that prevented it from fulfilling Thisrequest.exceptionorg.springframework.web.util.NestedServletException:Request processing failed; nested Exception is org.springframework.transaction.CannotCreateTransactionException:Could not open JDBC Connection forTransaction Nested exception is java.sql.SQLException:Access denied forUser ' Lzj ' @ ' localhost '(using Password:yes) org.springframework.web.servlet.FrameworkServlet.processRequest (Frameworkservlet.java: 982) Org.springframework.web.servlet.FrameworkServlet.doGet (Frameworkservlet.java:861) Javax.servlet.http.HttpServlet.service (Httpservlet.java:622) Org.springframework.web.servlet.FrameworkServlet.service (Frameworkservlet.java:846) Javax.servlet.http.HttpServlet.service (Httpservlet.java:729) Org.apache.tomcat.websocket.server.WsFilter.doFilter (Wsfilter.java:52) org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal (Characterencodingfilter.java: 197) Org.springframework.web.filter.OncePerRequestFilter.doFilter (Onceperrequestfilter.java:107) root causeorg.springframework.transaction.CannotCreateTransactionException:Could not open JDBC Connection forTransaction Nested exception is java.sql.SQLException:Access denied forUser ' Lzj ' @ ' localhost '(using Password:yes) Org.springframework.jdbc.datasource.DataSourceTransactionManager.doBegin ( Datasourcetransactionmanager.java:289) Org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction ( Abstractplatformtransactionmanager.java:373) Org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary ( Transactionaspectsupport.java:447) Org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction ( Transactionaspectsupport.java:277) Org.springframework.transaction.interceptor.TransactionInterceptor.invoke (Transactioninterceptor.java: 96) Org.springframework.aop.framework.ReflectiveMethodInvocation.proceed (Reflectivemethodinvocation.java: 179) Org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke (Exposeinvocationinterceptor.java: 92) Org.springframework.aop.framework.ReflectiveMethodInvocation.proceed (Reflectivemethodinvocation.java: 179) Org.springframework.aop.framework.JdkDynamicAopProxy.invoke (Jdkdynamicaopproxy.java:213) com.sun.proxy. $Proxy 15.list (Unknown Source) Com.lzj.controller.HelloController.hello (hellocontroller.java:< /c3>25) sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method) Sun.reflect.NativeMethodAccessorImpl.invoke ( Nativemethodaccessorimpl.java:62) Sun.reflect.DelegatingMethodAccessorImpl.invoke (Delegatingmethodaccessorimpl.java:43) Java.lang.reflect.Method.invoke (Method.java:498) Org.springframework.web.method.support.InvocableHandlerMethod.doInvoke (Invocablehandlermethod.java:205) Org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest ( Invocablehandlermethod.java:133) Org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle ( Servletinvocablehandlermethod.java:116) Org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod ( Requestmappinghandleradapter.java:827) org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal ( Requestmappinghandleradapter.java:738) Org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle ( Abstracthandlermethodadapter.java:85) Org.springframework.web.servlet.DispatcherServlet.doDispatch (Dispatcherservlet.java:963) Org.springframework.web.servlet.DispatcherServlet.doService (Dispatcherservlet.java:897) org.springframework.web.servlet.FrameworkServlet.processRequest (Frameworkservlet.java:970) Org.springframework.web.servlet.FrameworkServlet.doGet (Frameworkservlet.java:861) Javax.servlet.http.HttpServlet.service (Httpservlet.java:622) Org.springframework.web.servlet.FrameworkServlet.service (Frameworkservlet.java:846) Javax.servlet.http.HttpServlet.service (Httpservlet.java:729) Org.apache.tomcat.websocket.server.WsFilter.doFilter (Wsfilter.java:52) org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal (Characterencodingfilter.java: 197) Org.springframework.web.filter.OncePerRequestFilter.doFilter (Onceperrequestfilter.java:107) Root causejava.sql.SQLException:Access denied forUser ' Lzj ' @ ' localhost '(using Password:yes) com.mysql.jdbc.SQLError.createSQLException (Sqlerror.java:964) Com.mysql.jdbc.MysqlIO.checkErrorPacket (Mysqlio.java:3973) Com.mysql.jdbc.MysqlIO.checkErrorPacket (Mysqlio.java:3909) Com.mysql.jdbc.MysqlIO.checkErrorPacket (Mysqlio.java:873) com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication (Mysqlio.java:1710) Com.mysql.jdbc.MysqlIO.doHandshake (Mysqlio.java:1226) Com.mysql.jdbc.ConnectionImpl.coreConnect (Connectionimpl.java:2205) com.mysql.jdbc.ConnectionImpl.connectOneTryOnly (Connectionimpl.java:2236) Com.mysql.jdbc.ConnectionImpl.createNewIO (Connectionimpl.java:2035) Com.mysql.jdbc.ConnectionImpl.<init> (connectionimpl.java:790) com.mysql.jdbc.JDBC4Connection.<init> (jdbc4connection.java:47) Sun.reflect.NativeConstructorAccessorImpl.newInstance0 (Native Method) Sun.reflect.NativeConstructorAccessorImpl.newInstance (Nativeconstructoraccessorimpl.java:62) sun.reflect.DelegatingConstructorAccessorImpl.newInstance (Delegatingconstructoraccessorimpl.java:45) java.lang.reflect.Constructor.newInstance (Constructor.java:423) com.mysql.jdbc.Util.handleNewInstance (Util.java:425) com.mysql.jdbc.ConnectionImpl.getInstance (Connectionimpl.java:400) Com.mysql.jdbc.NonRegisteringDriver.connect (Nonregisteringdriver.java:330) java.sql.DriverManager.getConnection (Drivermanager.java:664) java.sql.DriverManager.getConnection (Drivermanager.java:208) Org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriverManager ( Drivermanagerdatasource.java:153) Org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriver ( Drivermanagerdatasource.java:144) Org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnectionFromDriver ( Abstractdriverbaseddatasource.java:196) Org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnection ( Abstractdriverbaseddatasource.java:159) Org.springframework.jdbc.datasource.DataSourceTransactionManager.doBegin ( Datasourcetransactionmanager.java:246) Org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction ( Abstractplatformtransactionmanager.java:373) Org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary ( Transactionaspectsupport.java:447) Org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction ( Transactionaspectsupport.java:277) Org.springframework.transaction.interceptor.TransactionInterceptor.invoke (Transactioninterceptor.java: 96) Org.springframework.aop.framework.ReflectiveMethodInvocation.proceed (Reflectivemethodinvocation.java: 179) Org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke (Exposeinvocationinterceptor.java: 92) Org.springframework.aop.framework.ReflectiveMethodInvocation.proceed (Reflectivemethodinvocation.java: 179) Org.springframework.aop.framework.JdkDynamicAopProxy.invoke (Jdkdynamicaopproxy.java:213) com.sun.proxy. $Proxy 15.list (Unknown Source) Com.lzj.controller.HelloController.hello (hellocontroller.java:< /c4>25) sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method) Sun.reflect.NativeMethodAccessorImpl.invoke ( Nativemethodaccessorimpl.java:62) Sun.reflect.DelegatingMethodAccessorImpl.invoke (Delegatingmethodaccessorimpl.java:43) Java.lang.reflect.Method.invoke (Method.java:498) Org.springframework.web.method.support.InvocableHandlerMethod.doInvoke (Invocablehandlermethod.java:205) Org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest ( Invocablehandlermethod.java:133) Org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle ( Servletinvocablehandlermethod.java:116) Org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod ( Requestmappinghandleradapter.java:827) org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal ( Requestmappinghandleradapter.java:738) Org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle ( Abstracthandlermethodadapter.java:85) Org.springframework.web.servlet.DispatcherServlet.doDispatch (Dispatcherservlet.java:963) Org.springframework.web.servlet.DispatcherServlet.doService (Dispatcherservlet.java:897) org.springframework.web.servlet.FrameworkServlet.processRequest (Frameworkservlet.java:970) Org.springframework.web.servlet.FrameworkServlet.doGet (Frameworkservlet.java:861) Javax.servlet.http.HttpServlet.service (Httpservlet.java:622) Org.springframework.web.servlet.FrameworkServlet.service (Frameworkservlet.java:846) Javax.servlet.http.HttpServlet.service (Httpservlet.java:729) Org.apache.tomcat.websocket.server.WsFilter.doFilter (Wsfilter.java:52) org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal (Characterencodingfilter.java: 197) Org.springframework.web.filter.OncePerRequestFilter.doFilter (Onceperrequestfilter.java:107)

Some of the online statement is that the database does not have access rights, of course, this is a situation.

There is another possibility that the framework does not get the DataSource correct username, which is related to configuration:

My original Applicationcontext.xml file configuration is this:

    <!--Load Properties -    <Context:property-placeholder Location= "Classpath:datasource.properties" />    <!--Configure DataSource -    <BeanID= "DataSource"class= "Org.springframework.jdbc.datasource.DriverManagerDataSource" >        < Propertyname= "Driverclassname"value= "${driverclassname}"/>        < Propertyname= "url"value= "${url}"/>        < Propertyname= "username"value= "${username}"/>        < Propertyname= "Password"value= "${password}"/>    </Bean>
The Datasource.properties file code is as follows:
Driverclassname=com.mysql.jdbc.driverurl=jdbc:mysql://127.0.0.1:3306/testusername=rootpassword=test

When the framework constructs a Abstractdriverbaseddatasource instance, the username is always the current machine name.

I also did not find the code to construct username!

Finally, the username of the Datasource.properties file is changed to Jdbc.username, namely:

Driverclassname=com.mysql.jdbc.driverurl=jdbc:mysql://127.0.0.1:3306/testjdbc.username=rootpassword=test

The DataSource bean configuration for the Applicationcontext.xml file is as follows:

    <!--Configure DataSource -    <BeanID= "DataSource"class= "Org.springframework.jdbc.datasource.DriverManagerDataSource" >        < Propertyname= "Driverclassname"value= "${driverclassname}"/>        < Propertyname= "url"value= "${url}"/>        < Propertyname= "username"value= "${jdbc.username}"/>        < Propertyname= "Password"value= "${password}"/>    </Bean>

This solves, each time to take the wrong username problem. What is the specific reason, I do not know. Also need to study the source code.

If you know the reason, please leave a message.

If there is an answer, I will make it up.

Resolution of Access denied for user ' Lzj ' @ ' localhost ' problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.