问题描述
我安装的Mysql5.1,进入命令模式时需要密码:自己设置的(mysql)有一个默认的数据库test,我想连接这个库,或者其他库怎么连接呢?spring中的配置如下:<bean id ="dataSource" class ="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/test" /> <property name="username" value="" /> //关键是用户名和密码不知道怎么写 <property name="password" value=""/> //关键是用户名和密码不知道怎么写</bean> 严重: Cannot create PoolableConnectionFactory (Access denied for user 'root'@'localhost' (using password: YES))2010-10-25 14:08:20 org.hibernate.cfg.SettingsFactory buildSettings警告: Could not obtain connection metadataorg.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Access denied for user 'root'@'localhost' (using password: YES))at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1225)at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:880)at org.springframework.orm.hibernate3.LocalDataSourceConnectionProvider.getConnection(LocalDataSourceConnectionProvider.java:81)at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:84)at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2009)at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1292)at org.springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory(LocalSessionFactoryBean.java:805)at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:745)at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:134)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1202)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1172)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:428)at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:251)at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:156)at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:248)at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:160)at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:284)at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:352)at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:244)at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:187)at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:49)at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3972)at org.apache.catalina.core.StandardContext.start(StandardContext.java:4467)at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:791)at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:771)at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:546)at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1041)at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:964)at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:502)at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1277)at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:321)at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053)at org.apache.catalina.core.StandardHost.start(StandardHost.java:785)at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)at org.apache.catalina.core.StandardService.start(StandardService.java:519)at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)at org.apache.catalina.startup.Catalina.start(Catalina.java:581)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)at java.lang.reflect.Method.invoke(Method.java:585)at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)Caused by: java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3515)at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3447)at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:911)at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:3953)at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1276)at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2048)at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:723)at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:298)at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:282)at org.apache.commons.dbcp.DriverConnectionFactory.createConnection(DriverConnectionFactory.java:38)at org.apache.commons.dbcp.PoolableConnectionFactory.makeObject(PoolableConnectionFactory.java:294)at org.apache.commons.dbcp.BasicDataSource.validateConnectionFactory(BasicDataSource.java:1247)at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1221)... 44 more2010-10-25 14:08:20 org.hibernate.dialect.Dialect <init>信息: Using dialect: org.hibernate.dialect.MySQLDialect 问题补充:安装完MySql然后进行配置时设置密码,如图附加current root password:rootnew rootpassword: testconfirm password: test然后我再配置我的spring配置文件:spring中的配置如下: <bean id ="dataSource" class ="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/test" /> <property name="username" value="test" /> <property name="password" value="test"/> </bean> 还是报错:警告: Could not obtain connection metadataorg.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Access denied for user 'test'@'localhost' (using password: YES))at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1225)at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:880)at org.springframework.orm.hibernate3.LocalDataSourceConnectionProvider.getConnection(LocalDataSourceConnectionProvider.java:81)at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:84)at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2009)at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1292)at org.springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory(LocalSessionFactoryBean.java:805)at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:745)at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:134)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1202)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1172)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:428)at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:251)at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:156)at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:248)at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:160)at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:284)at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:352)at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:244)at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:187)at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:49)at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3972)at org.apache.catalina.core.StandardContext.start(StandardContext.java:4467)at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:791)at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:771)at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:546)at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1041)at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:964)at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:502)at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1277)at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:321)at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053)at org.apache.catalina.core.StandardHost.start(StandardHost.java:785)at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)at org.apache.catalina.core.StandardService.start(StandardService.java:519)at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)at org.apache.catalina.startup.Catalina.start(Catalina.java:581)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)at java.lang.reflect.Method.invoke(Method.java:585)at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)Caused by: java.sql.SQLException: Access denied for user 'test'@'localhost' (using password: YES)at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3515)at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3447)at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:911)at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:3953)at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1276)at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2048)at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:723)at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:298)at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:282)at org.apache.commons.dbcp.DriverConnectionFactory.createConnection(DriverConnectionFactory.java:38)at org.apache.commons.dbcp.PoolableConnectionFactory.makeObject(PoolableConnectionFactory.java:294)at org.apache.commons.dbcp.BasicDataSource.validateConnectionFactory(BasicDataSource.java:1247)at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1221)... 44 more2010-10-26 14:36:17 org.hibernate.dialect.Dialect <init>信息: Using dialect: org.hibernate.dialect.MySQLDialect
解决方案
可能还是你的用户和密码的问题吧。在试一试我的办法。先关闭mysql # service mysqld stop把权限屏蔽 # mysqld_safe --skip-grant-table 会出现这样的字样: Starting demo from .....新开起一个终端输入 # mysql -u root mysql mysql> UPDATE user SET Password=PASSWORD('newpassword') where USER='root'; mysql> FLUSH PRIVILEGES;//记得要这句话,否则如果关闭先前的终端,又会出现原来的错误 mysql> q这样看看。
解决方案二:
引用Caused by: java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES) 没分配权限 或者数据库 用户名密码错误!用户名密码 你安装的时候会填的什么就写什么! 默认 root
解决方案三:
用户名用root.你在安装mysql数据库的时候, 会让你设置密码,把那个你设置的输入进去。上面是我的mysql数据库,我设置的密码是root.
解决方案四:
<bean id ="dataSource" class ="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/test" /> <property name="username" value="root" /> <property name="password" value="root"/> </bean> 要连其他库,把test改成库名就可以了~
解决方案五:
<bean id ="dataSource" class ="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://192.168.10.60:3309/test" /> <property name="username" value="root" /> <property name="password" value="root"/> </bean> 用户名和密码,就是你连接数据库的用户名和密码。