连接 Tomcat 6 和 MySQL 5(+ Pandora 项目管理软件)
Posted
技术标签:
【中文标题】连接 Tomcat 6 和 MySQL 5(+ Pandora 项目管理软件)【英文标题】:Connecting Tomcat 6 and MySQL 5 (+ Plandora project management software) 【发布时间】:2011-09-28 03:35:55 【问题描述】:我知道已经有很多关于这个主题的讨论,但我需要一些帮助来解决我的具体问题。
首先是细节:
操作系统:Ubuntu Server 11.04 数据库:mysql 5.1.54 Web 服务器:Apache Tomcat 6
我尝试安装的软件是 Pandora 1.10.4 (http://www.plandora.org/)。
我正在尝试按照此处的安装过程:http://www.plandora.org/project.htm,并查看其他地方的教程和 Tomcat 6 文档。
.war 文件已通过管理器成功安装,我目前正在查看登录屏幕。当我尝试使用默认 root 帐户进行首次连接时,显示以下错误:
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 中创建了一个用户,一个 Pandora 数据库(并将提供的 .sql 脚本中的内容加载到其中),并授予 Pandora 用户对该特定数据库的完全访问权限。
我已将 Pandora 提供的 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。
我认为这涵盖了我到目前为止所做的事情。任何建议将不胜感激:)
【问题讨论】:
你能ping到数据库吗?您是否允许防火墙中的网络连接? @精英绅士 - 感谢您的回答。 servlet 正在尝试访问其他项目中 php 解释器经常使用的 localhost 数据库,所以我认为可以肯定地说可以连接到该数据库。你是这个意思吗?此服务器上没有运行防火墙。 【参考方案1】:Caused by: java.net.ConnectException: Connection refused
此时,JDBC 驱动程序已成功加载,并且jdbc:mysql://localhost:3306/plandora
的 JDBC URL 是正确的(至少对于 JDBC 驱动程序而言)。所以这些都不是问题。这里发生的是连接获取已经被拒绝了。这基本上意味着主机localhost
的端口3306
上没有任何东西正在接受TCP/IP 连接。
这可能有多种原因:
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 上以某种方式打开。
【讨论】:
您好 BalusC,感谢您的回答。您间接帮助我找到了解决方案:“--skip-networking”在最新的 Ubuntu / MySql 组合中不再使用,但它的替换“bind-address”设置为 10.0.1.20(局域网上服务器的 ip )。我将其更改为 127.0.0.1,瞧,一切都好起来了。唯一的问题是这个修复似乎破坏了 phpmyadmin 的数据库连接,所以我现在要修复它:) Phpmyadmin 只是一个 cookie 问题,所以这只是我抢先一步。 不客气。我将这种可能性添加到我的答案中。请注意,您也可以通过使用jdbc:mysql://10.0.1.20:3306/plandora
而不是 localhost:3306
来解决此问题。
谢谢,现在一切都说得通了。以上是关于连接 Tomcat 6 和 MySQL 5(+ Pandora 项目管理软件)的主要内容,如果未能解决你的问题,请参考以下文章