Unable to connect to mysql database from VM using Java


Florina

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.

riggs stupid

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 rootyou'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 Florinato 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";

Related


Unable to connect to mysql database from VM using Java

Florina 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 { Stri

Unable to connect to mysql database from VM using Java

Florina 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 { Stri

Unable to connect to MySQL VM from host

oshirowanen I have a Ubuntu 18.04 Desktop 64 bithost. On this host, I created one using VirtualBox Ubuntu 18.04 Server 64 bit VM, installed MySQL on it, and created a MySQL user, a database, and a table. I have set the network adapter in this virtual machine t

Unable to connect to mysql database using Doctrine in Symfony

Madman in Hesse I'm trying to connect an Symfony 4application with a mysqldatabase , but I'm having some issues. I have installed doctrinethe following command line: composer require symfony/orm-packand composer require symfony/maker-bundle --dev. After that I

Unable to connect to (remote) MySQL database using MySQLWorkbench

username I am using Mac OS with macOS 10.11.6. I want to connect MySQLWorkbench 6.3.7 to a MySQL database stored on a Linux server. I can create a connection using Sequel Pro, but doing so in MySQLWorkbench doesn't work. This is what I'm doing: 1. Open "Setup

Unable to connect to MySQL database using Cron job

Julian I'm trying to do a cron job to delete all users who deactivated their accounts a year ago, the script works on the website, but when doing it through the cron job, I get a fatal error. Fatal error: Cannot instantiate non-existent class: pdo in /homepage

Unable to connect to MySQL database using MySQLConnection

horse skin I am using the MySqlConnection class to connect to a local MySQL database: SourceHost = ConfigurationManager.AppSettings["sourceHost"]; SourceDatabase = ConfigurationManager.AppSettings["sourceDatabase"]; SourceUsername = ConfigurationManager.AppSet

Unable to connect to mysql database using shell script

Siva Kumar v I'm having trouble connecting to MySQL DB from a shell script. Please find the following code snippet I wrote to connect to MySQL database. Please suggest this. my shell script: #!bin/bash Query="select * from Main" MySQL -u ro

Unable to connect to (remote) MySQL database using MySQLWorkbench

username I am using Mac OS with macOS 10.11.6. I want to connect MySQLWorkbench 6.3.7 to a MySQL database stored on a Linux server. I can create a connection using Sequel Pro, but doing so in MySQLWorkbench doesn't work. This is what I'm doing: 1. Open "Setup

Unable to connect to MySQL database using springboot?

Ryan S I have generated some tables in MySQL workbench and joined them as datasources in Intellij. From there, I auto-generated entity classes using the persistence tools provided by Intellij and everything looks fine. I'm not sure what happened. .The big stac

Unable to connect to mysql database using Doctrine in Symfony

Madman in Hesse I'm trying to connect an Symfony 4application with a mysqldatabase , but I'm having some issues. I have installed doctrinethe following command line: composer require symfony/orm-packand composer require symfony/maker-bundle --dev. After that I

Unable to connect to mysql database using shell script

Siva Kumar v I'm having trouble connecting to MySQL DB from a shell script. Please find the following code snippet I wrote to connect to MySQL database. Please suggest this. my shell script: #!bin/bash Query="select * from Main" MySQL -u ro

Unable to connect to MySQL database using Cron job

Julian I'm trying to do a cron job to delete all users who deactivated their accounts a year ago, the script works on the website, but when doing it through the cron job, I get a fatal error. Fatal error: Cannot instantiate non-existent class: pdo in /homepage

Unable to connect to MySQL database from SQL developer

SSPrabhu When I try to create a connection from SQL Developer to MySQL server, I get an error. I realize it treats it as Oracle DB. I tried various options but I realized that I need to install a 3rd party JDBC connector and was able to install it successfully

Unable to connect to MySQL database from SQL developer

SSPrabhu When I try to create a connection from SQL Developer to MySQL server, I get an error. I realize it treats it as Oracle DB. I tried various options but I realized that I need to install a 3rd party JDBC connector and was able to install it successfully

Unable to connect to MySQL database from SQL developer

SSPrabhu When I try to create a connection from SQL Developer to MySQL server, I get an error. I realize it treats it as Oracle DB. I tried various options but I realized that I need to install a 3rd party JDBC connector and was able to install it successfully

Unable to connect to MySQL database from SQL developer

SSPrabhu When I try to create a connection from SQL Developer to MySQL server, I get an error. I realize it treats it as Oracle DB. I tried various options but I realized that I need to install a 3rd party JDBC connector and was able to install it successfully

Unable to connect to MySQL database from SQL developer

SSPrabhu When I try to create a connection from SQL Developer to MySQL server, I get an error. I realize it treats it as Oracle DB. I tried various options but I realized that I need to install a 3rd party JDBC connector and was able to install it successfully

Unable to connect to remote database using Java and Mariadb

syllabus: I'm 97% sure that I have my Java web app set up correctly, but I'm still getting the error that the Mariadb connector is missing or something close to it. I've tried fixing it but to no avail... Added mariadb dependencies in the pom.xmlfile (as shown

Unable to connect to remote database using Java and Mariadb

syllabus: I'm 97% sure that I have my Java web app set up correctly, but I'm still getting the error that the Mariadb connector is missing or something close to it. I've tried fixing it but to no avail... Added mariadb dependencies in the pom.xmlfile (as shown

Unable to connect to remote database using Java and Mariadb

syllabus: I'm 97% sure that I have my Java web app set up correctly, but I'm still getting the error that the Mariadb connector is missing or something close to it. I've tried fixing it but to no avail... Added mariadb dependencies in the pom.xmlfile (as shown

Unable to connect to remote database using Java and Mariadb

syllabus: I'm 97% sure that I have my Java web app set up correctly, but I'm still getting the error that the Mariadb connector is missing or something close to it. I've tried fixing it but to no avail... Added mariadb dependencies in the pom.xmlfile (as shown

Unable to connect to remote database using Java and Mariadb

syllabus: I'm 97% sure that I have my Java web app set up correctly, but I'm still getting the error that the Mariadb connector is missing or something close to it. I've tried fixing it but to no avail... Added mariadb dependencies in the pom.xmlfile (as shown

Unable to connect to remote database using Java and Mariadb

syllabus: I'm 97% sure that I have my Java web app set up correctly, but I'm still getting the error that the Mariadb connector is missing or something close to it. I've tried fixing it but to no avail... Added mariadb dependencies in the pom.xmlfile (as shown

Unable to connect to database - MySQL

Nikola Atanasov I know this question has been asked many times, but in all the other posts I've searched I can't find an answer. I can't seem to connect to the database. My OS is Ubuntu 12.04 if it helps. Here is my code config.php: <?php define('DB_HOST','loc