简体   繁体   English

MySql在已部署的Java Web应用程序中不起作用

[英]MySql doesn't work from deployed java web app

I made a page using struts. 我使用struts制作了一个页面。 When running on localhost (glassfish) it is working fine. 在本地主机(glassfish)上运行时,它工作正常。 However, when I copy WAR file to a remote server and run the application from there login form does not work. 但是,当我将WAR文件复制到远程服务器并从那里运行应用程序时,登录表单不起作用。 That means something is wrong when connecting on mysql database. 这意味着在mysql数据库上连接时出了点问题。

How could I solve this? 我该如何解决?

Code snippet: 程式码片段:

Connection conn = null;
    Statement stmt = null;
    try{
        InitialContext ctx = new InitialContext();
        DataSource ds = (DataSource)ctx.lookup("jdbc/SB");


        conn = ds.getConnection();
        stmt = conn.createStatement();
        ResultSet rs = null;
        if(stmt.execute("SELECT * FROM users WHERE username='"+loginform.getUsername()+"' AND geslo=MD5('"+loginform.getPassword()+"') LIMIT 1;"))
        {
    rs = stmt.getResultSet();
        }

        rs.next();

Unfortunately I cannot read from the server console. 不幸的是,我无法从服务器控制台读取信息。 What else should I do to see the exception (remote logging?)? 我应该怎么做才能看到异常(远程日志记录?)?

Thanks on replies. 感谢回复。

UPDATE: - Both locally and remotely GlassFish 3.0.1 is running. 更新:-本地和远程GlassFish 3.0.1均在运行。 - My working environment is NetBeans 6.9.1. -我的工作环境是NetBeans 6.9.1。 - Deploying locally is done simply in NetBeans. -本地部署只需在NetBeans中完成。 I just click on the project name and click publish. 我只是单击项目名称,然后单击发布。 For remote deployment, admin gave me access to the FTP server where I just copy the WAR file. 对于远程部署,管理员为我提供了访问FTP服务器的权限,我仅在其中复制了WAR文件。 - MySQL server is located somewhere else. -MySQL服务器位于其他位置。 The point is, it is accessible from the localhost web application but not from the same remote web application. 关键是,可以从localhost Web应用程序访问它,但不能从同一远程Web应用程序访问它。

Assuming you have admin rights on the remote server, you need to 假设您具有远程服务器上的管理员权限,则需要

  1. Verify that the datasource "jdbc/SB" exists. 验证数据源“ jdbc / SB”是否存在。 (Resources / JDBC / JDBC Resources) (资源/ JDBC / JDBC资源)
  2. Verify that the "poolname" in the datasoucre exists (Resources / JDBC/ Connection Pools) 验证datasoucre中的“池名”是否存在(资源/ JDBC /连接池)
  3. Open the appropriate connection pool and click on the "ping" button. 打开适当的连接池,然后单击“ ping”按钮。
  4. If ping fails verify the connection params under the Additional Properties tab. 如果ping失败,请在“其他属性”选项卡下验证连接参数。

There could be a dozen other issues as well. 可能还有其他十几个问题。 You really need to get access to the server log so you see what the specific exception is. 您确实需要访问服务器日志,以便查看具体的异常情况。 It's unlikely it's your code and more likely that the remote server is missing the driver dependency or isn't properly configured (the connection pool an datasource) 这不太可能是您的代码,更可能是远程服务器缺少驱动程序依赖性或配置不正确(连接池是数据源)

Some ideas are 一些想法是
- log to file instead of console -登录到文件而不是控制台
- Print log on your struts page -在您的Struts页面上打印日志
- maybe you don't have grants or firewall problem to access DB -也许您没有授予权限或防火墙问题即可访问数据库

I'd bet that you don't have authority to access the DB... 我敢打赌,您无权访问数据库...

Try this as a test... install the MySQL client on another machine and then connect to the MySQL server from there. 尝试将其作为测试...在另一台计算机上安装MySQL客户端,然后从那里连接到MySQL服务器。

If that fails then: 如果失败,则:

Say you log in with: User: test Password: password 假设您使用以下身份登录:用户:test密码:password

Then open the Mysql client and enter: 然后打开Mysql客户端并输入:

use nameOfDatabase;
GRANT ALL ON nameofDatabase.* TO 'userName'@'ipaddressOfServerOrDNSName';

That should do it. 那应该做。

There are other ways to give access see http://dev.mysql.com/doc/refman/5.1/en/grant.html 还有其他授予访问权限的方法,请参见http://dev.mysql.com/doc/refman/5.1/en/grant.html

Are you sure your database is setup correctly on your remote server? 您确定远程服务器上的数据库设置正确吗? It sounds like maybe a firewall issue. 听起来好像是防火墙问题。

Unfortunately I cannot read from the server console. 不幸的是,我无法从服务器控制台读取信息。

If you have shell access to the remote server, the solution is simple: 如果您可以通过外壳访问远程服务器,则解决方案很简单:

  • Change the logging configs so that your webapp logs to a file instead of / as well as the console. 更改日志记录配置,以便您的Web应用程序将日志记录到文件而不是/控制台。
  • Login and read the file. 登录并阅读文件。

(Indeed, if you were using Tomcat, stuff written to System.out / System.err is written to a file anyway. Maybe this is true for Glassfish also.) (实际上,如果您使用的是Tomcat,则无论如何,写入System.out / System.err的内容都会写入文件。也许对Glassfish也是这样。)

If you don't have shell access, you may have to modify your webapp to provide an extra page that can be used to download or view the log file from your web browser. 如果您没有外壳程序访问权限,则可能必须修改Web应用程序以提供额外的页面,该页面可用于从Web浏览器下载或查看日志文件。 If you have to resort to this, pay attention to the obvious security concerns. 如果您必须诉诸于此,请注意明显的安全问题。

This is my weg (wild eyed guess).... 这是我的韦格(眼神guess测)....

Your local app server has the MySql driver installed. 您的本地应用服务器已安装MySql驱动程序。 The JDBC resource is also defined correctly. JDBC资源也已正确定义。 The MySql server is probably running on your localhost. MySql服务器可能正在您的本地主机上运行。 You have developed your app locally in NetBeans. 您已经在NetBeans中本地开发了应用程序。

When you deploy an app from NetBeans onto a local server, the IDE makes sure that server has the DB driver installed. 当您将应用程序从NetBeans部署到本地服务器上时,IDE确保该服务器已安装数据库驱动程序。 NetBeans cannot do that when you deploy to a remote host. 部署到远程主机时,NetBeans无法做到这一点。 If you use some other deployment method, driver installation is not automagic. 如果使用其他部署方法,则无法自动安装驱动程序。 It is an easy step to forget. 这是一个容易忘记的步骤。

You can find out if the MySql drive is installed on your server by writing a web app that attempts to load or use one of the classes from the driver jar file. 您可以通过编写一个尝试加载或使用驱动程序jar文件中的类之一的Web应用程序来确定服务器上是否安装了MySql驱动器。 If the access generates an exception, you can probably get something that works by including the MySql drive in your webapp's WEB-INF/lib. 如果访问产生异常,则可以通过将MySql驱动器包含在Web应用程序的WEB-INF / lib中来获得某些工作。

NetBeans also helps folks create and manage their JDBC resource definitions. NetBeans还可以帮助人们创建和管理其JDBC资源定义。 If you create an app that targets a database on your localhost, the JDBC resource will have localhost as part of the URL. 如果您创建针对本地主机上的数据库的应用程序,则JDBC资源会将本地主机作为URL的一部分。 This will work great when the app is deployed onto the same server as the DB server. 当应用程序与数据库服务器部署在同一服务器上时,这将非常有用。 If the app is moved to another server, the JDBC resource needs to be changed to account for the possibility that the DB server is NOT on the localhost. 如果将应用程序移动到另一台服务器,则需要更改JDBC资源以解决DB服务器不在本地主机上的可能性。

If you do not have admin access to the remote server, you will have trouble getting the resources defined. 如果您没有对远程服务器的管理员访问权限,则将难以获取定义的资源。 If your remote server was a GlassFish 3.1 domain/instance, you could leverage application scoped resources to get the resources defined for your app. 如果远程服务器是GlassFish 3.1域/实例,则可以利用应用程序范围的资源来获取为应用程序定义的资源。

Regarding access to the log file. 关于访问日志文件。

If you are using asadmin to deploy onto the remote server, this command will help you access the server log if you are working with GlassFish v2.x: http://docs.sun.com/app/docs/doc/821-0179/display-log-records-1?l=en&a=view . 如果使用asadmin部署到远程服务器上,则在使用GlassFish v2.x时,此命令将帮助您访问服务器日志: http : //docs.sun.com/app/docs/doc/821-0179 / display-log-records-1?l = zh-CN&a = view You can also access the log via the admin console. 您也可以通过管理控制台访问日志。

If you are using rcp to deploy your app, by copying it into the autodeploy directory of the remote server, you may want to see if you can copy the log onto your local machine. 如果使用rcp部署应用程序,请将其复制到远程服务器的autodeploy目录中,则可能需要查看是否可以将日志复制到本地计算机上。

For example... if your admin told you to deploy using an ftp command like this: 例如...如果您的管理员告诉您使用ftp命令进行部署,如下所示:

ftp> cd /a/b/glassfish/domains/domain1/autodeploy
ftp> put MyGreatWebapp.war

you may be able to get the log by doing something like this: 您可以通过执行以下操作来获取日志:

ftp> cd /a/b/glassfish/domains/domain1/logs
ftp> get server.log

Experiment.... 实验....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 部署在Tomcat上的Java应用程序无法连接到jdbc-mysql - Java application deployed on Tomcat doesn't connect to jdbc-mysql 从tomcat取消部署Web应用程序不会删除已部署的目录 - Undeploy web application from tomcat doesn't remove the deployed directory Java Web服务器src无法正常工作 - java web server src doesn't work 使用Java的MySQL导出不起作用 - MySQL export with Java doesn't work 在Vagrant中部署的Web应用程序可在浏览器中运行,但身份验证在REST客户端中不起作用 - Web application deployed in Vagrant works in browser but authentication doesn't work in REST client 使用Heroku,如何在部署应用程序时自动启动未绑定端口的Java应用程序? - With Heroku, how can I automatically start a Java app that doesn't bind to a port when the app is deployed? Java应用程序在Eclipse上工作正常,但在部署为可运行的jar后它无法正常工作 - a Java application works fine on Eclipse but it doesn't work properly after being deployed as a runnable jar 部署到Azure网站时Java Servlet中的UTF-8不起作用 - UTF-8 in Java Servlet doesn't work when deployed to Azure Websites Java无法从cmd工作 - java doesn't work from cmd Web 应用程序中 hsqldb 文件的相对路径不起作用? - Relative path to hsqldb files in a web app doesn't work?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM