gpt4 book ai didi

mysql - Ubuntu 上的 Tomcat 6 和 MySQL 5.x 连接错误

转载 作者:行者123 更新时间:2023-11-29 00:57:11 24 4
gpt4 key购买 nike

在 Tomcat 6 上运行的 JSP 网络应用程序中尝试连接到 MySQL 5.x 数据库时,出现以下异常:

org.apache.jasper.JasperException: javax.servlet.ServletException: com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception: 

** BEGIN NESTED EXCEPTION **

java.net.SocketException
MESSAGE: java.security.AccessControlException: access denied (java.net.SocketPermission [0:0:0:0:0:0:0:1]:3307 connect,resolve)

STACKTRACE:

java.net.SocketException: java.security.AccessControlException: access denied (java.net.SocketPermission [0:0:0:0:0:0:0:1]:3307 connect,resolve)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:151)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:280)
at com.mysql.jdbc.Connection.createNewIO(Connection.java:1699)
at com.mysql.jdbc.Connection.<init>(Connection.java:405)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:268)
at java.sql.DriverManager.getConnection(DriverManager.java:620)
at java.sql.DriverManager.getConnection(DriverManager.java:200)
at org.apache.jsp.doLogin_jsp._jspService(doLogin_jsp.java:70)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at sun.reflect.GeneratedMethodAccessor32.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at org.apache.catalina.security.SecurityUtil$1.run(SecurityUtil.java:244)
at java.security.AccessController.doPrivileged(Native Method)
at javax.security.auth.Subject.doAsPrivileged(Subject.java:537)
at org.apache.catalina.security.SecurityUtil.execute(SecurityUtil.java:276)
at org.apache.catalina.security.SecurityUtil.doAsPrivilege(SecurityUtil.java:162)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:283)
at org.apache.catalina.core.ApplicationFilterChain.access$000(ApplicationFilterChain.java:56)
at org.apache.catalina.core.ApplicationFilterChain$1.run(ApplicationFilterChain.java:189)
at java.security.AccessController.doPrivileged(Native Method)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:185)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:636)

可能是什么原因,我该如何解决?

最佳答案

这里是跟踪的相关部分:

java.net.SocketException MESSAGE: java.security.AccessControlException: access denied (java.net.SocketPermission [0:0:0:0:0:0:0:1]:3307 connect,resolve)

Web 应用程序没有连接/解析提到的套接字的权限。您需要在 Tomcat 级别配置它。打开 /conf/catalina.policy 并添加以下代码块:

grant {
permission java.net.SocketPermission "localhost:3307", "connect,resolve";
};

如果你想更严格一些,例如仅授予对 /lib/filename.jar 中存在的特定 JDBC 驱动程序的访问权限,然后改为添加:

grant codeBase "jar:file:${catalina.home}/lib/filename.jar!/-" {
permission java.net.SocketPermission "localhost:3307", "connect,resolve";
};

与具体问题无关,以下几行

at java.sql.DriverManager.getConnection(DriverManager.java:200)
at org.apache.jsp.doLogin_jsp._jspService(doLogin_jsp.java:70)

表示您正在 JSP 文件中连接数据库。也许您刚刚开始学习,但我只想说这不是最佳实践。数据库连接应该在它自己的类中完成,您又在一个 servlet 类中使用这些类,然后转发到 JSP,JSP 又显示结果。通过 URL 调用 servlet 应该会产生相同的结果,但您最终会得到更好的可重用和可维护代码。

关于mysql - Ubuntu 上的 Tomcat 6 和 MySQL 5.x 连接错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5489880/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com