mysql版本:mysql-5.1.30-win32.zip
mysql驱动:mysql-2.0.4.jar
tomcat版本:apache-tomcat-5.5.25.exe
系统:vista
问题:启动tomcat,mysql服务,报错如下:
java.sql.SQLException: Cannot connect to MySQL server on localhost:3306. Is there a MySQL server running on the machine/port you are trying to connect to? (java.net.ConnectException) 配置如下:
<%@ page import="java.sql.Connection"%>
<%@ page import="java.sql.DriverManager"%>
<%@ page import="java.sql.Statement"%>
<%@ page import="java.sql.ResultSet"%>
<%@ page import="java.sql.ResultSetMetaData"%>
<%
String drv = "org.gjt.mm.mysql.Driver";
String url = "jdbc:mysql://localhost:3306/WebRoot";
String usr = "root";
String pwd = "";
%>测试代码:
<%@page language="java" contentType="text/html;charset=gb2312"%>
<%@ include file="inc.jsp"%>
<%String sql = "select * from user";
try {
Class.forName(drv).newInstance();
Connection conn = DriverManager.getConnection(url, usr, pwd);
Statement stm = conn.createStatement();
ResultSet rs = stm.executeQuery(sql);
ResultSetMetaData rsmd = rs.getMetaData();
int cols = rsmd.getColumnCount();
while (rs.next()) {
for (int i = 1; i <= cols; i++) {
String field = (String) (rsmd.getColumnName(i));
String value = (String) (rs.getString(i));
out.print(field + "=" + value + ";");
}
}
rs.close();
stm.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
out.println(e);
}
%>
其他相关情况:
1,因为之前安装过安装版的mysql,后来删掉了,并且把注册表的项也给删掉了。现在电脑上的mysql用命令mysqld --console启动服务后在系统的任务管理器中看不到mysql的服务。
希望各位高手帮忙解决一下。

解决方案 »

  1.   

    在进程管理器(任务管理器)中看一下有没有mysqld.exe 这个进程?确认mysql数据库服务已经起来了。
      

  2.   

    任务管理器中进程里面已经有mysqld.exe 这个进程了。
      

  3.   


    这样说明服务起来了。再到你的MySQL安装目录中运行
    mysql -u root WebRoot看看能不能登录,你的root 没有密码?另外注意一下防火墙的设置,
      

  4.   

    我运行了mysql -u root demo
    显示Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 3
    Server version: 5.1.34-community MySQL Community Server (GPL)Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.应该是连接上了,
    demo是我建的数据库的名字,WebRoot是发布WEB应用的工程名字。非常感谢楼上这么热心。
      

  5.   

    root没有密码,防火墙也没有开。
      

  6.   

    <%
    String drv = "org.gjt.mm.mysql.Driver";
    String url = "jdbc:mysql://localhost:3306/demo";
    String usr = "root";
    String pwd = "";
    %>
    Connection conn = DriverManager.getConnection(url, usr, null);试一下。
    1. 把数据库名改成 demo
    2. password = "" 和没有是不一样的,但我也没试过 DriverManager.getConnection 中如何不传密码进去。 你先试一下传 NULL ,不行的话再试一下给root 索引设个密码。
      

  7.   

    改成null或者是给root用户加密码都不行啊,还是报同样的错误。
      

  8.   

    建议做如下试验。因为可能问题方法有好几种。1. Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/demo", "root", null);

    Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/demo", "root", null);2. 检查一下MySQL的日志文件 在 mysql的 datadir 上的hostname.err 中 看一下是程序根本没有去联接正确的服务器还是MySQL拒绝登录。
      

  9.   

    090528 17:02:04  InnoDB: Operating system error number 32 in a file operation.
    InnoDB: The error means that another program is using InnoDB's files.
    InnoDB: This might be a backup or antivirus software or another instance
    InnoDB: of MySQL. Please close it to get rid of this error.晕了,怎么说有别的程序在用?
      

  10.   

    检查一下MY。INI中的设置,运行MYSQL自己的配置程序设置一下。
      

  11.   

    六楼正解:密码和用户都是你自己定义好的  相应进行修改就行不能为webroot只能是 数据库名!
      

  12.   

    数据库没有密码;
    执行:
    shell>mysqladmin password “newpassword”;
    shell>mysql -u root -p
    password:"newpassword"that’s ok!
    Congratulations!
      

  13.   

    1 cmd 下面运行 netstat -na 命令,看看3306端口是谁占用了先。
    2 检查 MySQL 程序是否已经开启?
    3 重置Root 密码
    4 已经内容都检查和操作过后,重连接是否依然出现之前问题呢?请贴出错误问题提示。