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?

Nikhil
  • 2,857
  • 9
  • 34
  • 58
neeraj narang
  • 480
  • 3
  • 13
  • 25
  • 1
    `localhost` or `127.0.0.1` refers to your current computer (there even are jokes about it, like [this](http://nerdnirvana.org/wp-content/uploads/2010/05/no-place-like-localhost.jpg)). 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 06: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 06: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 06: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 06: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 07:53
  • Ok after changing the bind-address to 0.0.0.0 I was able to reach the mysql server but now I'm getting the error `"Host 'the host name' is not allowed to connect to this MySQL server"` – neeraj narang Feb 20 '13 at 08:05

3 Answers3

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)

Habib
  • 219,104
  • 29
  • 407
  • 436
  • 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 07:30
  • @neerajnarang, no that is same as localhost, its loopback ip address. – Habib Feb 20 '13 at 07: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 07: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](http://stackoverflow.com/questions/8380797/enable-remote-mysql-connection) for more info – greenkode Feb 20 '13 at 10:42
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());
}
Azad
  • 5,047
  • 20
  • 38
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

neeraj narang
  • 480
  • 3
  • 13
  • 25