neeraj
neeraj

Reputation: 480

Database connection string for a remote database server named 'localhost'

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?

Upvotes: 1

Views: 7892

Answers (3)

neeraj
neeraj

Reputation: 480

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

Upvotes: 0

Azad
Azad

Reputation: 5055

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

Upvotes: 0

Habib
Habib

Reputation: 223247

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)

Upvotes: 3

Related Questions