Unable to connect to mysql database from VM using Java
On my host I have Wamp installed and I can connect using PHP or a browser. My client is a virtual machine using Linux and I'm trying to connect to a database on the host to add some entries to it.
Here is my code:
Connection conn = null;
try {
String url = "jdbc:mysql://192.168.1.236:8080/fps";
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection(url, "username", "password");
System.out.println("Database connection established");
} catch (Exception e) {
e.printStackTrace();
} finally {
if (conn != null) {
try {
conn.close();
System.out.println("Database connection terminated");
} catch (Exception e) {}
}
}
I must mention that I'm trying to connect over my local network, and I've also set up Wamp to work on port 8080 (can be seen above). Another thing is, on the virtual machine, I can access the database using a browser.
The above code generates the error:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communication link failed
The last packet successfully sent to the server was 0ms ago. The driver has not received any packets from the server. at java.lang.java.sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native method) com. mysql.jdbc.Util.handleNewInstance(Util.java:400) com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1038) .newInstance(Constructor.java:526). MysqlIO.readPacket at com.mysql.jdbc (MysqlIO.java:628) MysqlIO.doHandshake at com.mysql.jdbc (MysqlIO.java:1014) com.mysql.jdbc.ConnectionImpl.coreConnect at com.mysql.jdbc (ConnectionImpl.java:2249).ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:java:215) at DatabaseConnection.main(DatabaseConnection.java:12) Reason: java.io.EOFException: Unable to read response from server. Expected to read 4 bytes, read 0, then the connection was unexpectedly lost. at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:560) at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:2926)...16 more
I believe the problem might be in the client's mysql settings (in the /etc/mysql/my.cnf file), but I don't really know how to change them as I haven't found an example for this particular case. I also thought that I might be causing problems by changing the port to 8080 when mysql normally uses 3306.
So the question is, how do I make this work? thanks.
EDIT: I would also like to add that the port change (to 8080) is done in apache's conf file on the server. By default it is 80. So maybe it shouldn't affect mysql's 3306.
Another thing is, on the virtual machine, I can access the database using a browser.
This means that you can run a website hosted on the HOST from a browser on the VM. That's not quite the same as connecting from a remote PC. In this case the running code (i.e. PHP I assume) runs on the host and connects to MYSQL from PHP, all on the host.
By default , if that 's the account root
you're using , that account is only allowed to connect when running on the same PC that MySQL is running on.
Try creating a new MySQL user account, make sure that new account is allowed to log in to MySQL from the VM's IP.
E.g:
CREATE USER 'Florina'@'192.168.1.%' IDENTIFIED BY 'A-Password';
GRANT ALL PRIVILEGES ON Your_Database TO 'Florina'@'192.168.1.%';
This should allow the account Florina
to connect to MYSQL Server from any address on your subnet, so it doesn't matter if the VM gets a different IP address after each reboot.
It's also a good idea to check if your firewall allows remote connections to port 8080! and 3306, if you're just confused about port changes.
Actually , are you sure you have changed MYSQL to listen on port 8080 or Apache to listen on port 8080. If apache is changed, it will be entered in the browser . It doesn't mean that MySQL is listening on port 8080, in this case, change from:8080
:8080
String url = "jdbc:mysql://192.168.1.236/fps";