我知道已经有很多关于这个主题的讨论,但我需要一些帮助来解决我的具体问题。
首先是细节:
操作系统:Ubuntu Server 11.04 数据库:MySQL 5.1.54 网络服务器:Apache Tomcat 6
我尝试安装的软件是 Plandora 1.10.4 ( http://www.plandora.org/ )。
我正在尝试按照此处的安装过程进行操作:http://www.plandora.org/project.htm ,以及查看其他地方的教程和 Tomcat 6 文档。
.war 文件已通过管理器成功安装,我目前正在查看登录屏幕。当我尝试使用默认根帐户进行首次连接时,我看到以下错误:
org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Communications link failure The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.)
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
at sun.reflect.GeneratedConstructorAccessor28.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:532)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1116)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:344)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2333)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2370)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2154)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:792)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
at sun.reflect.GeneratedConstructorAccessor25.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:532)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:381)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:305)
at org.apache.commons.dbcp.DriverConnectionFactory.createConnection(DriverConnectionFactory.java:38)
at org.apache.commons.dbcp.PoolableConnectionFactory.makeObject(PoolableConnectionFactory.java:582)
at org.apache.commons.dbcp.BasicDataSource.validateConnectionFactory(BasicDataSource.java:1556)
at org.apache.commons.dbcp.BasicDataSource.createPoolableConnectionFactory(BasicDataSource.java:1545)
at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1388)
at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
at com.pandora.dao.DataAccess.getConnection(DataAccess.java:66)
at com.pandora.dao.DataAccess.getConnection(DataAccess.java:45)
at com.pandora.dao.UserDAO.getObjectByUsername(UserDAO.java:375)
at com.pandora.bus.UserBUS.authenticateUser(UserBUS.java:211)
at com.pandora.delegate.UserDelegate.authenticateUser(UserDelegate.java:102)
at com.pandora.gui.struts.action.LoginAction.doLogin(LoginAction.java:65)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:280)
at org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:216)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
at com.pandora.gui.struts.action.GeneralStruts.process(GeneralStruts.java:54)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
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:127)
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:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:679)
Caused by: java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:327)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:193)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:180)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:384)
at java.net.Socket.connect(Socket.java:546)
at java.net.Socket.connect(Socket.java:495)
at java.net.Socket.<init>(Socket.java:392)
at java.net.Socket.<init>(Socket.java:235)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:257)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:294)
... 48 more
我假设我已经在 CATALINA_HOME/lib(即/usr/share/tomcat6/lib)中正确安装了 MySQL 驱动程序(mysql-connector-java-5.1.16-bin.jar),问题是包含连接详细信息。
我在 MySQL 中创建了一个用户,一个用于 Plandora 的数据库(并将提供的 .sql 脚本中的内容加载到其中),并向 Plandora 用户授予对该特定数据库的完全访问权限。
我已将 Plandora 提供的 context.xml 文件的内容与我发现已存在于 CATALINA_BASE/conf(即/var/lib/tomcat6/conf)中的 context.xml 文件合并。现在内容如下:
<?xml version='2.0' encoding='utf-8'?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!-- The contents of this file will be loaded for each web application -->
<Context>
<!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<Resource name="jdbc/plandora" auth="Container"
type="javax.sql.DataSource" username="example_username" password="example_password"
driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/plandora"
maxActive="50" maxIdle="20"/>
<!-- Uncomment this to disable session persistence across Tomcat restarts -->
<!--
<Manager pathname="" />
-->
<!-- Uncomment this to enable Comet connection tacking (provides events
on session expiration as well as webapp lifecycle) -->
<!--
<Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
-->
</Context>
我没有更改 plandora 提供的任何其他文件。至于 Tomcat 6,我添加了一个用户来访问管理器和主机管理器 webapps,但仅此而已。按照 Tomcat 版本 6 的建议,我单独保留了 server.xml。
我认为这涵盖了我到目前为止所做的一切。任何建议将不胜感激:)
最佳答案
Caused by: java.net.ConnectException: Connection refused
此时,JDBC 驱动程序已成功加载并且 jdbc:mysql://localhost:3306/plandora
的 JDBC URL 是正确的(至少对于 JDBC 驱动程序而言)。所以这些都不是问题。这里发生的是连接获取被拒绝了。这基本上意味着在接受 TCP/IP 连接的主机 localhost
的端口 3306
上没有任何内容。
这可能有几个原因:
- MySQL 未启动。
- MySQL 确实不在
localhost
机器上运行。 - MySQL 不监听
localhost
后面的 IP(mysqld
bind-address
未设置为127.0.0.1
) - MySQL 不接受 TCP/IP 连接(
mysqld
有--skip-networking
选项)。 - 在 MySQL 的
my.cnf
中定义的 MySQL 端口号不是3306
。 - MySQL 已用完连接。
- 某些防火墙/代理阻止了连接。
我猜测是--skip-networking
选项。我经常看到这是 Ubuntu + MySQL + JDBC 连接问题的原因。它在 Ubuntu 上以某种方式默认打开。
关于java - 连接 Tomcat 6 和 MySQL 5(+ Plandora 项目管理软件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6571872/