我正在尝试连接到安装在 Windows 上的 Linux docker 容器或带有 java 代码的
sql
子系统中的 ubuntu
服务器数据库,但它无法正常工作。
我需要这个领域的真正的核心专家。我知道这个问题有很多解决方案,我尝试了很多,但没有任何效果。特殊情况是 SQL Server 安装在 Windows 上,并且我有一个 linux docker 容器,因此连接必须在 os 之间。我尝试过jtds和jdbc。
以下代码在 Windows 上执行时有效,但在 Linux 子系统或 docker 上执行时无效。
我还在连接字符串中尝试了“useNTLMv2=true”:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import net.sourceforge.jtds.jdbcx.*;
import net.sourceforge.jtds.jdbc.Driver;
public class helloWorld {
public static void main(String[] args) throws ClassNotFoundException {
//String connectionUrl = "jdbc:jtds:sqlserver://<db_ip>:1433;databaseName=someDB;useNTLMv2=true;";
String connectionUrl = "jdbc:jtds:sqlserver://<server_ip>:1433;databaseName=someDB;username=xyz;password=123";
Class.forName("net.sourceforge.jtds.jdbc.Driver");
try (Connection con = DriverManager.getConnection(connectionUrl); Statement stmt = con.createStatement()) {
String SQL = "SELECT [PLZ] FROM [City]";
ResultSet rs = stmt.executeQuery(SQL);
// Iterate through the data in the result set and display it.
while (rs.next()) {
System.out.println(rs.getString("PLZ"));
}
}
// Handle any errors that may have occurred.
catch (SQLException e) {
e.printStackTrace();
}
}
}
这是我使用 jtds 从 docker 容器中得到的异常:
java.sql.SQLException: Network error IOException: Connection refused (Connection refused)
at net.sourceforge.jtds.jdbc.JtdsConnection.<init>(JtdsConnection.java:436)
at net.sourceforge.jtds.jdbc.Driver.connect(Driver.java:184)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:270)
at de.arz.pharmafakt.datadelivery.Main.main(Main.java:93)
Caused by: java.net.ConnectException: Connection refused (Connection refused)
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:607)
at net.sourceforge.jtds.jdbc.SharedSocket.createSocketForJDBC3(SharedSocket.java:288)
at net.sourceforge.jtds.jdbc.SharedSocket.<init>(SharedSocket.java:251)
at net.sourceforge.jtds.jdbc.JtdsConnection.<init>(JtdsConnection.java:331)
与我从 ubuntu 子系统获得的代码相同的其他异常:
java.sql.SQLException: I/O Error: GSS Failed: Invalid name provided (Mechanism level: KrbException: Cannot locate default realm)
at net.sourceforge.jtds.jdbc.TdsCore.login(TdsCore.java:654)
at net.sourceforge.jtds.jdbc.JtdsConnection.<init>(JtdsConnection.java:371)
at net.sourceforge.jtds.jdbc.Driver.connect(Driver.java:184)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:677)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:251)
at helloWorld.main(helloWorld.java:16)
Caused by: java.io.IOException: GSS Failed: Invalid name provided (Mechanism level: KrbException: Cannot locate default realm)
at net.sourceforge.jtds.jdbc.TdsCore.sendMSLoginPkt(TdsCore.java:1976)
at net.sourceforge.jtds.jdbc.TdsCore.login(TdsCore.java:617)
这是我的jdbc代码:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class helloWorld {
public static void main(String[] args) {
String connectionUrl = "jdbc:sqlserver://<server_ip>:1433;databaseName=someDB;user=xyz;password=123;Encrypt=false";
try (Connection con = DriverManager.getConnection(connectionUrl); Statement stmt = con.createStatement();) {
String SQL = "SELECT PLZ FROM city";
ResultSet rs = stmt.executeQuery(SQL);
// Iterate through the data in the result set and display it.
while (rs.next()) {
System.out.println(rs.getString("PLZ") );
}
}
// Handle any errors that may have occurred.
catch (SQLException e) {
e.printStackTrace();
}
}
}
这是我在 ubuntu 上遇到的异常:
com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user
'xyz'. ClientConnectionId:21e99cbc-bd73-40a0-8101-542ddffb3a0b
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:265)
at com.microsoft.sqlserver.jdbc.TDSTokenHandler.onEOF(tdsparser.java:305)
at com.microsoft.sqlserver.jdbc.TDSParser.parse(tdsparser.java:138)
at com.microsoft.sqlserver.jdbc.TDSParser.parse(tdsparser.java:42)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.sendLogon(SQLServerConnection.java:6490)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.logon(SQLServerConnection.java:5068)
at com.microsoft.sqlserver.jdbc.SQLServerConnection$LogonCommand.doExecute(SQLServerConnection.java:5002)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:7685)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:4048)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:3487)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:3077)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectInternal(SQLServerConnection.java:2919)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:1787)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1229)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:677)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:251)
at helloWorld.main(helloWorld.java:16)
我尝试了互联网上发布的几乎所有针对此问题的解决方案。 我期待看到如何从 Linux 连接到安装在 Windows 上的 SQL Server。
我的数据库管理员解决了这个问题,他创建了一个能够进行sql身份验证的登录用户。但首先我必须检查 SQL Server 日志以详细了解问题所在,就像评论中建议的那样。