【问题标题】:Trying to connect to mysql database with jdbc尝试使用 jdbc 连接到 mysql 数据库
【发布时间】:2011-10-06 01:26:05
【问题描述】:

我在本地虚拟机上运行 ubuntu 服务器,我需要连接到那里的 mysql 数据库。 数据库已到位,jdbc 驱动程序也已到位。唯一的问题是,目前我连接的唯一方式可能是http://local/phpmyadmin/index.php?db=sandbox,并且不能在 jdbc 连接字符串中使用。

希望有人可以提出解决方案。

【问题讨论】:

    标签: java mysql jdbc ubuntu-server


    【解决方案1】:
    String url = "jdbc:mysql://localhost:3306/mysql";
    Connection con =  DriverManager.getConnection(url,"username", "pwd");
    

    localhost 替换为您的 VM 的 IP。你必须在你的虚拟机中使用 NAT 以外的东西来联网(比如 Host-only, internal or bridged 假设是 VirtualBox)。

    您可能必须配置 Ubuntu 防火墙以允许连接通过。

    另外,您必须设置 mysql 以接受来自外部的连接。 在文件/etc/mysql/my.cnf 中编辑绑定地址到你的ip:

    bind-address            = your-vms-ip
    

    【讨论】:

    • 我正在使用 VMware Workstation,目前我将我的 url 替换为 String url = "jdbc:mysql://my_vm_ip:3306/database_name";,但我得到了 Details : com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.。我想这可能完全是由于火灾而发生的,但我读到,它默认是禁用的,或者正如你提到的那样,网络配置可能是问题。
    • 您的工作站是如何“连接”的? NAT?桥接?看看我编辑的答案,也许有帮助。
    • 是的。它确实有帮助。我在SF问过类似的问题,因为我认为它会更合适。现在一切正常。这是另一个问题serverfault.com/questions/291378/…
    • 我只是想知道,因为您在问题中添加了赏金。
    猜你喜欢
    • 2015-07-10
    • 2012-10-13
    • 2013-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-01
    • 2012-10-20
    • 1970-01-01
    相关资源
    最近更新 更多