1

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?

6
  • 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) Commented Feb 20, 2013 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. Commented Feb 20, 2013 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 Commented Feb 20, 2013 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. Commented Feb 20, 2013 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? Commented Feb 20, 2013 at 7:53

3 Answers 3

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)

Sign up to request clarification or add additional context in comments.

4 Comments

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?
@neerajnarang, no that is same as localhost, its loopback ip address.
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...
@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
0

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());
}

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.