【问题标题】:Communications link failure with jdbc and mysql in remote connection远程连接中 jdbc 和 mysql 的通信链接失败
【发布时间】:2017-07-31 09:54:13
【问题描述】:

我正在尝试将 java 文件中的数据插入到安装在另一个系统中的 mysql 数据库中。

这是我的 java 文件

package serverDemo;

import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Scanner;

import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.Statement;

public class TestIn {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.println("Enter the values");
        System.out.println("id:  ");
        int id = sc.nextInt();
        System.out.println("name:  ");
        String name = sc.next();
        System.out.println("age:  ");
        int age = sc.nextInt();

        try {
            Connection con=DriverManager.getConnection("jdbc:mysql://192.168.43.116:3306/datacollect","root","spectrum7tech");
            System.out.println ("Database connection established");

            /*Statement stmt=(Statement) con.createStatement();
            stmt.executeUpdate("insert into ser_test_detail.details("+id+",'"+name+"',"+age+")");*/

            PreparedStatement st=(PreparedStatement) con.prepareStatement("INSERT INTO datacollect.namedetails(id, name, age) VALUES(?,?,?)");
            st.setInt(1,id);
            st.setString(2,name);
            st.setInt(3,age);
            st.executeUpdate();

        } catch (Exception e) {
            // TODO: handle exception
             System.err.println("Got an exception! "); 
             System.err.println(e.getMessage()); 
             e.printStackTrace();
        }
    }
}

给定的主机名是另一个包含 mysql 数据库的系统。

我正在控制台中获取异常详细信息

Enter the values
id:  
24
name:  
easrdtfghj
age:  
23
Got an exception! 
Communications link failure

Last packet sent to the server was 1 ms ago.
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

Last packet sent to the server was 1 ms ago.
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1074)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2103)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:718)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:46)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:302)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:282)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at serverDemo.TestIn.main(TestIn.java:24)
Caused by: java.net.UnknownHostException: LAPTOP-6JMVNJOQ
    at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
    at java.net.InetAddress$1.lookupAllHostAddr(Unknown Source)
    at java.net.InetAddress.getAddressesFromNameService(Unknown Source)
    at java.net.InetAddress.getAllByName0(Unknown Source)
    at java.net.InetAddress.getAllByName(Unknown Source)
    at java.net.InetAddress.getAllByName(Unknown Source)
    at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:243)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:280)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2026)
    ... 12 more

我尝试过使用 IP 地址而不是主机名。

如何建立连接?

请帮帮我。

【问题讨论】:

  • 带有MySQL的机器和你安装在同一个网络吗?
  • 是否需要在同一个网络中连接两个系统?
  • 在最简单的情况下,是的。如果数据库服务器和您的系统不在同一个路由器后面(在同一个网络中),数据库服务器将需要一个static public ip,以便网络外的机器可以连接到它
  • 公网ip要设置在db url中?

标签: java mysql remote-access remote-connection


【解决方案1】:

1) 转到我的.cnf

2) 注释绑定地址(因为它只接受本地连接)

“绑定地址=127.0.0.1”

"#bind-address = 127.0.0.1"

3) 重启mysql

【讨论】:

  • 谢谢,但请说,我应该在哪里找到 my.cnf?
  • 第一次遇到这个问题,要不要修改my.cnf文件?
  • 是的,正如您所见,bind-address 设置为仅接受来自本地主机的连接,因此不允许您从远程计算机连接,请将其更改为 0.0.0.0 或仅注释它
  • 如果我错了,请纠正我。我有 6 个名为“my.cnf”的文件,没有任何名为 bind 或 address 的词。
猜你喜欢
  • 1970-01-01
  • 2014-09-10
  • 2012-12-21
  • 2015-01-27
  • 2018-03-05
  • 1970-01-01
  • 2017-01-12
  • 2016-01-01
  • 1970-01-01
相关资源
最近更新 更多