使用 ODBC.jl 从 Ubuntu Linux 上的 Julia 连接到 SQL 服务器将无法连接

Connection to SQL Server from Julia on Ubuntu Linux using ODBC.jl won't connect

我已经尝试了所有可以在网上找到的排列,但仍然没有成功。我知道连接是可能的,因为它可以从 R、dbeaver 和命令行 sqlcmd 轻松工作,但我无法使用 ODBC.jl

从 Julia 使其工作
$ sqlcmd -S 66.66.66.66,1433 -U my.name -P mypasswd -d database_name -Q "SELECT TOP 5 stuff_id FROM table_name;"

stuff_id
-----------
A81064     
A82027     
A82046     
A82055     
A83011     

(5 rows affected)

但是在 Julia 1.7.0 中我得到了这个错误:

julia> using ODBC

julia> using DBInterface

julia> ODBC.drivers()
Dict{String, String} with 4 entries:
  "unixODBC"                          => "Driver=/usr/lib/x86_64-linux-gnu/libodbc.so.2[=15=]UsageCount…
  "ODBC Drivers"                      => ""
  "ODBC Driver 17 for SQL Server"     => "Driver=/opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.8…
  "unixODBC/usr/lib/x86_64-linux-gnu" => "Driver=/opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.8…

julia> 

con = ODBC.Connection("Driver={ODBC Driver 17 for SQL Server};Server=66.66.66.66:1433,Database=database_name,UID=my.name,PWD=mypasswd")


ERROR: HYT00: [Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired08001: [Microsoft][ODBC Driver 17 for SQL Server]MAX_PROVS: Connection string is not valid [87]. 08001: [Microsoft][ODBC Driver 17 for SQL Server]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online.
Stacktrace:
 [1] error(s::String)
   @ Base ./error.jl:33
 [2] driverconnect(connstr::String)
   @ ODBC.API ~/.julia/packages/ODBC/qhwMX/src/API.jl:114
 [3] connect
   @ ~/.julia/packages/ODBC/qhwMX/src/API.jl:351 [inlined]
 [4] ODBC.Connection(dsn::String; user::Nothing, password::Nothing, extraauth::Nothing)
   @ ODBC ~/.julia/packages/ODBC/qhwMX/src/dbinterface.jl:57
 [5] ODBC.Connection(dsn::String)
   @ ODBC ~/.julia/packages/ODBC/qhwMX/src/dbinterface.jl:55
 [6] top-level scope
   @ REPL[5]:1

我认为这可能与 odbcinst.ini 设置有关,但不知道如何 debug/fix 它。欢迎提出任何建议。谢谢。 J

我终于找到了这个问题的有效解决方案,但这非常困难,主要是因为其中大部分都缺乏文档。我不相信 ODBC 驱动程序设置正确但它可以工作!感谢所有建议。

julia> using ODBC

julia> using DBInterface

julia> using DataFrames

julia> ODBC.drivers()
Dict{String, String} with 4 entries:
  "unixODBC"                          => "Driver=/usr/lib/x86_64-linux-gnu/libodbc.so.2[=10=]UsageCount=1[=10=]"
  "ODBC Drivers"                      => ""
  "ODBC Driver 17 for SQL Server"     => "Driver=/opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.8.so.1.1[=10=]UsageCount=6[=10=]"
  "unixODBC/usr/lib/x86_64-linux-gnu" => "Driver=/opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.8.so.1.1[=10=]UsageCount=1[=10=]"

julia> conn2 = ODBC.Connection("Driver=ODBC Driver 17 for SQL Server;SERVER=ip#;DATABASE=DBName;UID=UserName;PWD=Passwd")
ODBC.Connection(Driver=ODBC Driver 17 for SQL Server;SERVER=ip#;DATABASE=DBName;UID=UserName;PWD=Passwd)

julia> results=DBInterface.execute(conn2, "SELECT TOP 15 variable FROM table")|> DataFrame
15×1 DataFrame
 Row │ variable 
     │ String      
─────┼─────────────
   1 │ A81064
   2 │ A82027
   3 │ A82046
   4 │ A82055
   5 │ A83011
  ⋮  │      ⋮
  12 │ A84027
  13 │ A84030
  14 │ A84032
  15 │ A84033
     6 rows omitted

julia>