Search code examples
javamysqljdbcubuntu-server

Trying to connect to mysql database with jdbc


I have ubuntu server running on virtual machine locally and I need to connect to mysql database there. Database is in place and jdbc driver too. The only problem is that only way at the moment for my connection can be something like http://local/phpmyadmin/index.php?db=sandbox and that can't be used in jdbc connection string.

Hope, that somebody can advise a solution.


Solution

  • String url = "jdbc:mysql://localhost:3306/mysql";
    Connection con =  DriverManager.getConnection(url,"username", "pwd");
    

    Replace localhost with the IP of your VM. You have to use something other than NAT for networking in your VM (like Host-only, internal or bridged assuming VirtualBox).

    You probably have to configure your Ubuntu firewall to let the connection through.

    Also, you have to set mysql up to accept connections from the outside. In the file /etc/mysql/my.cnf edit bind-address to your ip:

    bind-address            = your-vms-ip