【问题标题】:AWS Linux configuring mysql with tomcat7AWS Linux 使用 tomcat7 配置 mysql
【发布时间】:2013-03-14 08:27:33
【问题描述】:

我已经在我的 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”:调用公共构造函数时出错 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].

【问题讨论】:

    标签: mysql linux tomcat amazon-ec2


    【解决方案1】:

    我已经使用 JNDI 配置了 Tomcat 6,但我不记得确切的配置设置,但有一件事是确定您需要使用 JNDI。

    【讨论】:

    • @JeenavDongre 你可能不知道这个答案,但很好奇为什么我需要用tomcat将配置设置添加到我的WEB-INF/web.xml中,但是对于Glassfish,这是不必要的吗?在 Glassfish 中,您可以将 JDBC 配置为通过其 GUI 与数据库一起使用,除了 hibernate.cfg.xml 中的数据源之外,应用程序中没有任何设置。我使用 Jetty 进行本地开发,所以我担心应用程序中的 tomcat 设置可能会破坏 jetty 运行。我不确定情况是否如此,但我担心。
    • 首先您将没有任何类型的 GUI 来手动配置您的数据库。这是一个例子。
    【解决方案2】:
    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
    

    【讨论】:

    • 谢谢 Jeevan,今晚我才能测试这个,但我还有一些快速的问题。我假设 web.xml 是位于 WEB-INF 下的应用程序内的 xml 文件?您是否碰巧知道为什么在 web.xml 中对于 tomcat 需要资源引用,但对于 glassfish 不需要?另外,我注意到您在 context.xml 而不是 server.xml 中处理数据库连接,有什么区别?试一试后,今晚晚些时候我可能还有几个问题,谢谢。
    【解决方案3】:

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

    后来我将文件添加到 lib 文件夹并摆脱了异常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-23
      • 1970-01-01
      • 2014-10-26
      • 1970-01-01
      • 1970-01-01
      • 2013-07-14
      • 1970-01-01
      • 2013-06-29
      相关资源
      最近更新 更多