Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a remote mysql database server setup on a machine myuniversity.edu and server is named 'localhost'. On it I have the database named 'MyDatabase'.

I want to connect it through Java.

The connection urls that I have tried are:
    jdbc:mysql://myuniversity.edu/localhost
    jdbc:mysql://myuniversity.edu/localhost/MyDatabase
    jdbc:mysql://myuniversity.edu:3306/MyDatabase

but I get the `Connection refused: connect` exception.

Could someone please tell what the connection url should be in this case?

share|improve this question
1  
localhost or 127.0.0.1 refers to your current computer (there even are jokes about it, like this). The name of the server must be myuniversity.edu. If it's not available through your network like that, try using it's IP (something like 10.0.0.15) –  Luiggi Mendoza Feb 20 '13 at 6:28
    
I believe myuniversity.edu is a domain name so to access your mysql remotely you either need to know a domain name (e.g. server) of a machine where mysql is running and then use the full domain name (e.g. server.myuniversity.edu) or you need to know an IP address of that machine. You can use localhost only to connect to local (on the same machine) instance of mysql. –  peterm Feb 20 '13 at 6:28
    
As others have noted, localhost is not really it's name. If your server is Linux/Mac/Unix, the name can be found using the hostname command, and you can find its IP address by listing the interfaces (network cards) using ifconfig -a –  Andrew Alcock Feb 20 '13 at 6:34
    
In addition to switching to a non-loopback address, you will want to verify that your university allows inbound connections, on that port. –  Perception Feb 20 '13 at 6:35
    
I have tried connecting through jdbc:mysql://theipaddress:3306/MyDatabase but I get the same error. I checked the bind-address in my.cnf file and it's 127.0.0.1 Could you please tell me if it should be the ip address of the host? –  neeraj narang Feb 20 '13 at 7:53

3 Answers 3

Not really sure if your machine name is myuniversity.edu, you can instead try the IP Address with the connection string, Localhost is the name for loopback network interface and accessible on that machine only. Also make sure if your default port for mysql (may be 3306) is open. With IP address your connection string would look like:

jdbc:mysql://192.168.0.123/MyDatabase

With IP and port it would be:

jdbc:mysql://192.168.0.123:3306/MyDatabase

(You need to replace your IP in the above string)

share|improve this answer
    
I have tried that. I tried jdbc://mysql://ipaddress:3306/MyDatabase but I get the same error. I saw the my.cnf file and the bind-address there is 127.0.0.1 Should it be the ipaddress of the system? –  neeraj narang Feb 20 '13 at 7:30
    
@neerajnarang, no that is same as localhost, its loopback ip address. –  Habib Feb 20 '13 at 7:34
    
i ran this command: netstat -atn and this is the output tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN it shows that the server is listening on 127.0.0.1 on port 3306. When I try connecting through theipaddress:3306, I get the connection refused error... –  neeraj narang Feb 20 '13 at 7:39
    
@neerajnarang check your firewall. I've similarly encountered issues like that when trying to connect to my glassfish server. hosting on port 8080 didn't work, but hosting on port 80 worked. also, use the mysql configuration wizard to ensure that remote connections to your db are allowed see link for more info –  greenkode Feb 20 '13 at 10:42

I'ts impossible to connect remotely without (IP) address try this approach

if you want to connect it via internet :

  1. OPEN CMD on your computer

  2. in CMD write ping myuniversity.edu (for example ping google.com) then you will get an ip address of the website and you can copy the ip

then try this approach :

Connection con;
try{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://THE IP ADDRESS :3306/DatabaseName");
System.out.println("CONNECTED");
}catch(Exception ex)
{
System.out.println(ex.getMessage());
}
share|improve this answer
up vote 0 down vote accepted

Ok so here's what I did to fix the issue:

  • In my.cnf file, I changed the bind-address from '127.0.0.1' to the 'host ipaddress'. This allows connecting to the remote mysql server but would not allow access for any remote host trying to connect to it.
  • To fix that, I added an entry in user table with host '%'. This allows remote hosts to connect to the database.

Now I can connect to the database with jdbc:mysql://serverIpAddress:3306/MyDatabase

share|improve this answer
    
ok. can you mark the question as answered. thanks –  greenkode Feb 20 '13 at 10:42

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.