无法从tomcat连接到MySQL数据库 [英] Can't connect to MySQL database from tomcat

查看:454
本文介绍了无法从tomcat连接到MySQL数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我收到此错误:

  com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException:
无法创建到数据库服务器的连接。
尝试重新连接3次。放弃。

我只是试图连接到数据库。使用此代码

 <%@ page import =java.sql。*%& 

<%
try {
// Class.forName(com.mysql.jdbc.Driver);
Class.forName(org.gjt.mm.mysql.Driver);

out.println(found);
} catch(ClassNotFoundException ex){
out.println(Erro< br />);
out.println(ex.toString());
} catch(Exception e){
out.println(e.toString());

}

连接ocon;

try {
ocon = DriverManager.getConnection(jdbc:mysql:// localhost / cpjcoimbra?autoReconnect = true,*****,***** ); // password matched
out.print(connected);
} catch(Exception e){
out.println(e.toString()+< br />);
}

%>

它找到了驱动程序,但我得到那个错误,当我尝试连接到数据库。




我对catalina有这个权限50.local.policy

授权代码库文件:/ var / lib / tomcat6 / WEB-INF / lib / - {
permission java.security.AllPermission;
};

任何人都知道为什么会出现此错误?



编辑:
服务mysql状态提供:

  * / usr / bin / mysqladmin Ver 8.42 5.1.37,for debian-linux-gnu on i486 
版权所有2000-2008 MySQL AB,2008 Sun Microsystems,Inc.
此软件自带绝对保修。这是免费的软件,
,欢迎您修改并重新分配在GPL许可证下

服务器版本5.1.37-1ubuntu5
协议版本10
Connection Localhost通过UNIX套接字
UNIX套接字/var/run/mysqld/mysqld.sock
正常运行时间:1小时32分钟21秒

线程数:1问题:103慢查询:0打开:171冲洗表:1打开表:41每秒查询平均:0.18


解决方案

驱动程序是错误的 - 使用com.mysql.jdbc.Driver类。



JSP中的Scriptlet代码?哦,我的 - 这不好。



但这些不是您的问题的原因。



您是否授予了该用户名和密码的权限?以下是示例步骤:


  1. 以root用户身份登录:mysql -h localhost -u root -p< ret> ;; password =< your admin password>

  2. create database x;

  3. create user p identified by'q';

  4. grant all on x。* to'x'@'%';

提供您使用的用户名p和密码q



确保服务已启动并侦听端口3306.打开命令shell并键入'netstat -a'并查看对于端口3306上的侦听器。



来自MySQL错误数据库的此条目也可能是相关的。



为了将来参考,我发现粘贴任何错误消息谷歌。很可能我不是遇到特定问题的第一个人。



即使你设法解决这个问题,让它工作,这仍然是一个致命的有缺陷的设计。 JSP直接连接到数据库 - 没有安全性,除了您以纯文本输入的用户名和密码。你真的不想这样做。



您有连接问题。


  1. 您可以使用命令shell中的MySQL管理工具进行连接吗?

  2. 你能写一个简单的Java类来成功连接到数据库吗?


Hey, I'm getting this error:

com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException:
Could not create connection to database server. 
Attempted reconnect 3 times. Giving up.

I'm just trying to connect to the database. With this code

<%@page import="java.sql.*"%>

<%
try{
//  Class.forName("com.mysql.jdbc.Driver");
    Class.forName("org.gjt.mm.mysql.Driver");

    out.println("found");
} catch (ClassNotFoundException ex){
    out.println("Erro<br/>");
    out.println(ex.toString());
} catch (Exception e){
    out.println(e.toString());

}

Connection ocon;

try{
ocon = DriverManager.getConnection("jdbc:mysql://localhost/cpjcoimbra?autoReconnect=true", "*****", "*****"); //password matches
out.print("connected");
} catch (Exception e){
    out.println(e.toString()+"<br/>");
}

%>

It does find the driver but I'm getting that error when i try to connect to the database.

I have this permission on catalina 50.local.policy

grant codeBase "file:/var/lib/tomcat6/WEB-INF/lib/-" {
  permission java.security.AllPermission;
};

Anyone has any idea why that error shows up?

Edit: service mysql status gives this:

 * /usr/bin/mysqladmin  Ver 8.42 Distrib 5.1.37, for debian-linux-gnu on i486
Copyright 2000-2008 MySQL AB, 2008 Sun Microsystems, Inc.
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL license

Server version      5.1.37-1ubuntu5
Protocol version    10
Connection      Localhost via UNIX socket
UNIX socket     /var/run/mysqld/mysqld.sock
Uptime:         1 hour 32 min 21 sec

Threads: 1  Questions: 103  Slow queries: 0  Opens: 171  Flush tables: 1  Open tables: 41  Queries per second avg: 0.18

解决方案

The driver is wrong - use the "com.mysql.jdbc.Driver" class.

Scriptlet code in a JSP? Oh, my - that's not good.

But those are not the cause of your problem.

Did you GRANT permissions to that username and password? Here are sample steps:

  1. Log in as root: mysql –h localhost –u root –p <ret>; password = <your admin password>
  2. "create database x;"
  3. "create user p identified by ‘q’;"
  4. "grant all on x.* to ‘x’@’%’;"

Give the username p and password q you used to create the user when you connect to the database.

Make sure the service is up and listening on port 3306. Open a command shell and type 'netstat -a' and look for a listener on port 3306.

This entry from the MySQL bug database might be pertinent as well.

For future reference, I find it helpful to paste whatever error message I get into Google. It's likely that I'm not the first person to run into a particular problem.

Even if you manage to solve this and get it to work, this is still a fatally flawed design. The JSP is connecting directly to the database - no security, except for the username and password you've entered in plain text. You really don't want to do this.

You're having a connection problem. Separate that from the JSP for starters.

  1. Can you connect using the MySQL admin tool in a command shell?
  2. Can you write a simple Java class to connect successfully to the database?

这篇关于无法从tomcat连接到MySQL数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆