AWS Linux使用tomcat7配置mysql

时间:2013-03-24 22:41:24

标签: mysql linux tomcat amazon-ec2

我在我的AWS Linux微实例上安装了Tomcat 7和MySQL 5.5。我正在试图弄清楚如何配置Tomcat 7与MySQL通信,但没有运气。我一直是Glassfish的用户,所以通过GUI进行操作非常简单,但是对于Tomcat,我不知道如何配置它。

我看过Tomcat 7上的Apache文档,但发现自己更加困惑。我不知道我需要编辑哪些文件,我不知道要编辑它们的内容。我需要安装MySQL驱动程序吗?我使用JDBC还是JNDI?我的应用程序是一个使用Hibernate的Tapestry5应用程序,所以我不确定这是否重要。

有人知道一个很好的指南或者可以提供有关如何执行此操作的示例代码吗?仅仅是为了记录,我只有几个小时的Linux后轮,所以当涉及到任何Linux相关时,我都非常环保。

更新

我找到了以下默认配置

<!--    <Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
              description="User database that can be updated and saved"
              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
              pathname="conf/tomcat-users.xml" />
-->

我正在使用hibernate并在hibernate.cfg.xml中使用以下内容

<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.datasource">jdbc/mydatabase</property>

我注释掉了上面的资源并添加了以下内容,但这似乎也不起作用。我也注意到我也无法访问tomcat管理器。

<Resource type="javax.sql.DataSource"
            name="jdbc/mydatabase"
            factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
            driverClassName="com.mysql.jdbc.Driver"
            url="jdbc:mysql://localhost:3306/mysql"
            username="root"
            password="password"
/>

我将以下内容添加到context.xml文件

<ResourceLink type="javax.sql.DataSource"
                name="jdbc/mydatabase"
                global="jdbc/mydatabase"

有人知道我在使用此配置时遇到了什么问题吗?

我收到以下异常

  

根本原因

HTTP Status 500 - java.lang.RuntimeException: Exception constructing service 'ValueEncoderSource': Error invoking service
     

贡献方法   org.apache.tapestry5.hibernate.HibernateModule.contributeValueEncoderSource(MappedConfiguration,   boolean,HibernateSessionSource,Session,TypeCoercer,PropertyAccess,   LoggerSource):构造服务的异常   'HibernateSessionSource':调用构造函数public时出错   org.apache.tapestry5.internal.hibernate.HibernateSessionSourceImpl(org.slf4j.Logger,java.util.List中):   找不到数据源

javax.naming.NameNotFoundException: Name [jdbc/mydatabase] is not bound in this Context. Unable to find [jdbc].

3 个答案:

答案 0 :(得分:0)

我已经使用JNDI配置了Tomcat 6,但我不记得确切的配置设置,但有一点可以确定你需要使用JNDI。

答案 1 :(得分:0)

1. Context configuration

Configure the JNDI DataSource in Tomcat by adding a declaration for your
resource to your Context.

Open the context.xml which is located at the Tomcat configuration (conf) folder.

For Example: (This path might be differ for your tomcat instance)

D:\Program Files\Apache Software Foundation\Tomcat 5.5\conf

Add the following Resource entry with your database properties.

</Context>

--------------------------------------------

---------------------------------------------

<Resource name="jdbc/Foofys" auth="Container" type="javax.sql.DataSource"

maxActive="100" maxIdle="30" maxWait="10000"

username="root" password="pradeep" driverClassName="com.mysql.jdbc.Driver"

url="jdbc:mysql://localhost:3306/foofys"/>

</Context>

Bolded attributes should be as per your database configurations.

2. web.xml configuration

Open the context.xml which is located at the Tomcat configuration (conf) folder.

For Example: (This path might be differ for your tomcat instance)

D:\Program Files\Apache Software Foundation\Tomcat 5.5\conf

<?xml version="1.0" encoding="ISO-8859-1"?>

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/
j2ee/web-app_2_4.xsd"

version="2.4">

<resource-ref>

<description>DB Connection</description>

<res-ref-name>jdbc/Foofys</res-ref-name>

<res-type>javax.sql.DataSource</res-type>

<res-auth>Container</res-auth>

</resource-ref>

-----------------------------------------------

-------------------------------------------------

</web-app>

3. Copy the My SQL database driver into the tomcat common
libraries folder.

For Example: (This path might be differ for your tomcat instance)

D:\Program Files\Apache Software Foundation\Tomcat5.5\common\lib

mysql-connector-java-5.1.10.jar

答案 2 :(得分:0)

我在项目中遇到了类似的问题。我忘了将jdbc mysql连接器放在apache tomcat的lib文件夹中。 Mysql JDBC Connector

后来我将文件添加到lib文件夹中,并删除了异常。