Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can't connect to mysql db using java on Linux 1

Status
Not open for further replies.

Mellegem

Technical User
Apr 3, 2003
49
ZA
Hi all
I'm working on Linux(Debian) and trying to access mysql locally
through a java application. When I run the following code snip

Code:
Class.forName("com.mysql.jdbc.Driver").newInstance();
	try{
	    java.sql.Connection con;
	    con = DriverManager.getConnection("jdbc:mysql://192.168.0.5/db1?user=root&password=dorris");
...

pointing to the local machine I get
SQLException: Unable to connect to any hosts due to exception: java.net.ConnectException: Connection refused

The same statement works perfectly when connecting to mysql on my windows laptop.

PHP works perfectly on the linux box accessing mysql locally using the same username and password. Root is set to have full permissions from any host.

Can anybody think what the problem could be??

Thanks
Mellegem
 
the problem may be the odbc classes. in windows u have to setup an ODBC. i dont know how it is for Linux...

Known is handfull, Unknown is worldfull
 
What are the results to `SELECT host, user FROM user`?
 
The problem has nothing to do with MySQL, but rather with your network. Is MySQL running on the same box as the Java application? If so, try replacing the IP address in the connection string with 127.0.0.1 or localhost.

//Daniel
 
Are you running a firewall (iptables or ipchains) on the Linux machine.
Can you `telnet linuxhost 3306` from another machine?
 
Hi all thanks for the great response!
I've tried a number of options including localhost and 127.0.0.1 for hostname. Heres the best I can do for the output of the query asked for above. It doesn't fit very well..

|localhost|root|14ed2c76587c5fb6|Y|Y|Y|Y|Y|Y|Y|Y|Y|Y|Y|Y|Y|Y|
localhost | | | N | N | N | N | N | N | N | N | N | N | N | N | N | N |
| Wuler | | | N | N | N | N | N | N | N | N | N | N | N | N | N | N |
| localhost | debian-sys-maint | 452d9b183c800da2 | N | N | N | N | N | N | Y | Y | N | N | N | N | N | N |
| localhost|java|35d489185a67be26|Y|Y|Y|Y|Y|Y| Y | Y | Y | Y | Y | Y | Y | Y |

funny thing is I can't connect to the java user that I've just set up for testing.*shrug*. btw mysql and java and php are all running on localhost and user root which I have used in above example connects successfully from localhost.
 
From what I can see, all your users are only allowed to connect is on the localhost.
You can add or edit the current entry where user=root and set host='%'
I've found that the Windows installation automatically adds an entry which allows root to connect from anywhere, whereas it has to be added or changed on the Linux installtion.
Remember to flush your privileges after making the change.
 
Hi I've sorted it out, it turns out that while php connects to mysql using sockets, java uses ports and that is where the inconsistency came in. After trying to telnet my mysql server (thanks rzs0502) and failing miserably I found the line "Skip networking" and commented it out. After restarting mysql there was no more problems. [2thumbsup]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top