Reputation: 95
On my host computer I have installed Wamp and I can connect using PHP for example, or the browser. My client is a virtual machine with Linux and I am trying to connect to the database on the host to add some entries there.
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 a local network, and also that I setup Wamp to work on port 8080 (it can be seen above). Another thing is that, on the virtual machine, I can access the database using a browser.
The code above generates an error:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server. at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:526) at com.mysql.jdbc.Util.handleNewInstance(Util.java:400) at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1038) at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:628) at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1014) at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2249) at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2280) at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2079) at com.mysql.jdbc.ConnectionImpl.(ConnectionImpl.java:794) at com.mysql.jdbc.JDBC4Connection.(JDBC4Connection.java:44) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:526) at com.mysql.jdbc.Util.handleNewInstance(Util.java:400) at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:399) at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:325) at java.sql.DriverManager.getConnection(DriverManager.java:571) at java.sql.DriverManager.getConnection(DriverManager.java:215) at DatabaseConnection.main(DatabaseConnection.java:12) Caused by: java.io.EOFException: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost. at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:2926) at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:560) ... 16 more
I believe the problem could be from the settings of mysql on the client, in the file /etc/mysql/my.cnf but I don't really know how to change them since I didn't find an example for this particular situation. I also thought that the fact that I changed the port to 8080, when mysql normally uses 3306, could be causing problems.
So, the question is, how could I make this work? Thanks.
Edit: I also want to add that the port change (to 8080) was made in the conf file of apache on the server. This by default is 80. So maybe it shouldn't affect the 3306 of mysql.
Upvotes: 1
Views: 2169
Reputation: 94662
Another thing is that, on the virtual machine, I can access the database using a browser.
By this do you mean you can run a web site that lives on the HOST from the browser on the VM. Thats not quite that same thing as connecting from a remote PC. In this case the code running i.e. PHP I assume, is running on the HOST and connecting from PHP to MYSQL all of which are ON THE HOST.
By default the root
account, if thats what you are using is only allowed to connect if it is being run from the same PC as MySQL is running on.
Try creating a new MySQL user account, make sure that this new account is allowed to login to MySQL from the ip of the VM.
For example:
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 the MYSQL Server from any address on your subnet, so it wont matter if the VM gets different ip addresses after each reboot.
It would also be a good idea to check that you Firewall is allowing remote connections to port 8080! And 3306 if your just confused about the port change.
In fact are you sure you changed MYSQL to listen on port 8080 or did you change Apache to listen on port 8080. If you changed apache then you would be entering a url in the browser with the :8080
. This does not mean that MySQL is listening on port 8080 In this case remove the :8080
from your
String url = "jdbc:mysql://192.168.1.236/fps";
Upvotes: 2
Reputation: 52
normally the mysql DB is listening to the 3306port and not to 8080 port unless u change it
If the 8080 port number is correct please try to ping the IP adress from the VM command line and check if ur mysql service is UP .
Upvotes: 0
Reputation: 534
try using: Class.forName("com.mysql.jdbc.Driver");
instead of: Class.forName("com.mysql.jdbc.Driver").newInstance();
Upvotes: -1