Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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: 192.168.1.217

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;
    try
    {
        String dbString = null;
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        dbString = "jdbc:mysql://" + host + ":" + port + "/" + dbName;

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

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

    return dbConnection;
}

and in the main() i use:

Connection con = connect(1, "192.168.1.217", 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 stackoverflow.com 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 G. Vicario 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 Rook Dec 10 '12 at 16:16
add comment

2 Answers 2

up vote 2 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 0.0.0.0.

Find:

bind-address            = 127.0.0.1

Replace:

bind-address            = 0.0.0.0

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

[mysqld]
bind-address            = 0.0.0.0
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 '192.168.1.77' 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'@'192.168.1.77';. 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'@'192.168.1.77' (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'@'192.168.1.77' 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
show 3 more comments

Your method takes the wrong parameters. Compare

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

with

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

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

jdbc:mysql://1:192.168.1.217/3306; 

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, "192.168.1.217", 3306, "Ciao", "root", "cocacola"); 

to

Connection con = connect("192.168.1.217", 3306, "Ciao", "root", "cocacola");
share|improve this answer
1  
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 Rook Dec 10 '12 at 16:18
    
No idea. Often people don't even post the real code... –  trojanfoe Dec 10 '12 at 16:19
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.