简体   繁体   中英

Connect to MYSQL Databases hosted in a virtual machina from Java

I trying to conncet to my MySQL Database hosted in a virtual machine, but it doesn't work for me.

Here is my setup:

  • Newest Ubuntu Server witch Apache2 mysql installed and is working
  • Database "feedback" with the table "test" set up and filled with test data
  • The network adapter is bridged. The IP of the server is (if I type in ifconfig) 10.0.0.1 and the IP of my pc is 10.0.0.4.
  • Port 3306 was manually opened. The jar mysql connector java 5.1.21-bin.jar is a Referenced Libary

Here is the Java Code:

import java.sql.*;

public class Main {

  public static void main(String[] args) {

    try {

        Class.forName("com.mysql.jdbc.Driver").newInstance();
        System.out.println("Sucess");
        Connection con = DriverManager.getConnection("jdbc:mysql://10.0.0.1/feedback","root","myrootpassword");

        con.setReadOnly(true);



    } catch (Exception e) {


        System.err.print("NO CONNECTION");
    }

  }
}

I hope that someone could help with this, because I'm working on this problem for a while.

Thanks

Just two ideas:

  • Is MySQL configured to listen on all addresses and not only local ones ? What's the value of the bind-address configuration directive ? 0.0.0.0 means "listen on all interfaces", while 127.0.0.1 means "listen on the localhost interface, for access from this host only".
  • Maybe you can extract some details about the issue in your catch statement ?

Hope this helps !

If i am correctly understand a question ,To connect VM database,then check for the following steps

1.jdbc:mysql://10.0.0.1/feedback.

2.Make sure there is no firewall blocking the access to port 3306.

3.make sure the user you are connecting with is allowed to connect from this particular hostname.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM