从 Linux 机器通过 JDBC 使用 Windows 身份验证连接到 SQL 服务器
Connect To SQL Server With Windows Authentication From A Linux Machine Through JDBC
我希望能够使用 jdbc 和 windows 身份验证连接到 SQL 服务器。
我在互联网上看到一些答案说我应该将以下 属性 添加到连接字符串中:
integratedSecurity=true;
并添加
sqljdbc_auth.dll
到 java 路径。
但是,据我所知,这仅在我从 Windows 机器连接时适用。
当我在 Linux 机器上尝试这个时,我得到:
java.sql.SQLException: This driver is not configured for integrated authentication
我的问题是如何从 Linux 机器上进行。
谢谢
TL;DR
无法对 JDBC 从 Linux 上的 JVM 运行 到 MSSQL 的连接使用本机 Windows 身份验证].
这篇 MSDN 文章解释了在 Linux 上使用 JDBC 的身份验证方法、潜在错误和可用选项:
...in the JDBC 4.0 driver, you can use the authenticationScheme
connection property to indicate how you want to use Kerberos to
connect to SQL. There are two settings here.
NativeAuthentication (default) – This uses the sqljdbc_auth.dll and is specific to the Windows platform. This was the only option
prior to the JDBC 4.0 driver.
JavaKerberos – Makes use of the Java API’s to invoke kerberos and does not rely on the Windows Platform. This is java specific and not
bound to the underlying operating system, so this can be used on both
Windows and Linux platforms.
...
The following document outlines how to use Kerberos with the JDBC
Driver and walks through what is needed to get JavaKerberos working
properly.
使用 Kerberos 集成身份验证连接到 SQL 服务器
http://msdn.microsoft.com/en-us/library/gg558122%28v=sql.110%29.aspx
好吧,最后我回答了我自己的问题:
在使用 Microsoft JDBC 驱动程序的 linux 机器上使用 Windows 身份验证是不可能的。
使用以下连接字符串可以使用 jTDS JDBC 驱动程序:
jdbc:jtds:sqlserver://host:port;databaseName=dbname;domain=domainName;useNTLMv2=true;
谢谢大家的评论
我知道这是一个比较老的话题,但万一 Google 派人来这里:
SQL 服务器有两个主要的 JDBC 驱动程序。一个来自 Microsoft,另一个来自 jTDS。令人惊讶的是,jTDS 可以使用来自其他平台的 Windows 身份验证 (NTLM) 进行连接,包括 Linux,如此处所述:http://jtds.sourceforge.net/faq.html#windowsAuth。当然,它也可以使用 SQL-authenticated 登录。 SQL-从任何 OS 使用经过身份验证的登录并不比其他任何更难,所以不要忘记这些选项。
Microsoft 提供的版本是@mjn 从文档中引用的版本。它能够通过指定 integratedSecurity=true
、authenticationScheme=javaKerberos
和 authentication=NotSpecified
.
使用 Windows 身份验证进行连接
即使您不特意去寻找更多的困惑,也很难让它工作,所以请始终牢记您使用的是哪个驱动程序 - 并在这些帖子中告诉我们,以便您可以得到更具体的帮助。
对于使用 DBeaver 的用户,连接到 SQL 服务器数据库的方法是:
为了使用 DBeaver
从 Linux Debian 连接到 SQL 服务器
1.- Select SQL 服务器 jTDS 驱动程序
2.- 输入连接信息
3.- 转到驱动程序属性选项卡并添加域、用户、密码
请注意,在某些 post 中,我发现他们需要将 属性 USENTLMV2 更改为 TRUE,但通过将 USERTLNMV2 设置为 true 或 false,它对我有用。
我发现的一个问题是,当我尝试使用我的用户名和密码连接到数据库时,抛出了下一个错误:
登录失败。登录来自不受信任的域,不能与 Windows 身份验证一起使用。
由于我的用户即将过期而引发此错误。我尝试了另一个 AD 用户,它可以连接。
此 JDBC URL 经验证可与最新的 Microsoft SQL 服务器 JDBC 驱动程序一起使用:
jdbc:sqlserver://[server]:[port];database=[db\;trustServerCertificate=true;integratedSecurity=true;user=[user without domain];password=[pw];authenticationScheme=NTLM;domain=[domain];authentication=NotSpecified
示例:
jdbc:sqlserver://mysql.myorg.com:1433;database=mydb;trustServerCertificate=true;integratedSecurity=true;user=myuser;password=mypwd;authenticationScheme=NTLM;domain=ad.myorg.com;authentication=NotSpecified
我能够连接到 SQL Server 2016 数据集市和 JDBC 连接 Microsoft JDBC Driver using Windows Authentication using the following script on a Ubuntu Linux Docker 图片 运行 Windows 10.
# initializes spark session
from pyspark.sql import SparkSession
spark = SparkSession\
.builder\
.master('local[*]')\
.appName('FDM')\
.config("spark.driver.extraClassPath","pyspark_jars/*")\
.config('spark.executor.memory', '4g')\
.config('spark.driver.memory', '16g')\
.config('spark.executor.cores', '4')\
.getOrCreate()
jdbc_url = '''jdbc:sqlserver://SERVER;databaseName=DBNAME;trustServerCertificate=true;integratedSecurity=true;user=USERID;password=PASSWORD;authenticationScheme=NTLM;domain=US;authentication=NotSpecified'''
spark_df = spark.read\
.format("jdbc")\
.option("url", jdbc_url)\
.option("driver","com.microsoft.sqlserver.jdbc.SQLServerDriver")\
.option("query", 'select top(1000) * from SCHEMA.TABLE')\
.option("fetchsize", 100000)\
.load()
spark_df.write.csv('TEST.csv', mode = "overwrite", header=True)
我希望能够使用 jdbc 和 windows 身份验证连接到 SQL 服务器。 我在互联网上看到一些答案说我应该将以下 属性 添加到连接字符串中:
integratedSecurity=true;
并添加
sqljdbc_auth.dll
到 java 路径。
但是,据我所知,这仅在我从 Windows 机器连接时适用。 当我在 Linux 机器上尝试这个时,我得到:
java.sql.SQLException: This driver is not configured for integrated authentication
我的问题是如何从 Linux 机器上进行。
谢谢
TL;DR
无法对 JDBC 从 Linux 上的 JVM 运行 到 MSSQL 的连接使用本机 Windows 身份验证].
这篇 MSDN 文章解释了在 Linux 上使用 JDBC 的身份验证方法、潜在错误和可用选项:
...in the JDBC 4.0 driver, you can use the authenticationScheme connection property to indicate how you want to use Kerberos to connect to SQL. There are two settings here.
NativeAuthentication (default) – This uses the sqljdbc_auth.dll and is specific to the Windows platform. This was the only option prior to the JDBC 4.0 driver.
JavaKerberos – Makes use of the Java API’s to invoke kerberos and does not rely on the Windows Platform. This is java specific and not bound to the underlying operating system, so this can be used on both Windows and Linux platforms.
...
The following document outlines how to use Kerberos with the JDBC Driver and walks through what is needed to get JavaKerberos working properly.
使用 Kerberos 集成身份验证连接到 SQL 服务器 http://msdn.microsoft.com/en-us/library/gg558122%28v=sql.110%29.aspx
好吧,最后我回答了我自己的问题: 在使用 Microsoft JDBC 驱动程序的 linux 机器上使用 Windows 身份验证是不可能的。 使用以下连接字符串可以使用 jTDS JDBC 驱动程序:
jdbc:jtds:sqlserver://host:port;databaseName=dbname;domain=domainName;useNTLMv2=true;
谢谢大家的评论
我知道这是一个比较老的话题,但万一 Google 派人来这里:
SQL 服务器有两个主要的 JDBC 驱动程序。一个来自 Microsoft,另一个来自 jTDS。令人惊讶的是,jTDS 可以使用来自其他平台的 Windows 身份验证 (NTLM) 进行连接,包括 Linux,如此处所述:http://jtds.sourceforge.net/faq.html#windowsAuth。当然,它也可以使用 SQL-authenticated 登录。 SQL-从任何 OS 使用经过身份验证的登录并不比其他任何更难,所以不要忘记这些选项。
Microsoft 提供的版本是@mjn 从文档中引用的版本。它能够通过指定 integratedSecurity=true
、authenticationScheme=javaKerberos
和 authentication=NotSpecified
.
即使您不特意去寻找更多的困惑,也很难让它工作,所以请始终牢记您使用的是哪个驱动程序 - 并在这些帖子中告诉我们,以便您可以得到更具体的帮助。
对于使用 DBeaver 的用户,连接到 SQL 服务器数据库的方法是:
为了使用 DBeaver
从 Linux Debian 连接到 SQL 服务器1.- Select SQL 服务器 jTDS 驱动程序
2.- 输入连接信息
3.- 转到驱动程序属性选项卡并添加域、用户、密码
请注意,在某些 post 中,我发现他们需要将 属性 USENTLMV2 更改为 TRUE,但通过将 USERTLNMV2 设置为 true 或 false,它对我有用。
我发现的一个问题是,当我尝试使用我的用户名和密码连接到数据库时,抛出了下一个错误:
登录失败。登录来自不受信任的域,不能与 Windows 身份验证一起使用。
由于我的用户即将过期而引发此错误。我尝试了另一个 AD 用户,它可以连接。
此 JDBC URL 经验证可与最新的 Microsoft SQL 服务器 JDBC 驱动程序一起使用:
jdbc:sqlserver://[server]:[port];database=[db\;trustServerCertificate=true;integratedSecurity=true;user=[user without domain];password=[pw];authenticationScheme=NTLM;domain=[domain];authentication=NotSpecified
示例:
jdbc:sqlserver://mysql.myorg.com:1433;database=mydb;trustServerCertificate=true;integratedSecurity=true;user=myuser;password=mypwd;authenticationScheme=NTLM;domain=ad.myorg.com;authentication=NotSpecified
我能够连接到 SQL Server 2016 数据集市和 JDBC 连接 Microsoft JDBC Driver using Windows Authentication using the following script on a Ubuntu Linux Docker 图片 运行 Windows 10.
# initializes spark session
from pyspark.sql import SparkSession
spark = SparkSession\
.builder\
.master('local[*]')\
.appName('FDM')\
.config("spark.driver.extraClassPath","pyspark_jars/*")\
.config('spark.executor.memory', '4g')\
.config('spark.driver.memory', '16g')\
.config('spark.executor.cores', '4')\
.getOrCreate()
jdbc_url = '''jdbc:sqlserver://SERVER;databaseName=DBNAME;trustServerCertificate=true;integratedSecurity=true;user=USERID;password=PASSWORD;authenticationScheme=NTLM;domain=US;authentication=NotSpecified'''
spark_df = spark.read\
.format("jdbc")\
.option("url", jdbc_url)\
.option("driver","com.microsoft.sqlserver.jdbc.SQLServerDriver")\
.option("query", 'select top(1000) * from SCHEMA.TABLE')\
.option("fetchsize", 100000)\
.load()
spark_df.write.csv('TEST.csv', mode = "overwrite", header=True)