AWS Linux使用tomcat7配置mysql

AWS Linux使用tomcat7配置mysql,mysql,linux,tomcat,amazon-ec2,Mysql,Linux,Tomcat,Amazon Ec2,我已经在我的AWS Linux微实例上安装了Tomcat 7和MySQL 5.5。我正试图弄明白如何配置Tomcat7与MySQL通信,但运气不好。我一直是Glassfish用户,所以通过GUI进行操作非常简单,但是使用Tomcat,我不知道如何配置它 我查看了Tomcat7上的Apache文档,但发现自己更加困惑。我不知道我需要编辑的文件在哪里,也不知道用什么编辑它们。我需要安装MySQL驱动程序吗?我使用JDBC还是JNDI?我的应用程序是使用Hibernate的Tapestry5应用程序,

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

我查看了Tomcat7上的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" />
-->
贡献法 org.apache.tapestry5.hibernate.HibernateModule.contributeValueEncoderSource(MappedConfiguration, boolean、HibernateSessionSource、会话、类型强制器、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].

我已经使用JNDI配置了Tomcat6,但我不记得确切的配置设置,但有一点是肯定的,您需要使用JNDI

1。上下文配置
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
在Tomcat中配置JNDI数据源,方法是为 将资源添加到您的上下文中。 打开位于Tomcat配置(conf)文件夹中的context.xml。 例如:(对于tomcat实例,此路径可能不同) D:\Program Files\Apache软件基金会\Tomcat 5.5\conf 使用数据库属性添加以下资源项。 -------------------------------------------- --------------------------------------------- 粗体属性应与数据库配置一致。 2.web.xml配置 打开位于Tomcat配置(conf)文件夹中的context.xml。 例如:(对于tomcat实例,此路径可能不同) D:\Program Files\Apache软件基金会\Tomcat 5.5\conf 数据库连接 jdbc/Foofys javax.sql.DataSource 容器 ----------------------------------------------- ------------------------------------------------- 3.将MySQL数据库驱动程序复制到tomcat公用程序中 图书馆文件夹。 例如:(对于tomcat实例,此路径可能不同) D:\Program Files\Apache软件基金会\Tomcat5.5\common\lib mysql-connector-java-5.1.10.jar
我在一个项目中遇到了类似的问题。我忘了将JDBCMySQL连接器放在ApacheTomcat的lib文件夹中


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

@JeenavDongre您可能不知道这个答案,但奇怪的是,为什么我需要使用tomcat将配置设置添加到我的WEB-INF/WEB.xml,而对于Glassfish,这是不必要的?在Glassfish中,您可以通过数据库的GUI配置JDBC以使用数据库,而应用程序中除了hibernate.cfg.xml中的数据源之外没有其他设置。我使用Jetty进行本地开发,因此我担心应用程序中的tomcat设置可能会中断Jetty的运行。我不确定情况是否如此,但我担心的是,首先,您没有任何GUI来手动配置数据库。这是一个例子。谢谢Jeevan,我今晚才能测试这个,但我还有几个快速的问题。我假设web.xml是应用程序中web-INF下的xml文件?您是否知道为什么tomcat在web.xml中需要resource ref,而glassfish中不需要resource ref?另外,我注意到您在context.xml而不是server.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
javax.naming.NameNotFoundException: Name [jdbc/mydatabase] is not bound in this Context. Unable to find [jdbc].
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