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?
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)
I'ts impossible to connect remotely without (IP) address
try this approach
if you want to connect it via internet :
OPEN CMD on your computer
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());
}
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