Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

i'm developing a java program and i need a database. Now i'm using MAMP and it's pretty easy but i would have a virtual machine (ubuntu server) and i need to connect my java program with this virtual machine using vitualBox.

the situation:

  • I installed VirtualBox on my mac and I installed an ubuntu-server machine
  • set "bridge adapter" in the network settings of VB
  • I installed mysql on ubuntu-server and i created a simple database (all work well by ubuntu)
  • doing ifconfig by ubuntu I get the ip:

so in the java program i made this function:

public static Connection connect(String host, int port, String dbName, String user, String passwd)
    Connection dbConnection = null;
        String dbString = null;
        dbString = "jdbc:mysql://" + host + ":" + port + "/" + dbName;

        dbConnection = DriverManager.getConnection(dbString, user, passwd);

    catch (Exception e)
        System.err.println("Failed to connect with the DB");

    return dbConnection;

and in the main() i use:

Connection con = connect(1, "", 3306, "Ciao", "root", "cocacola");

3306 was a default value. I don't know if is correct, it works on mamp, but…. how I can find the correct port that I have to use with VB?

when I ran the program I get the catch excepion… what's wrong?

share|improve this question

migrated from Dec 12 '12 at 11:24

This question came from our site for professional and enthusiast programmers.

Java code appart... Can you actually connect to the database server using your favourite MySQL client? – Álvaro González Dec 10 '12 at 15:40
You're going to have to: 1) forward ports in virtualbox and 2) open up that port in the ubuntu firewall. It might be better to just run mysql on the mac? – trojanfoe Dec 10 '12 at 15:42
@Alvaro G. Vicario: no i can't – ffmm Dec 10 '12 at 15:46
@trojanfoe: thanks, how can i do it? in virual box i don't find something for opening the post – ffmm Dec 10 '12 at 15:47
@trojanfoe - I don't know if it is off topic - simply because at present, it's likely to be both a coding issue and a computer set up issue! – Dave Dec 10 '12 at 16:16
up vote 3 down vote accepted

Check your /etc/my.cnf/ or /etc/mysql/my.cnf file for the bind_address. Your MySQL binds to localhost by default, you should put it to


bind-address            =


bind-address            =

If you don't have a my.cnf file, you can create it. In that case, you would need at least this:

bind-address            =
share|improve this answer
hi, i do what you say and now i have a different error: Failed to connect with the DB java.sql.SQLException: null, message from server: "Host '' is not allowed to connect to this MySQL server" – ffmm Dec 10 '12 at 17:10
You'll need to add correct permissions for the user you're connecting as. If you're connecting as user "root", that would be something like this: GRANT ALL PRIVILEGES ON *.* TO 'root'@'';. You'll need to execute this locally on your ubuntu VM, so in terminal connect with mysql -u root -p and execute it there. – wkoot Dec 10 '12 at 17:21
now the error is: java.sql.SQLException: Access denied for user 'root'@'' (using password: YES) – ffmm Dec 10 '12 at 17:33
Try executing your script without a password. There was no password associated with the root user for this IP. So MySQL hasn't allocated one with the GRANT. You can also use this with your password: GRANT ALL PRIVILEGES ON *.* TO 'root'@'' IDENTIFIED BY 'your_password'; – wkoot Dec 10 '12 at 17:44
thanks a lot! it works!!! i ''tik'' your answer! a little question: if i restart the router the ip change, so i should add a new connection.. there is a way to baypass this stuff? – ffmm Dec 10 '12 at 17:51

Your method takes the wrong parameters. Compare

public static Connection connect(String host, int port, String dbName, String user, String passwd)


Connection con = connect(1, "", 3306, "Ciao", "root", "cocacola");

This means, when you create the string dbString you are creating


However, you overload doesn't even match, so I'm amazed you've gotten this far (although I do C# not Java so maybe this is OK)! The Connection connect takes 5 parameters, yet when you call the method, you pass 6!

I think change

Connection con = connect(1, "", 3306, "Ciao", "root", "cocacola"); 


Connection con = connect("", 3306, "Ciao", "root", "cocacola");
share|improve this answer
He can't even connect using his favourite MySQL client, so he's got connectivity issues anyway. – trojanfoe Dec 10 '12 at 16:17
@trojanfoe Agreed. Although how did he get as far as the try catch when the signature doesn't match (is this a Java thing?) – Dave Dec 10 '12 at 16:18
No idea. Often people don't even post the real code... – trojanfoe Dec 10 '12 at 16:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .