5 messages in com.mysql.lists.javaRe: Cannot get connected to MySQL-3.2...
FromSent OnAttachments
sagara w21 Sep 2002 13:40 
Mark Matthews21 Sep 2002 14:44 
sagara w22 Sep 2002 01:34 
Mark Matthews22 Sep 2002 05:58 
sagara w23 Sep 2002 02:17 
Subject:Re: Cannot get connected to MySQL-3.23.52 using Connector/J 2.0.14
From:Mark Matthews (ma@mysql.com)
Date:09/21/2002 02:44:35 PM
List:com.mysql.lists.java

----- Original Message ----- From: "sagara w" <saga@yahoo.com> To: <ja@lists.mysql.com> Sent: Saturday, September 21, 2002 3:40 PM Subject: Cannot get connected to MySQL-3.23.52 using Connector/J 2.0.14

Cannot get connected to MySQL-3.23.52 using Connector/J 2.0.14

I use MySQL-3.23.52 with JDK 1.4.1 on Red Hat Linux 7.2 on a Pentium III based PC server with 256MB RAM.

I did following steps: 1. tar -xvzf mysql-connector-java-2.0.14.tar.gz 2. jar xvf mysql-connector-java-2.0.14-bin.jar 3. set CLASSPATH to

".:/usr/local/mysql-connector-java-2.0.14/mysql-connector-java-2.0.14-bin.ja r"

Here is the code fragment of my JDBCtest.java program:

import java.io.*; import java.sql.*; import javax.sql.*; import java.util.*;

class JDBCtest { public static void main(String[] args) { Connection Conn=null; Statement St = null; ResultSet RS = null;

try {

Class.forName("com.mysql.jdbc.Driver").newInstance();

System.out.println("Driver registration successful...");

Conn =

DriverManager.getConnection("jdbc:mysql://localhost/test?user=root&password= rootpass");

System.out.println("getConnection successful..."); . . .

When I run the program, it prints "Driver registration successful..." and hangs at the DriverManager.getConnection for a long time and prints the following message:

Error Occured Cannot connect to MySQL server on localhost:3306. Is there a MySQ L server running on the machine/port you are trying to connect to? (java.net.Con nectException) Sql State 08S01

I have modified the JNDIDataSource.java, complied and ran. There also it hangs at the Connection con = ds.getConnection("root", "rootpass") and prints the following message:

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) at com.mysql.jdbc.Connection.connectionInit(Unknown Source) at com.mysql.jdbc.jdbc2.Connection.connectionInit(Unknown Source) at com.mysql.jdbc.Driver.connect(Unknown Source) at com.mysql.jdbc.jdbc2.optional.MysqlDataSource.getConnection(Unknown Source) at com.mysql.jdbc.jdbc2.optional.MysqlDataSource.getConnection(Unknown Source) at JNDIDataSource.main(JNDIDataSource.java:63)

I can get connected to the same MySQL server in my Perl programs.

Can Connector/J 2.0.14 be used with MySQL-3.23.52 with JDK 1.4.1 on Red Hat Linux 7.2 on a Pentium III based PC server? If possible, could somebody please post a code sample indicating how to get connected to the MySQL server?

There's nothing wrong with your code. My guess is that when you installed RedHat, you chose "Medium Firewall", which doesn't allow TCP/IP connections to be made except to ports you allow...and MySQL is not one of them. Perl can use Unix domain sockets (which aren't firewalled) to talk to MySQL, but JDBC can not.

As root, type "lokkit", and configure a "custom" firewall that allows incoming connections to port 3306, and do a "/etc/rc.d/init.d/iptables restart" or a "/etc/rc.d/init.d/ipchains restart" (whichever is appropriate for your firewall setup), or in the worst case, re-start your machine, and you should be good to go.

-Mark

-Mark