【问题标题】:How can the java SQL connector be used to connect to a database on a different computerjava SQL连接器如何用于连接不同计算机上的数据库
【发布时间】:2017-01-03 17:34:52
【问题描述】:

我的计算机上有一个用于测试的简单数据库,我正在尝试从我的笔记本电脑上的数据库中检索一些信息。所以我希望我的笔记本电脑请求查看我的计算机 MySQL 数据库中的信息。下面显示了我试图在我的笔记本电脑上运行的 java 代码,以收集学生表中的第一个条目,该表位于我的计算机上。

我的笔记本电脑和电脑上都安装了 MySQL 工作台,如果电脑将存储数据而笔记本电脑只提取数据,是否需要同时在两台机器上。

到目前为止,我从研究中了解到,应该在 url 中使用公共 ip 而不是计算机的 ip,所以我添加了它,但我收到了 CommunicationsException 以及“连接超时”堆栈跟踪。我已经阅读了this 答案和this 对类似问题的答案,但是我很难理解这两种解决方案,有人可以向我推荐使用 MySQL 从数据库远程访问数据的初学者指南。

public class TestRemote{
    //JDBC variables
    Connection connection;
    Statement statement;
    ResultSet resultSet;

    //String variables
    String url;
    String user;
    String password;

    public static void main(String[] args) {
        TestRemote sql = new TestRemote();
        ArrayList<String> firstnames = sql.getColumn("students", "firstname", "studentid=4");

        System.out.println(firstnames.get(0));
    }

    // Constructor
    public TestRemote()
    {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            System.out.println("couldnt find class");
        }
        url = "jdbc:mysql://81.159.3.167:3306/test";           //?autoReconnect=true&useSSL=false";
        user = "user";
        password = "pass123";

        connection = null;
        statement = null;
        resultSet = null;      
   }

    private void closeConnection(){
        try{
           if(connection != null)
               connection.close();
           if(statement != null)
               statement.close();
           if(resultSet != null)
               resultSet.close();

           connection=null; resultSet=null; statement=null;
        }catch(Exception e){
               e.printStackTrace();
        }
     }

     public ArrayList<String> getColumn(String table, String column, String where) {
         ArrayList<String> resultsArray = new ArrayList<String>();
         try {
             connection = DriverManager.getConnection(url, user, password);
             statement = connection.createStatement();
             if(!where.equals(""))
                 resultSet = statement.executeQuery("SELECT "+column+" FROM "+table + " WHERE "+where);
             else
                 resultSet = statement.executeQuery("SELECT "+column+" FROM "+table);

             while(resultSet.next()) {
                 String val = resultSet.getString(1);
                 if(val==null)
                     resultsArray.add("");
                 else
                     resultsArray.add(val);
             }
             //resultsArray = (ArrayList<String>) resultSet.getArray(column);
         } catch (SQLException ex) {
             Logger lgr = Logger.getLogger(Model.class.getName());
             lgr.log(Level.SEVERE, ex.getMessage(), ex);
         }
         closeConnection();

         return resultsArray;
     }
}

【问题讨论】:

  • 两台笔记本电脑都安装了 MySQL 工作台 - 您需要连接 MySQL 数据库,而不是工作台。此外,只有一个很好。第二个可以使用它的 IP 地址通过同一网络访问它。

标签: java mysql remote-access


【解决方案1】:

您的 Java 代码可能没问题。但问题是,另一台机器上是否有 MySQL 服务器在公共 IP 上的 3306 端口上运行并监听?默认情况下,它应该只侦听 localhost,因此您需要更改 MySQL 安装,以便它侦听公共 IP。还要确保没有防火墙阻止访问。尝试连接笔记本电脑上的工作台以访问另一个盒子上的 MySQL 服务器。如果您运行了此程序,请再次尝试您的 Java 代码。

【讨论】:

  • 你能指导我看一个教程,该教程建议使用哪个设置来允许笔记本电脑查询我服务器上的数据库
  • /etc/mysql/my.cnf 中将bind-address127.0.0.1 设置为它应该侦听的IP,或者如果您希望它侦听包括127.0.0.1 在内的所有接口,则设置为0.0.0.0。如果用# 符号注释掉,别忘了评论它。
  • 我在 Windows 上运行的 MySQL 文件夹中似乎找不到目录等,它会位于其他地方吗?
  • 没关系,我是通过Server -> Options文件找到的,通过菜单栏
【解决方案2】:

我的笔记本电脑和电脑上都安装了 MySQL 工作台,是吗? 如果计算机将存储数据,则必须在两台机器上 而笔记本电脑只提取数据。

这里不是你所谓的“计算机”是你的服务器。它不需要mysql工作台。它只需要mysql服务器

应该在 url 中使用公共 ip 而不是 电脑

数据库几乎不应该暴露在公共 IP 地址上。如果您的两台计算机都在 LAN 上,则专用网络 IP 是服务器应该侦听的内容,这就是您应该在连接字符串上使用的内容。

CommunicationsException 以及堆栈中的“连接超时” 追踪

因为服务器没有运行,没有监听该 ip:port 或防火墙丢弃数据包。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-07-26
    • 2012-03-25
    • 1970-01-01
    • 2020-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多