Connecting to SQL Server with ActiveRecord - sql-server

Have you ever had to connect to SQL Server with ActiveRecord? Is this possible? Can anyone provide some starting points?

This what I used:
From here:
http://github.com/rails-sqlserver/2000-2005-adapter/tree/master
Installation
First, you will need Ruby DBI and Ruby ODBC. To my knowledge the ADO DBD for DBI is no longer supported. The installation below is not a comprehensive walk thru on how to get all the required moving parts like FreeTDS installed and/or configured. It will also assume gem installations of both the dependent libraries and the adapter itself.
It should be noted that this version of the adapter was developed using both the ancient 0.0.23 version of DBI up to the current stable release of 0.4.0. Because later versions of DBI will be changing many things, IT IS HIGHLY RECOMMENDED that you max your install to version 0.4.0 which the examples below show. For the time being we are not supporting DBI versions higher than 0.4.0. The good news is that if you were using a very old DBI with ADO, technically this adapter will still work for you, but be warned your path is getting old and may not be supported for long.
$ gem install dbi --version 0.4.0
$ gem install dbd-odbc --version 0.2.4
$ gem install rails-sqlserver-2000-2005-adapter -s http://gems.github.com
From here: http://lambie.org/2008/02/28/connecting-to-an-mssql-database-from-ruby-on-ubuntu/
Firstly, update your ~/.profile to include the following:
export ODBCINI=/etc/odbc.ini
export ODBCSYSINI=/etc
export FREETDSCONF=/etc/freetds/freetds.conf
Then reload your .profile, by logging out and in again.
Secondly, on Ubuntu 7.10 Server I needed to install some packages.
mlambie#ubuntu:~$ sudo aptitude install unixodbc unixodbc-dev freetds-dev sqsh tdsodbc
With FreeTDS installed I could configure it like this:
mlambie#ubuntu:/etc/freetds$ cat freetds.conf
[ACUMENSERVER]
host = 192.168.0.10
port = 1433
tds version = 7.0
The important thing here is ACUMENSERVER, which is the DSN that I’ll use when connecting to the database. The host, and port are self-explanatory, and it’s worth noting that I had to use 7.0 specifically as the tds version.
Testing FreeTDS is not too hard:
mlambie#ubuntu:~$ sqsh -S ACUMENSERVER -U username -P password
sqsh: Symbol `_XmStrings' has different size in shared object, consider re-linking
sqsh-2.1 Copyright (C) 1995-2001 Scott C. Gray
This is free software with ABSOLUTELY NO WARRANTY
For more information type '\warranty'
1> use acumen
2> go
1> select top 1 firstname, lastname from tblClients
2> go
[record returned]
(1 row affected)
1> quit
Next up it’s necessary to configure ODBC:
mlambie#ubuntu:/etc$ cat odbcinst.ini
[FreeTDS]
Description = TDS driver (Sybase/MS SQL)
Driver = /usr/lib/odbc/libtdsodbc.so
Setup = /usr/lib/odbc/libtdsS.so
CPTimeout =
CPReuse =
FileUsage = 1
mlambie#ubuntu:/etc$ cat odbc.ini
[ACUMENSERVER]
Driver = FreeTDS
Description = ODBC connection via FreeTDS
Trace = No
Servername = ACUMENSERVER
Database = ACUMEN
I then tested the connection with isql:
mlambie#ubuntu:~$ isql -v ACUMENSERVER username password
+---------------------------------------+
| Connected! |
| |
| sql-statement |
| help [tablename] |
| quit |
| |
+---------------------------------------+
SQL> use ACUMEN
[][unixODBC][FreeTDS][SQL Server]Changed database context to 'Acumen'.
[ISQL]INFO: SQLExecute returned SQL_SUCCESS_WITH_INFO
SQLRowCount returns -1
SQL> select top 1 firstname from tblClients;
[record returned]
SQLRowCount returns 1
1 rows fetched
SQL> quit
OK, so we’ve got ODBC using FreeTDS to connect to a remote MSSQL server. All that’s left is to add Ruby into the mix.
mlambie#ubuntu:~$ sudo aptitude install libdbd-odbc-ruby
The last thing to test is that Ruby can use DBI and ODBC to hit the actual database, and that’s easy to test:
mlambie#ubuntu:~$ irb
irb(main):001:0> require "dbi"
=> true
irb(main):002:0> dbh = DBI.connect('dbi:ODBC:ACUMENSERVER', 'username', 'password')
=> #<DBI::DatabaseHandle:0xb7ac57f8 #handle=#<DBI::DBD::ODBC::Database:0xb7ac5744
#handle=#<odbc::database:0xb7ac576c>, #attr={}>, #trace_output=#</odbc::database:0xb7ac576c><io:0xb7cbff54>,
#trace_mode=2>
irb(main):003:0> quit
And a more complete test (only with SQL SELECT, mind you):
#!/usr/bin/env ruby
require 'dbi'
db = DBI.connect('dbi:ODBC:ACUMENSERVER', 'username', 'password')
select = db.prepare('SELECT TOP 10 firstname FROM tblClients')
select.execute
while rec = select.fetch do
puts rec.to_s
end
db.disconnect
</io:0xb7cbff54>
From here (to fix the odbc lib being in the wrong place):
http://ubuntuforums.org/showthread.php?t=433435&page=2
libtdsodbc.so
with freeTDS (freetds-dev, tdsodbc), you can either edit the path in the odbcinst.ini file for the [FreeTDS] driver section OR cp the /usr/lib/odbc/libtdsodbc.so into /usr/lib/libtdsodbc.so.
either way works when accessing mssql from the prompt
isql -v $dsn $user $passwd
i found this to be useful
http://www.unixodbc.org/doc/FreeTDS.html#Configuration
And then in the database.yml file:
development:
adapter: sqlserver
mode: odbc
dsn: dsn_name
username: my_username
password: my_password

These are the steps i've compiled for Centos 5.3. It took me a lot of trial and error to get this working. It's from a completely clean Centos installation.
Install EPEL:
rpm -Uvh http://download.fedora.redhat.com/pub/epel/5/i386/epel-release-5-3.noarch.rpm
Install ruby, rubygems, freetds, unixODBC, development tools:
yum install gcc
yum install freetds
yum install ruby-devel
yum install unixODBC-devel
yum install ruby rubygems
Install rails:
gem install rails
Install DB related gems:
gem install dbd-odbc
gem install rails-sqlserver-2000-2005-adapter -s http://gems.github.com
Download, build and install ruby-odbc:
wget http://www.ch-werner.de/rubyodbc/ruby-odbc-0.9997.tar.gz
tar zxvf ruby-odbc-0.9997.tar.gz
cd ruby-odbc-0.9997
ruby extconf.rb
make
make install
Final gem list:
# gem list
*** LOCAL GEMS ***
actionmailer (2.3.2)
actionpack (2.3.2)
activerecord (2.3.2)
activeresource (2.3.2)
activesupport (2.3.2)
dbd-odbc (0.2.4)
dbi (0.4.1)
deprecated (2.0.1)
rails (2.3.2)
rails-sqlserver-2000-2005-adapter (2.2.17)
rake (0.8.7)
You can use various tools like isql to test your ODBC connection. Brian's post covers this in nice detail so I won't repeat. In rails you need a database.yml that looks something like this:
development:
adapter: sqlserver
mode: odbc
dsn: dsnName
username: username
password: password

On Ubuntu, I use FreeTDS and the activerecord-sqlserver-adapter gem.
You can install FreeTDS through apt:
sudo apt-get install freetds
And add this to your Gemfile:
gem 'activerecord-sqlserver-adapter'
I had to change the configured FreeTDS version number in /etc/freetds/freetds.conf to 8.0 in order to get things working correctly.
[global]
tds version = 8.0
activerecord-sqlserver-adapter on GitHub
FreeTDS project

Related

How to properly install pyodbc and drivers on M1 mac

I have a new M1 pro macbook and I'm trying to install pyodbc and relevant drivers on my machine.
So far I have installed unixodbc via homebrew and ODBC drivers according to the instructions found here: https://learn.microsoft.com/en-us/sql/connect/odbc/linux-mac/install-microsoft-odbc-driver-sql-server-macos?view=sql-server-ver15https://learn.microsoft.com/en-us/sql/connect/odbc/linux-mac/install-microsoft-odbc-driver-sql-server-macos?view=sql-server-ver15
I then pip installed pyodbc but when I try to run pyodbc.connect() within a test script, python just aborts with the following message, regardless which connection string I use:
[1] 2086 abort python test.py
After investigating a bit more I find that pyodbc doesn't have any drivers. When I run pyodbc.drivers() I get an empty list. So it seems like pyodbc cannot find the drivers I installed earlier.
Here is the output when I run odbcinst -j:
unixODBC 2.3.9
DRIVERS............: /opt/homebrew/etc/odbcinst.ini
SYSTEM DATA SOURCES: /opt/homebrew/etc/odbc.ini
FILE DATA SOURCES..: /opt/homebrew/etc/ODBCDataSources
USER DATA SOURCES..: /Users/kdot/.odbc.ini
SQLULEN Size.......: 8
SQLLEN Size........: 8
SQLSETPOSIROW Size.: 8
and the odbcinst.ini file contains:
[ODBC Driver 17 for SQL Server]
Description=Microsoft ODBC Driver 17 for SQL Server
Driver=/opt/homebrew/lib/libmsodbcsql.17.dylib
UsageCount=3
I've also tried to explicitly give the driver file path to pyodbc in the connection string but then pyodbc.connect() just hangs instead of aborting right away.
I am running all of this within a virtual environment with the following versions:
python: 3.8.10
pyodbc: 4.0.32
Does anyone know how to make pyodbc see and use the relevant drivers?
First, install the ODBC driver as instructed by Microsoft here.
Further, Microsoft recommends creating symbolic links as follows:
sudo ln -s /usr/local/etc/odbcinst.ini /etc/odbcinst.ini
sudo ln -s /usr/local/etc/odbc.ini /etc/odbc.ini
However, the location of those files on M1 macs might be different, depending on the the default homebrew directory. In my case, the following paths worked:
sudo ln -s /opt/homebrew/etc/odbcinst.ini /etc/odbcinst.ini
sudo ln -s /opt/homebrew/etc/odbc.ini /etc/odbc.ini
This is what worked for me on a m2 macbook (from the official ms documentation).
Make sure you have homebrew installed.
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
brew tap microsoft/mssql-release https://github.com/Microsoft/homebrew-mssql-release
brew update
HOMEBREW_ACCEPT_EULA=Y brew install msodbcsql17 mssql-tools
sudo ln -s /opt/homebrew/etc/odbcinst.ini /etc/odbcinst.ini
sudo ln -s /opt/homebrew/etc/odbc.ini /etc/odbc.ini
The problem here is that "several critical CLI tools like nvm and brew do not have native versions built for the new M1 architecture." Source.
Luckily Apple Silicon comes with a translation layer named Rosetta 2, which is basically a CLI interface through which you have to install tools like brew in order for them to install correctly.
In this Medium post you can read how to use Rosetta 2. We got pyodbc working by installing Python 3.9, pip, wheel, openssl, unixodbc, freetds, the correct drivers and finally pyodbc (in this order) all with Rosetta 2.
Found the following link helpful to install pyodbc on MacBook M1 Chip.
We happen to get sql.h file missing error and if we try to install the wheel directly from the site it throws platform not supported error.
Follow the below document and then add the export commands into your zrc or bash file and you should be good to go.
https://whodeenie.medium.com/installing-pyodbc-and-unixodbc-for-apple-silicon-8e238ed7f216
Need more reading about the bug and how people tried to learn it.
https://github.com/mkleehammer/pyodbc/issues/846
I still had this issue with the default Homebrew & ODBC installations on macOS Ventura with a 2022 MacBook Pro.
The only thing that worked was to completely uninstall Homebrew and the ODBC drivers and then reinstall both with the prefix arch -x86_64.
$ arch -x86_64 /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
$ arch -x86_64 brew install msodbcsql18 mssql-tools18

Error: TCP Provider: Error code 0x2746. During the Sql setup in linux through terminal

I am trying to setup the ms-sql server in my linux by following the documentation
https://learn.microsoft.com/pl-pl/sql/linux/quickstart-install-connect-ubuntu?view=sql-server-2017
The SQL server status is Active (Running).
I am getting the following error while executing the command
sqlcmd -S localhost -U SA -P '<YourPassword>'
Error:
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : TCP Provider:
Error code 0x2746. Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL
Server : Client unable to establish connection.
I also tried by giving the command
sqlcmd -S 127.0.0.1 -U SA -P '<YourPassword>'
But the same error is displayed. When I tried the wrong password it also displays the same error.
[UPDATE 17.03.2020: Microsoft has released SQL Server 2019 CU3 with an Ubuntu 18.04 repository. See: https://techcommunity.microsoft.com/t5/sql-server/sql-server-2019-now-available-on-ubuntu-18-04-supported-on-sles/ba-p/1232210 . I hope this is now fully compatible without any ssl problems. Haven't tested it jet.]
Reverting to 14.0.3192.2-2 helps.
But it's possible to solve the problem also using the method indicated by Ola774, not only in case of upgrade from Ubuntu 16.04 to 18.04, but on every installation of SQL Server 2017 on Ubuntu 18.04.
It seems that Microsoft now in cu16 messed up with their own patch for the ssl-version problems applied in cu10 (https://techcommunity.microsoft.com/t5/SQL-Server/Installing-SQL-Server-2017-for-Linux-on-Ubuntu-18-04-LTS/ba-p/385983). But linking the ssl 1.0.0 libraries works.
So just do the following:
Stop SQL Server
sudo systemctl stop mssql-server
Open the editor for the service configuration by
sudo systemctl edit mssql-server
This will create an override for the original service config. It's correct that the override-file, or, more exactly "drop-in-file", is empty when used the first time.
In the editor, add the following lines to the file and save it:
[Service]
Environment="LD_LIBRARY_PATH=/opt/mssql/lib"
Create symbolic links to OpenSSL 1.0 for SQL Server to use:
sudo ln -s /usr/lib/x86_64-linux-gnu/libssl.so.1.0.0 /opt/mssql/lib/libssl.so
sudo ln -s /usr/lib/x86_64-linux-gnu/libcrypto.so.1.0.0 /opt/mssql/lib/libcrypto.so
Start SQL Server
sudo systemctl start mssql-server
If you are having issues with the client on Debian 10 with OpenSSL1.1.1 the fix is to revert to the previously default weaker key length.
To do so:
Modify /etc/ssl/openssl.cnf config file as follows
(fyi see known issues with OpenSSL 1.1.1 in Debian 10 below):
Change the last line from CipherString = DEFAULT#SECLEVEL=2 to CipherString = DEFAULT#SECLEVEL=1
https://github.com/microsoft/msphpsql/issues/1021
https://wiki.debian.org/ContinuousIntegration/TriagingTips/openssl-1.1.1
sudo apt-get install mssql-server=14.0.3192.2-2
Reverting to this version worked for me.
My scenario was a fresh install (everything latest version) on Ubuntu Server 18.04.2 receiving the client connection error from sqlcmd:
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : TCP Provider: Error code 0x2746
Simply:
TCP Provider: Error code 0x2746
This is likely a problem with openssl vs. sql-server protocol/version.
Check your openssl version. Run the following command on your terminal openssl version:
$ openssl version
OpenSSL 1.0.2k-fips 26 Jan 2017
If your openssl version is not 1.0, then you may want to solve the connection problem by one of the following options:
Option 1: Workaround your openssl
sed -i -E 's/(CipherString\s*=\s*DEFAULT#SECLEVEL=)2/\11/' /etc/ssl/openssl.cnf
Yes, it is .cnf.
This command changes your SECLEVEL to 1, if you have it in your /etc/ssl/openssl.cnf file. Done.
Option 2: Downgrade openssl.
If your openssl version is 1.1, you would probably like it to be 1.0.
This method is basic: download the source code, configure and make the binary. It may take few minutes to build everything:
cd /usr/local/src/
wget https://www.openssl.org/source/old/1.0.1/openssl-1.0.1k.tar.gz
tar -xvf /usr/local/src/openssl-1.0.1k.tar.gz
cd /usr/local/src/openssl-1.0.1k
./config --prefix=/usr/local/ --openssldir=/usr/local/openssl
make
make test
make install
mv /usr/bin/openssl /usr/bin/openssl-bak
then
cp -p /usr/local/openssl/bin/openssl /usr/bin/openssl
or
cp -p /usr/local/ssl/bin/openssl /usr/bin/openssl
ll -ld /usr/bin/openssl
openssl version
Leave comments if you need insights for something special: docker image, or different system, etc.
Upgrade from Ubuntu 16.04 to 18.04 still results in some issues
A few systems may require version 1.0 of the OpenSSL libraries to connect to SQL Server. Using OpenSSL 1.0 can be done as follows:
Stop SQL Server
sudo systemctl stop mssql-server
Open the editor for the service configuration
sudo systemctl edit mssql-server
In the editor, add the following lines to the file and save it:
[Service]
Environment="LD_LIBRARY_PATH=/opt/mssql/lib"
Create symbolic links to OpenSSL 1.0 for SQL Server to use
sudo ln -s /usr/lib/x86_64-linux-gnu/libssl.so.1.0.0 /opt/mssql/lib/libssl.so
sudo ln -s /usr/lib/x86_64-linux-gnu/libcrypto.so.1.0.0 /opt/mssql/lib/libcrypto.so
Start SQL Server
sudo systemctl start mssql-server
I hope this helps
You can either roll back to the previous version with the command sudo apt-get install mssql-server=14.0.3192.2-2 or keep the new version by following MSSQL_Ubuntu's answer.
Also disable the updates on the mssql-server package:
sudo apt-mark hold mssql-server
This will not prevent you to update it manually when you wish so.
Same problem. It's awful because im in dev now and that "great" update just killing my working time.
Update:
MS SQL version rollback helped me, but unfortunately I have to remove all my data. Thanks that it was my dev machine. All notes below tested on
ijin -> lsb_release -a
No LSB modules are available.
Distributor ID: LinuxMint
Description: Linux Mint 19 Tara
Release: 19
Codename: tara
1) I've remover MS SQL and its data
sudo rm -rf /var/opt/mssql
sudo apt-get purge mssql-server mssql-tools
sudo apt-get autoremove
sudo apt-get autoclean
2) Check available versions of MS SQL in repository
ijin -> apt-cache policy mssql-server
3) Installed custom MS SQL
sudo apt-get install mssql-server=15.0.1600.8-1 mssql-tools
4) Setup
sudo /opt/mssql/bin/mssql-conf setup
5) Mem limit, server agent
sudo /opt/mssql/bin/mssql-conf set sqlagent.enabled true
sudo /opt/mssql/bin/mssql-conf set memory.memorylimitmb 3072
4) Restart, status
sudo service mssql-server restart
sudo service mssql-server status
Probably there is some issues with interaction of openssl package and updated MS SQL, I can't find if it is true or not, but googled a few notes about it. So you can use
apt-cache policy openssl
sudo apt-get install openssl=<version>
openssl version
To change openssl version and try to connect.
Updated SQL Server to the version 14.0.3223.3-15 (Ubuntu 18.04.2 LTS) today and got exactly the same issue for both local and remote connections. Rolling back to the previous version (14.0.3192.2-2 in my case) worked for me:
sudo apt-get install mssql-server=14.0.3192.2-2
List versions installed on your machine:
apt-cache policy mssql-server
After trying a few solutions, I found this:
https://www.youtube.com/watch?v=mfLbCarRzpg
sudo ln -s /usr/lib/x86_64-linux-gnu/libssl.so.1.0.0 /opt/mssql/lib/libssl.so
sudo ln -s /usr/lib/x86_64-linux-gnu/libcrypto.so.1.0.0 /opt/mssql/lib/libcrypto.so
sudo systemctl stop mssql-server
sudo systemctl edit mssql-server
Add these lines:
[Service]
Environment="LD_LIBRARY_PATH=/opt/mssql/lib"
Then restart the server:
sudo systemctl start mssql-server
I had the very same issue from within a docker container, I had to downgrade msodbc, mssql-tools and lib ssl:
RUN ACCEPT_EULA=Y apt-get install msodbcsql17=17.3.1.1-1 mssql-tools=17.3.0.1-1 -y
RUN wget http://security.debian.org/debian-security/pool/updates/main/o/openssl1.0/libssl1.0.2_1.0.2s-1~deb9u1_amd64.deb \
&& dpkg -i libssl1.0.2_1.0.2s-1~deb9u1_amd64.deb
Now it works like a charm.
Just in case, older versions of ms odbc driver and tools can be found here: https://packages.microsoft.com/debian/9/prod/pool/main/m/
For openssl: http://security-cdn.debian.org/debian-security/pool/updates/main/o/openssl1.0/
I encountered the same issue with SQL Server 2019 (RTM - 15.0.2000.5) on Debian 10, both trying to connect locally with sqlcmd or remote through SSMS.
The same resolution as mentioned above was able to resolve this for me. My OpenSSL version was 1.0.2, this should be adjusted for whichever version is available.
sudo ln -s /usr/lib/x86_64-linux-gnu/libssl.so.1.0.2 /opt/mssql/lib/libssl.so
sudo ln -s /usr/lib/x86_64-linux-gnu/libcrypto.so.1.0.2 /opt/mssql/lib/libcrypto.so
sudo systemctl restart mssql-server
Now both local and remote connections work OK
At this moment , only this version is working, looks like problem in openssl .
Working on Debian 9 .
Before anything be sure that you have valid backup of DB.
You need to purge mssql-server
apt-get remove --purge mssql-server
and after that check and delete /var/opt/mssql and /opt/mssql .
Next use this version 15.0.1700.37-2
apt-get install mssql-server=15.0.1700.37-2
after config you should be able to connect to mssql server 2019 at localhost or 127.0.0.1
sqlcmd -S localhost -U SA -P 'YourPassword'
After 2 days working on this problem I've finally solved it! In my case, I am using Fedora 28, so for those using RHEL, I followed this tutorial:
Installing Microsoft SQL Server on Red Hat Enterprise Linux 8 Beta
So, are you using Python 3? Apparently, you need to switch to Python 2 before installing it, using the following code (I guess in Ubuntu would work as well):
sudo alternatives --config python
Create a repository from https://packages.microsoft.com/config/rhel/7/mssql-server-2017.repo
Download it, then install it without resolving dependencies.
After that, you can run the setup for mssql-conf:
sudo /opt/mssql/bin/mssql-conf setup
And continue the Microsoft documentation tutorial from that step.
Microsoft Tutorial for installing SQL Server 2017 on RHEL
Microsoft Tutorial for installing SQL Server 2017 on Ubuntu
Note: I read in some forums that SQL Server 2019 may be causing that problem, so I recommend installing the 2017 version.
I got the same issue.
My OS is Ubuntu 18.10
sudo apt-get install mssql-server=14.0.3192.2-2
Then, in my case, I could not enter my SQL server because I got the below message
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : Login failed for user 'sa'. Reason: Server is in script upgrade mode. Only administrator can connect at this time..
Then, I followed the ServerFault Answer
Each command took a process for a while in my case.
Update 2021, Fedora 34, Sql Server 2019.
If you still have that error you need to execute the following command:
dnf install openldap-compat
Or check what is actually happening in console by starting the mssql in a single mode:
sudo -u mssql /opt/mssql/bin/sqlservr -m
More info you can find here: https://learn.microsoft.com/en-us/sql/linux/sql-server-linux-troubleshooting-guide?view=sql-server-ver15#connection
I don't recommend down grading openssl any more since you should be able to fix any openssl issue you have by upgrading to a new version. In my case openssl 1.1.1k worked. The easiest way to install an openssl version not in your distro is to install from source. For full details see https://wiki.openssl.org/index.php/Compilation_and_Installation
For me the following steps were sufficient but I assume you will also need build-essentials and a few other packages.
git clone git://git.openssl.org/openssl.git
cd openssl/
git checkout OpenSSL_1_1_1k
chmod +x ./config
./config
make
sudo make install
However this will not entirely solve the problem anymore. sqlcmd seems to have a DNS resolution bug that is not in older versions. That means you need the full domain name or ipaddress plus connection protocol or port number may also be required. So while before things like
sqlcmd -S 127.0.0.1
or
sqlcmd -S <server_name>
worked just fine. Now you may need something like
sqlcmd -S tcp:127.0.0.1,<port_number> or
sqlcmd -S tcp:<server_name>.<AD domain>.<domain name>,<port_number>
On my side, the problem was caused by a mounting issue. I found the solution here: https://github.com/microsoft/mssql-docker/issues/603#issuecomment-652958304
For some reason, if you map /var/opt/mssql/, and not only /var/opt/mssql/data, it fails on a Windows filesystem. There is no problem doing that on a Linux filesystem.
If your using multiple php versions, please remove all unwanted versions
sudo apt-get purge php5.*
sudo apt-get purge php5.6 #specific version
restart apache2 or nginix server
restart php sudo service php7.4-fpm restart
After 2 days of struggle in which, as suggested by various sources, I tried to:
downgrade openssl from 3 to 1
downgrade Microsoft ODBC Driver 18 to 17
update sql 2008 r2 SP3 with TLS support
change MinProtocol = TLSv1 and CipherString = DEFAULT#SECLEVEL=1
set protocol registry in windows to accept TLS1.0-TLS1.3
without any success:
error:0A000102:SSL routines::unsupported protocol]
error:0A0C0103:SSL routines::internal error]
TCP Provider: Error code 0x2746 (the last one after all the changes)
I finally found out a solution installing a lower version of ODBC Driver 17:
libmsodbcsql-17.10.so.1.1 (DIDN'T WORK) -> libmsodbcsql-17.6.so.1.1 (WORKS)
It seems that libmsodbcsql-17.10 doesn't work with sql 2008 r2 instead libmsodbcsql-17.6 does
Update 2023, SQL Server 2012, Ubuntu 22.10 with ODBC Driver 18 (installation instructions),:
/opt/microsoft/msodbcsql18/lib64/libmsodbcsql-18.2.so.1.1
from
cat /etc/apt/sources.list.d/mssql-release.list
deb [arch=amd64,armhf,arm64] https://packages.microsoft.com/ubuntu/22.10/prod kinetic main
openssl version
OpenSSL 3.0.5 5 Jul 2022 (Library: OpenSSL 3.0.5 5 Jul 2022)
odbcinst -q -d
[ODBC Driver 18 for SQL Server]
env | grep OPEN
OPENSSL_CONF=/home/knb/.ssh/openssl-conf-TLS1.1-mssql.cnf
# see file below
# "-C ": set "TrustServerCertificate=yes;" in the connection string.
export OPENSSL_CONF=${HOME}/.ssh/openssl-conf-TLS1.1-mssql.cnf && \
sqlcmd -S myserver -Uknb -Pxxxxx -C -q "select ##version;"
Result: Microsoft SQL Server 2012 (SP4-GDR) (KB4583465) - 11.0.7507.2 (X64)
File /home/knb/.ssh/openssl-conf-TLS1.1-mssql.cnf (For most of these, I don't know what it means)
HOME = .
oid_section = new_oids
# System default
openssl_conf = default_conf
[default_conf]
ssl_conf = ssl_sect
[ssl_sect]
system_default = system_default_sect
[system_default_sect]
CipherString = DEFAULT#SECLEVEL=0
[ new_oids ]
tsa_policy1 = 1.2.3.4.1
tsa_policy2 = 1.2.3.4.5.6
tsa_policy3 = 1.2.3.4.5.7
[ ca ]
default_ca = CA_default # The default ca section
[ CA_default ]
dir = ./demoCA # Where everything is kept
certs = $dir/certs # Where the issued certs are kept
crl_dir = $dir/crl # Where the issued crl are kept
database = $dir/index.txt # database index file.
# several certs with same subject.
new_certs_dir = $dir/newcerts # default place for new certs.
certificate = $dir/cacert.pem # The CA certificate
serial = $dir/serial # The current serial number
crlnumber = $dir/crlnumber # the current crl number
# must be commented out to leave a V1 CRL
crl = $dir/crl.pem # The current CRL
private_key = $dir/private/cakey.pem# The private key
x509_extensions = usr_cert # The extensions to add to the cert
name_opt = ca_default # Subject Name options
cert_opt = ca_default # Certificate field options
default_days = 365 # how long to certify for
default_crl_days= 30 # how long before next CRL
default_md = default # use public key default MD
preserve = no # keep passed DN ordering
policy = policy_match
[ policy_match ]
countryName = match
stateOrProvinceName = match
organizationName = match
organizationalUnitName = optional
commonName = supplied
emailAddress = optional
[ policy_anything ]
countryName = optional
stateOrProvinceName = optional
localityName = optional
organizationName = optional
organizationalUnitName = optional
commonName = supplied
emailAddress = optional
[ req ]
default_bits = 2048
default_keyfile = privkey.pem
distinguished_name = req_distinguished_name
attributes = req_attributes
x509_extensions = v3_ca # The extensions to add to the self signed cert
string_mask = utf8only
[ req_distinguished_name ]
countryName = Country Name (2 letter code)
countryName_default = AU
countryName_min = 2
countryName_max = 2
stateOrProvinceName = State or Province Name (full name)
stateOrProvinceName_default = Some-State
localityName = Locality Name (eg, city)
0.organizationName = Organization Name (eg, company)
0.organizationName_default = Internet Widgits Pty Ltd
organizationalUnitName = Organizational Unit Name (eg, section)
commonName = Common Name (e.g. server FQDN or YOUR name)
commonName_max = 64
emailAddress = Email Address
emailAddress_max = 64
[ req_attributes ]
challengePassword = A challenge password
challengePassword_min = 4
challengePassword_max = 20
unstructuredName = An optional company name
[ usr_cert ]
basicConstraints=CA:FALSE
nsComment = "OpenSSL Generated Certificate"
subjectKeyIdentifier=hash
authorityKeyIdentifier=keyid,issuer
[ v3_req ]
basicConstraints = CA:FALSE
keyUsage = nonRepudiation, digitalSignature, keyEncipherment
[ v3_ca ]
subjectKeyIdentifier=hash
authorityKeyIdentifier=keyid:always,issuer
basicConstraints = critical,CA:true
[ crl_ext ]
authorityKeyIdentifier=keyid:always
[ proxy_cert_ext ]
basicConstraints=CA:FALSE
nsComment = "OpenSSL Generated Certificate"
subjectKeyIdentifier=hash
authorityKeyIdentifier=keyid,issuer
proxyCertInfo=critical,language:id-ppl-anyLanguage,pathlen:3,policy:foo
[ tsa ]
default_tsa = tsa_config1 # the default TSA section
[ tsa_config1 ]
dir = ./demoCA # TSA root directory
serial = $dir/tsaserial # The current serial number (mandatory)
crypto_device = builtin # OpenSSL engine to use for signing
signer_cert = $dir/tsacert.pem # The TSA signing certificate
# (optional)
certs = $dir/cacert.pem # Certificate chain to include in reply
# (optional)
signer_key = $dir/private/tsakey.pem # The TSA private key (optional)
signer_digest = sha256 # Signing digest to use. (Optional)
default_policy = tsa_policy1 # Policy if request did not specify it
# (optional)
other_policies = tsa_policy2, tsa_policy3 # acceptable policies (optional)
digests = sha1, sha256, sha384, sha512 # Acceptable message digests (mandatory)
accuracy = secs:1, millisecs:500, microsecs:100 # (optional)
clock_precision_digits = 0 # number of digits after dot. (optional)
ordering = yes # Is ordering defined for timestamps?
# (optional, default: no)
tsa_name = yes # Must the TSA name be included in the reply?
# (optional, default: no)
ess_cert_id_chain = no # Must the ESS cert id chain be included?
# (optional, default: no)
ess_cert_id_alg = sha1 # algorithm to compute certificate
# identifier (optional, default: sha1)

Error: apt-get install msodbcsql

When I execute the following in the terminal:
#sudo apt-get install msodbcsql
I get the following error:
Installation Failed, ODBC Driver 11 for SQL Server Detected!
I tried to reinstall following this tutorial:
https://learn.microsoft.com/en-us/sql/connect/odbc/linux/installing-the-microsoft-odbc-driver-for-sql-server-on-linux
but I still get the same error. Any ideas?
I'm using Debian with Ubuntu packages for mssql server. I had the same problem trying to reinstall mssql-tools, the message:
Installation Failed, ODBC Driver 13 for SQL Server Detected!
The solution:
In file /etc/odbcinst.ini delete the following lines:
[ODBC Driver 13 for SQL Server]
Description=Microsoft ODBC Driver 13 for SQL Server
Driver=/opt/microsoft/msodbcsql/lib64/libmsodbcsql-13.1.so.8.0
UsageCount=1
Finally, I did apt -f install and problem solved.
To solve this i did a couple of things:
I removed the contents of /etc/odbcinst.ini as suggested by Sebastian
I stopped the mssql-server service sudo systemctl stop mssql-serverI don't know if this was necessary, but fiddling with tools related to it, it seemed the decent thing to do.
I did sudo apt remove mssql-tools
Then sudo apt remove msodbcsql The mssqlodbc part appended by whatever version number
Then sudo apt install mssql-toolsthis will install the newest version of mssql-tools for which msodbcsql is a requirement and thus it will itself fetch the required (newest i should think) version of that package. At this point, the problem has been fixed, The Sql service should still be started though
sudo systemctl start mssql-server
And thus everything was fine for me again.
Please notice that i did this in elementary OS 0.4 Loki.
I was trying to install ODBC 13 but had a very similar error
Installation Failed, ODBC Driver 13 for SQL Server Detected!
I had to:
sudo apt-get remove unixodbc mssql-tools odbcinst libodbc1
manually install the package apt had downloaded (I found it in /var/cache/apt/archives/msodbcsql_13.1.4.0-1_amd64.deb)
sudo dpkg -i msodbcsql_13.1.4.0-1_amd64.deb
ldd /opt/microsoft/msodbcsql/lib64/libmsodbcsql-*
then you can reinstall unixodbc, mssql-tools, libodbc1, etc.
I had to remove unixODBC before the installation could complete successfully.
MS were serious when they said "yum remove unixODBC #to avoid conflicts"
I had otherwise followed MS removal instructions for msodbc 11 prior to installing 13. (RedHat 7 intructions applied to Fedora 25 - DNF instead of YUM)
I discovered I needed to remove /usr/local/etc/odbcinst.ini too. With that config file in place I kept getting the "Installation Failed, ODBC Driver 11 for SQL Server Detected!" error.
Thus:
$ sudo bash
# apt-get remove unixodbc mssql-tools odbcinst libodbc1
# rm /usr/local/etc/odbcinst.ini
# apt-get -f install
# sudo dpkg -i msodbcsql_13.1.4.0-1_amd64.deb
Installation Failed, ODBC Driver 11 for SQL Server Detected!
If you've installed package with registration option (which is by default), remove the driver by:
odbcinst -u -d -n "ODBC Driver 13 for SQL Server"
Eventually locate your driver's ini file by: odbcinst -j and remove the driver section manually.
See: Installing the Microsoft ODBC Driver for SQL Server on Linux and macOS

OGR2OGR with MS SQL on Ubuntu 16.04

I need to access a Microsoft SQL Server with OGR2OGR from an Ubuntu Server 16.04. It is working on Microsoft, so the basic setup is fine.
My problem is that when I run my OGR2OGR command (after installing GDAL with sudo apt-get install gdal-bin):
ogr2ogr -overwrite -f MSSQLSpatial "MSSQL:server=tcp:<DATABASE_SERVER>,<PORT>;database=<DATABASE_NAME>;uid=<USER>;Pwd=<PASSWORD>;" "<IMPORT PATH FILE>" -nln "<TABLE NAME>" -progress
I get the error:
ERROR 1: Unable to initialize connection to the server for MSSQL:"DATABASE_SERVER";
[unixODBC][Driver Manager]Can't open lib 'SQL Server' : file not found
Try specifying the driver in the connection string from the list of available drivers:
I tried to install Microsoft ODBC Driver for SQL Server based on this instruction, but it cannot be installed when gdal is installed. The error is:
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:
The following packages have unmet dependencies:
msodbcsql : Depends: unixodbc-utf16 (>= 2.3.1-1) but it is not going to be installed
E: Unable to correct problems, you have held broken packages.
I managed to install the Microsoft ODBC driver after a while but it meant removing GDAL. So I can either have GDAL or the Microsoft ODBC driver. Is there a way to solve this problem?
Thanks for your help!
UPDATE (2017.01.03): I found the following solution and I also commented on the according article on the MSDN Blog (so far without reply) here
Install UnixODBC if not already there
sudo apt-get install unixodbc unixodbc-dev
Download from somewhere: msodbcsql-11.0.2270.0.tar.gz (it could be better to use msodbcsql-13.x.x.x.tar.gz but I was not able to find it) and
tar xvfz msodbcsql-11.0.2270.0.tar.gz
cd msodbcsql-11.0.2270.0
ldd lib64/libmsodbcsql-11.0.so.2270.0
If there are missing dependencies install them, in my case it could be done like this: (everything except the apt-get install is to fix the naming of the file by creating a link:)
sudo apt-get install libssl1.0.0 libssl-dev
cd /lib/x86_64-linux-gnu
sudo ln -s libssl.so.1.0.0 libssl.so.10
sudo ln -s libcrypto.so.1.0.0 libcrypto.so.10
Installing and cleaning up:
sudo bash ./install.sh install --force --accept-license
rm -rf /tmp/msodbcubuntu
If the Driver is not found by the tool using it, (e.g. ogr2ogr or pyodbc) edit /etc/odbcinst.ini and create a connector for [SQL Server]
[SQL Server]
Description=Microsoft ODBC Driver 11 for SQL Server
Driver=/opt/microsoft/msodbcsql/lib64/libmsodbcsql-11.0.so.2270.0
Threading=1
UsageCount=2
Shamelessly from : https://learn.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-ver15
sudo su
curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
curl https://packages.microsoft.com/config/ubuntu/16.04/prod.list > /etc/apt/sources.list.d/mssql-release.list
sudo apt-get update
sudo ACCEPT_EULA=Y apt-get install msodbcsql17
Verify with
ogr2ogr --formats
Which should then list:
MSSQLSpatial -vector- (rw+): Microsoft SQL Server Spatial Database

Linux python3 - Can't open lib 'SQL Server'

I am trying to connect to an Microsoft Azure SQL server database.
This is how i am trying to connect:
conn = pyodbc.connect('DRIVER={SQL Server};SERVER=%s' % (self.config.get("Sql", "DataSource")),
user= self.config.get("Sql", "UserId"),
password=self.config.get("Sql", "Password"),
database=self.config.get("Sql", "Catalog"))
I am getting an error while excuting this line. The error:
pyodbc.Error: ('01000', "[01000] [unixODBC][Driver Manager]Can't open lib 'SQL Server' : file not found (0) (SQLDriverConnect)")
Can't figure why this is happening, Any idea?
replace DRIVER={SQL Server} with DRIVER={/opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.0.so.1.1}
Update - December 2022
The current installation instructions for the ODBC driver are here
I also recommend you install the ODBC Driver and then try to use pyodbc. I am assuming you are on an Ubuntu 15.04+ machine.
To install the ODBC Driver follow the following instructions:
sudo su
wget https://gallery.technet.microsoft.com/ODBC-Driver-13-for-Ubuntu-b87369f0/file/154097/2/installodbc.sh
sh installodbc.sh
Once you do that, install pyodbc using pip and try the following script:
import pyodbc
server = 'tcp:myserver.database.windows.net'
database = 'mydb'
username = 'myusername'
password = 'mypassword'
cnxn = pyodbc.connect('DRIVER={ODBC Driver 13 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
cursor = cnxn.cursor()
cursor.execute("SELECT ##version;")
row = cursor.fetchone()
while row:
print row
row = cursor.fetchone()
Let me know how that goes.
Cheers,
Meet
Download Dependencies depends on your platform,
(for other OS Download your Dependencies)
This example for Ubuntu:
# 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 14.04
# curl https://packages.microsoft.com/config/ubuntu/14.04/prod.list > /etc/apt/sources.list.d/mssql-release.list
#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 18.10
# curl https://packages.microsoft.com/config/ubuntu/18.10/prod.list > /etc/apt/sources.list.d/mssql-release.list
# #Ubuntu 19.04
# curl https://packages.microsoft.com/config/ubuntu/19.04/prod.list > /etc/apt/sources.list.d/mssql-release.list
exit
sudo apt-get update
sudo ACCEPT_EULA=Y apt-get install msodbcsql17
# optional: for bcp and sqlcmd
sudo ACCEPT_EULA=Y apt-get install mssql-tools
echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bash_profile
echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc
source ~/.bashrc
# optional: for unixODBC development headers
sudo apt-get install unixodbc-dev
and then change,
DRIVER={/opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.0.so.1.1}
DRIVER={ODBC Driver 17 for SQL Server}
Check those links. It solved my problems which were kind of similar.
Installing FreeTDS
Connecting to SQL-Azure using FreeTDS
If you are using an offline REHL server, then follow the below method to setup connection to Microsoft SQL Server.
Download UNIXODBC & MSSQLTools packages—e.g., unixODBC-2.3.7-1.rh.x86_64.rpm/mssql-tools-17.9.1.1-1.x86_64.rpm—from https://packages.microsoft.com/rhel/, as per your REHL version.
Place downloaded files on the REHL server via winscp or any ssh client.
Install these two files in sequence given below:
yum localinstall unixODBC-2.3.7-1.rh.x86_64.rpm
yum localinstall mssql-tools-17.9.1.1-1.x86_64.rpm)
Go to the installation folder, and copy the path as shown in e.g.,
/opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.9.so.1.1
Put this path in code:
driverpath = r"/opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.9.so.1.1"
Your problem will get solved.
I see that this post has an accepted answer but it did not work for me running Python 3.8 on Ubuntu 20.04. I tried several things but I think that ultimately, it was a security issue having to do with the openSSL package. Here are the steps I recommend.
Check the openSSL version. Do this in the Linux console
openssl version
The problematic version was 1.1.1f on my system. If that is the version you have, update it with these commands
wget https://www.openssl.org/source/openssl-1.1.1p.tar.gz -O openssl-1.1.1p.tar.gz
tar -zxvf openssl-1.1.1p.tar.gz
cd openssl-1.1.1p
./config
make
sudo make install
sudo ldconfig
openssl version
Then you have to edit /etc/ssl/openssl.cnf . Put this in the beginning
openssl_conf = default_conf
And this at the end
[ default_conf ]
ssl_conf = ssl_sect
[ssl_sect]
system_default = system_default_sect
[system_default_sect]
MinProtocol = TLSv1.2
CipherString = DEFAULT:#SECLEVEL=0
Like I said, that worked for me. Hopefully it can help someone else too.
Hi I am using this snippet, which searches and return lastest available version of ODBC Driver, or raises an error:
def select_driver():
"""Find least version of: ODBC Driver for SQL Server."""
drv = sorted([drv for drv in pyodbc.drivers() if "ODBC Driver " in drv and " for SQL Server" in drv])
if len(drv) == 0:
raise Exception("No 'ODBC Driver XX for SQL Server' found.")
return drv[-1]
print(select_driver()) # ODBC Driver 17 for SQL Server

Resources