Ubuntu ODBC Sql Server
2021-04-30 本文已影响0人
LiarMaiq
操作系统 Ubuntu 20.04
Microsoft ODBC 17
根据文章安装 Microsoft ODBC 17 Install the Microsoft ODBC driver for SQL Server (Linux) - SQL Server | Microsoft Docs
sudo su
curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
#Download appropriate package for the OS version
#Choose only ONE of the following, corresponding to your OS version
#Ubuntu 16.04
curl https://packages.microsoft.com/config/ubuntu/16.04/prod.list > /etc/apt/sources.list.d/mssql-release.list
#Ubuntu 18.04
curl https://packages.microsoft.com/config/ubuntu/18.04/prod.list > /etc/apt/sources.list.d/mssql-release.list
#Ubuntu 20.04
curl https://packages.microsoft.com/config/ubuntu/20.04/prod.list > /etc/apt/sources.list.d/mssql-release.list
#Ubuntu 20.10
curl https://packages.microsoft.com/config/ubuntu/20.10/prod.list > /etc/apt/sources.list.d/mssql-release.list
exit
sudo apt-get update
sudo ACCEPT_EULA=Y apt-get install -y msodbcsql17
# optional: for bcp and sqlcmd
sudo ACCEPT_EULA=Y apt-get install -y mssql-tools
echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc
source ~/.bashrc
# optional: for unixODBC development headers
sudo apt-get install -y unixodbc-dev
安装完成后会在/etc/
下找到odbc.ini
和odbcinst.ini
两个配置文件,DSN配置在odbc.ini
里面配置,DSN配置示例:
[NAME]
Driver=ODBC Driver 17 for SQL Server
Description=some description
Server=192.168.0.1
Port=1433
User=sa
Password=sa
Database=DB
配置完成后,使用isql
命令进行连接测试:isql DSN name password
在确认配置正确的前提下,测试可能会出现连接失败的情况,可以使用isql -v
查看错误信息,我自己遇到的错误是openssl
版本导致,我自己的openssl
版本是1.1.1f
,出现无法连接的问题,网上搜索的结果可能会告诉你修改openssl
的配置,降低协议版本,但自己实测之后并不能解决,我是通过升级openssl
解决的,下载openssl 1.1.1k
编译安装即可。
在进行Linux的ODBC开发时,还需要注意在编译链接时连接odbc
库的顺序需要是target_link_libraries(Odbc odbc msodbcsql-17)
,即首先连接odbc
,再连接具体的驱动程序库,否则你的SQLConnect/SQLDriverConnect
可能会返回SQL_INVALID_HANDLE
错误。