MySQL 无法创建 PoolableConnectionFactory(用户 ''@'localhost' 拒绝访问
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3782197/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot create PoolableConnectionFactory (Access denied for user ''@'localhost'
提问by Joe
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:
在过去的几天里,我一直在尝试为 Web 应用程序设置 DatabaseConnectionPool,但没有成功。我已经阅读了 Tomcat 文档的相关部分以及关于该主题的大量内容,并认为我做的一切都是正确的,但显然不是因为我不断收到以下错误:
Cannot create PoolableConnectionFactory (Access denied for user ''@'localhost' (using password: YES))
I'm not getting the error when I start Tomcat running, but when I try to run the following servlet:
当我开始运行 Tomcat 时,我没有收到错误消息,但是当我尝试运行以下 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();
}
}
}
}
The context for the webapp is:
webapp 的上下文是:
<?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>
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.
我真正不明白的是,当我指定那是用户时,为什么错误并不是说拒绝访问 'root'@'localhost'。
What I've tried:
我试过的:
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.
我有重复的 context.xml 吗?不。我删除了 Tomcat 6.0/conf 中的默认值。我尝试将 context.xml 放入 [mywebappname]/META-INF/context.xml 但这不仅不起作用,而且导致创建了一个名为 TwitterSearch.xml 的文件,该文件自动生成并放入 Tomcat 6.0/conf /Catalina/localhost 目录。所以现在我只是在编辑那个,那是我唯一的一个。
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的版本吗?好吧,我完全重新安装了最新版本的Tomcat 6.0,所以我认为不是这样。
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.
我们是否缺少一些罐子?我有 tomcat-dbcp.jar、jconnector.jar 以及我认为应该在 Tomcat 6.0/lib 目录中拥有的所有其他文件。
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?
当我查看 MySQL 数据库中的密码时,出于安全目的,它们似乎已被编码为一个长字母数字字符串。这是正常的吗?我应该在我的 context.xml 中使用它还是只使用普通密码?
I really don't know how I can sort this out and would really appreciate some expert advice.
我真的不知道如何解决这个问题,非常感谢一些专家的建议。
Many thanks in advance.
提前谢谢了。
Joe
乔
回答by nos
Your setup looks fine. This looks to purly be a permissions problem.
您的设置看起来不错。这看起来纯粹是权限问题。
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.
您需要在 mysql 中授予该用户访问权限。虽然 Java 将连接到 localhost,但它会使用 tcp/ip 来连接 - 但是在 mysql localhost 和 127.0.0.1 中具有不同的含义。发出此 SQL 应该可以解决问题。
grant all on twitter.* to 'root'@'127.0.0.1' identified by 'mypwd';
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"
假设 Java 将 'localhost' 解析为 127.0.0.1,如果仍然无法正常工作,您可以尝试将连接字符串更改为“jdbc:mysql://127.0.0.1:3306/twitter”
Should I have this in my context.xml or just the normal password?
我应该在我的 context.xml 中使用它还是只使用普通密码?
As you have it now, the plaintext password.
正如您现在拥有的那样,明文密码。
回答by taf
In your configuration context.xml for the webapp you have to change user="root" by username="root"
在 webapp 的配置 context.xml 中,您必须通过 username="root" 更改 user="root"