Trying to connect to mysql database with jdbc

元气小坏坏 提交于 2019-12-24 09:58:59

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/6705874/trying-to-connect-to-mysql-database-with-jdbc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!