【问题标题】:Unable to connect to mysql from java. mysql in another system无法从 java 连接到 mysql。另一个系统中的mysql
【发布时间】:2015-08-04 00:12:05
【问题描述】:

我想从 mysql 在另一个系统中的 java 代码连接到 mysql。 我在另一台机器“nilotpal”中创建了一个用户。 其他机器地址为 192.168.92.93。 我可以ping到这台机器。我在哪里失踪??有人可以帮忙吗?!!

我正在使用的程序是:

    import java.sql.*;

    public class FirstExample {

        static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
        static final String DB_URL = "jdbc:mysql://192.168.92.93:3306/tution";

        //  Database credentials
        static final String USER = "nilotpal";
        static final String PASS = "nilotpal";

        public static void main(String[] args) {
            Connection conn = null;
            Statement stmt = null;
            try{
                //STEP 2: Register JDBC driver
                Class.forName("com.mysql.jdbc.Driver");

                //STEP 3: Open a connection
                System.out.println("Connecting to database...");
                conn = DriverManager.getConnection(DB_URL,USER,PASS);

                //STEP 4: Execute a query
                System.out.println("Creating statement...");
                stmt = conn.createStatement();
                String sql;
                sql = "SELECT id, first, last, age FROM Employees";
                ResultSet rs = stmt.executeQuery(sql);

                //STEP 5: Extract data from result set
                while(rs.next()){
                    //Retrieve by column name
                    int id  = rs.getInt("id");
                    int age = rs.getInt("age");
                    String first = rs.getString("first");
                    String last = rs.getString("last");

                    //Display values
                    System.out.print("ID: " + id);
                    System.out.print(", Age: " + age);
                    System.out.print(", First: " + first);
                    System.out.println(", Last: " + last);
                }
                //STEP 6: Clean-up environment
                rs.close();
                stmt.close();
            conn.close();
        }catch(SQLException se){
            //Handle errors for JDBC
            se.printStackTrace();
        }catch(Exception e){
            //Handle errors for Class.forName
            e.printStackTrace();
        }finally{
            //finally block used to close resources
            try{
                if(stmt!=null)
                    stmt.close();
            }catch(SQLException se2){
            }// nothing we can do
            try{
                if(conn!=null)
                    conn.close();
            }catch(SQLException se){
                se.printStackTrace();
            }//end finally try
        }//end try

        System.out.println("Goodbye!");

        }//end main
    }//end FirstExample

【问题讨论】:

  • 声明了2个实例变量:
  • 认为你的 mysql 数据库没有监听外部 IP 地址。检查你的mysql配置。

标签: java mysql


【解决方案1】:

来自Connect to mysql on a different server的参考

还要确保mysql用户nilotpal有远程连接权限。其他明智的 mysql-server 将不允许您的 nilotpal 用户远程登录。即来自您的服务器(来自程序)。

您可以从 mysql.user 表中确定。

mysql> select Host,User from user where User = "root";
+------------+------+
| Host       | User |
+------------+------+
| 127.0.0.1  | root |
| ::1        | root |
| localhost  | root |
| sgeorge-mn | root |
| %          | root |
+------------+------+
4 rows in set (0.01 sec)

% 表示任何主机。

要创建具有远程连接权限的用户,请使用以下 mysql 查询:

mysql> CREATE USER 'nilotpal'@'%' IDENTIFIED BY 'nilotpal';

【讨论】:

  • 我使用相同的命令添加了用户“nilotpal”。 mysql> 从用户中选择用户,主机; +----------+------------+ |用户 |主持人 | +----------+------------+ |芒果 | % | |尼洛帕尔 | % | |根 |本地主机 | +----------+------------+ 3 行(0.00 秒)
  • SELECT 不会创建用户(用于检查可用用户)。以下命令将创建用户(为此您应该有权创建用户): CREATE USER 'nilotpal'@'%' IDENTIFIED BY 'nilotpal';
  • 通过选择命令,我想显示用户“nilotpal”在主机中添加了值“%”。仅使用 create user 命令创建了用户 :) 告诉我是否需要在我的客户端系统中有任何客户端软件?目前我的客户端系统中没有任何此类客户端软件。
  • 您能否检查您的 mysql 服务器是否接受来自其他客户端(mysql-workbench 或其他)的登录。如果 mysql 服务器不接受来自其他客户端的连接,请检查以 --skip-networking 选项启动的 mysql 服务器。参考stackoverflow.com/a/2985169/4823238
【解决方案2】:

先用 ping 命令 ping 机器,如果是 ping 则检查 mysql 用户权限。

授予用户权限:

CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password';
授予 * 上的所有特权。 * TO 'newuser'@'localhost';
刷新特权;

【讨论】:

  • 已授予权限,也可以 ping 我的机器.. 仍然无法连接..
猜你喜欢
  • 1970-01-01
  • 2011-08-15
  • 1970-01-01
  • 2011-10-25
  • 1970-01-01
  • 2020-04-25
  • 2019-09-04
  • 1970-01-01
  • 2019-04-28
相关资源
最近更新 更多