无法创建PoolableConnectionFactory(对用户``@'localhost'的访问被拒绝 [英] Cannot create PoolableConnectionFactory (Access denied for user ''@'localhost'
问题描述
最近几天,我一直在尝试为Web应用程序设置DatabaseConnectionPool,但没有成功.我已经阅读了Tomcat文档的相关部分以及有关该主题的大量内容,并认为我所做的一切都正确,但显然不是因为我不断遇到以下错误:
I have been trying to set up a DatabaseConnectionPool for a web app for the last couple of days with no success. I have read the relevant sections of the Tomcat docs and a great deal around the subject and think I'm doing everything right, but obviously not because I keep on getting the following error:
Cannot create PoolableConnectionFactory (Access denied for user ''@'localhost' (using password: YES))
当我启动Tomcat运行时,但在尝试运行以下servlet时,我没有收到错误消息:
I'm not getting the error when I start Tomcat running, but when I try to run the following servlet:
package twittersearch.web;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.sql.*;
import javax.sql.*;
import javax.naming.*;
import twittersearch.model.*;
public class ConPoolTest extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
Context ctx = null;
DataSource ds = null;
Connection conn = null;
try {
ctx = new InitialContext();
ds = (DataSource)ctx.lookup("java:comp/env/jdbc/twittersearchdb");
conn = ds.getConnection();
if(conn != null) {
System.out.println("have a connection from the pool");
}
} catch(SQLException e) {
e.printStackTrace();
} catch(NamingException e) {
e.printStackTrace();
} finally {
try {
if(conn!=null) {
conn.close();
}
} catch(SQLException e) {
e.printStackTrace();
}
}
}
}
该Webapp的上下文是:
The context for the webapp is:
<?xml version='1.0' encoding='utf-8'?>
<Context>
<!-- Configure a JDBC DataSource for the user database -->
<Resource name="jdbc/twittersearchdb"
type="javax.sql.DataSource"
auth="Container"
user="root"
password="mypwd"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/twitter"
maxActive="8"
maxIdle="4"/>
</Context>
我真正不明白的是,为什么当我指定用户为"root" @"localhost"时,错误并没有拒绝访问?
What I really don't understand is why the error isn't saying that the access is denied to 'root'@'localhost' when I've specified that that's the user.
我尝试过的事情:
我有重复的context.xml吗?否.我在Tomcat 6.0/conf中删除了默认设置.我尝试将context.xml放入[mywebappname]/META-INF/context.xml中,但不仅不起作用,而且还创建了一个名为TwitterSearch.xml的文件,该文件会自动生成并放入Tomcat 6.0/conf中/Catalina/localhost目录.所以现在我只编辑那个,那是我唯一的一个.
Do I have a duplicate context.xml? No. I deleted the default on in Tomcat 6.0/conf. I tried putting a context.xml in [mywebappname]/META-INF/context.xml but not only did this not work, but resulted in the creation of a file named TwitterSearch.xml which was autogenerated and put in the Tomcat 6.0/conf/Catalina/localhost directory. So now I'm just editing that one and thats the only one I have.
是Tomcat的版本吗?好吧,我完全重新安装了最新版本的Tomcat 6.0,所以我认为不是这样.
Is it the version of Tomcat? Well, I completely reinstalled the latest version of Tomcat 6.0 so I don't think it's that.
我们错过了一些罐子吗?我有tomcat-dbcp.jar,jconnector.jar以及我认为应该在Tomcat 6.0/lib目录中拥有的所有其他文件.
Are we missing some jars? I have the tomcat-dbcp.jar, jconnector.jar and all the other ones that I think I'm meant to have in the Tomcat 6.0/lib directory.
当我查看MySQL数据库中的密码时,出于安全考虑,它们似乎已编码为长字母数字字符串.这正常吗?我应该在我的context.xml中还是只在普通密码中输入这个密码?
When I look at the passwords in the MySQL database they seem to have been coded for security purposes into a long alpha-numeric string. Is this normal? Should I have this in my context.xml or just the normal password?
我真的不知道该如何解决这个问题,并且会非常感谢一些专家的建议.
I really don't know how I can sort this out and would really appreciate some expert advice.
非常感谢.
乔
推荐答案
您的设置看起来不错.看来这纯粹是一个权限问题.
Your setup looks fine. This looks to purly be a permissions problem.
您需要在mysql中授予该用户访问权限. Java将连接到localhost时,它将使用tcp/ip进行连接-但是在mysql localhost和127.0.0.1中具有不同的含义.发出此SQL应该可以解决问题.
You need to grant that user access in mysql. While Java will connect to localhost, it will do so using tcp/ip - however in mysql localhost and 127.0.0.1 have different meaning. Issuing this SQL should do the trick.
grant all on twitter.* to 'root'@'127.0.0.1' identified by 'mypwd';
假设Java将"localhost"解析为127.0.0.1,如果仍然无法解决问题,则可以尝试将连接字符串更改为"jdbc:mysql://127.0.0.1:3306/twitter"
That assumes Java resolves 'localhost' to 127.0.0.1, if things still doesn't work, you could try changing your connection string to "jdbc:mysql://127.0.0.1:3306/twitter"
我的context.xml中应该有这个吗? 还是普通密码?
Should I have this in my context.xml or just the normal password?
您现在拥有的是纯文本密码.
As you have it now, the plaintext password.
这篇关于无法创建PoolableConnectionFactory(对用户``@'localhost'的访问被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!