I am having a really hard time getting my docker container to access the MS SQL server. I have tried following this guide here, but to no avail.
Here is my Base Dockerfile:
FROM python:3
ADD ./odbcinst.ini /etc/odbcinst.ini
RUN apt-get update && apt-get install gcc
RUN apt-get install -y tdsodbc unixodbc-dev
RUN apt-get install unixodbc-bin -y
RUN apt-get clean -y
RUN apt-get update && apt-get install -y gcc unixodbc-dev mssql-python-pyodbc
RUN pip install pyodbc
RUN pip install plaster_pastedeploy pyramid pyramid_jinja2 pyramid_debugtoolbar waitress yagmail pyodbc
And here is the other docker file that extends it:
FROM pyodbc
COPY . .
RUN pip install -e companalysis/
CMD [ "pserve", "companalysis/development.ini" ]
EXPOSE 8081
and here is my sql connection string:
strconn = 'DRIVER=
{FreeTDS};SERVER=192.168.0.6;
DATABASE=xxxx;UID=xxxx;PWD=xxx'
and no matter what I do I get this error:
Error: ('01000', u"[01000] [unixODBC][Driver Manager]Can't open lib 'FreeTDS' : file not found (0) (SQLDriverConnect)")
I would love some assistance on this.
EDIT: I have gotten it to install with this in my dockerfile:
I got it to properly install the driver with this in my dockerfile:
RUN echo "[FreeTDS]\n\
Description = FreeTDS unixODBC Driver\n\
Driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so\n\
Setup = /usr/lib/x86_64-linux-gnu/odbc/libtdsS.so" >> /etc/odbcinst.ini
RUN export PYMSSQL_BUILD_WITH_BUNDLED_FREETDS=1
RUN apt-get update && apt-get install -y unixodbc unixodbc-dev freetds-dev freetds-bin tdsodbc
I solved this issue by editing /etc/odbcinst.ini like so:
RUN echo "[FreeTDS]\n\
Description = FreeTDS unixODBC Driver\n\
Driver = /usr/lib/arm-linux-gnueabi/odbc/libtdsodbc.so\n\
Setup = /usr/lib/arm-linux-gnueabi/odbc/libtdsS.so" >> /etc/odbcinst.ini
Paths to libtdsodbc.so and libtdsS.so might be different depending on your architechture, as already pointed out in the linked questions.
If in doubt about the paths run the docker container, get into it (docker exec -ti <hash> bash) and search for the correct path.
Depending on your OS and architecture you could also use the Microsoft ODBC driver instead of the FreeTDS driver. For detailed instructions see the docs.
Related
I'm trying to build a custom docker container using the RHEL 8 UBI. As part of this I want to install the MSSQL 17 ODBC driver.
I've followed the steps outlined in Microsofts Documentation here:
https://learn.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-ver15#redhat17
And added the Microsoft repo to my yum.repos.d directory however when I try to build the container I get the following error: nothing provides e2fsprogs needed by msodbcsql17-17.6.1.1-1.x86_64
When I dug a bit further into this it looks as though it looks as though for RHEL-7 Microsoft suggest installing e2fsprogs manually you can see that here: https://learn.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-ver15#offline-installation
This unfortunately isn't possible in RHEL-8 as e2fsprogs-static has been removed: https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/8/html-single/considerations_in_adopting_rhel_8/index#removed-packages_changes-to-packages
The full output from the build is:
$ docker build -f ./test.dockerfile -t daark:1 .
Sending build context to Docker daemon 25.77MB
Step 1/7 : FROM registry.redhat.io/ubi8/ubi
---> a1f8c9699786
Step 2/7 : RUN curl https://packages.microsoft.com/config/rhel/8/prod.repo > /etc/yum.repos.d/mssql-release.repo
---> Using cache
---> 90b3e1514239
Step 3/7 : RUN yum search odbc
---> Using cache
---> b26f78d0da28
Step 4/7 : RUN yum search msodbcsql17
---> Using cache
---> c6f7751b97dc
Step 5/7 : ENV ACCEPT_EULA=Y
---> Using cache
---> 2b0003944673
Step 6/7 : RUN yum install -y unixODBC unixODBC-devel
---> Using cache
---> 1d0b8c594905
Step 7/7 : RUN yum install -y msodbcsql17
---> Running in 67c30e75fb42
Updating Subscription Management repositories.
Unable to read consumer identity
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Last metadata expiration check: 0:08:11 ago on Wed Aug 5 09:36:32 2020.
Error:
Problem: cannot install the best candidate for the job
- nothing provides e2fsprogs needed by msodbcsql17-17.6.1.1-1.x86_64
(try to add '--skip-broken' to skip uninstallable packages or '--nobest' to use not only best candidate packages)
The command '/bin/sh -c yum install -y msodbcsql17' returned a non-zero code: 1
This error is pretty reproducible here is the test dockerfile i'm using to debug
FROM registry.redhat.io/ubi8/ubi
RUN curl https://packages.microsoft.com/config/rhel/8/prod.repo > /etc/yum.repos.d/mssql-release.repo
RUN yum search odbc
RUN yum search msodbcsql17
ENV ACCEPT_EULA=Y
RUN yum install -y unixODBC unixODBC-devel
RUN yum install -y msodbcsql17
Has anyone managed to get this ODBC driver installed on an RHEL 8 UBI based container?
I found a work around that I hope will help the next person to hit this.
Rather than running yum install -y msodbcsql17 I instead used yum to download the RPM
yum download -y msodbcsql17
then used rpm -Uvh --nodeps msodbcsql17*rpm to install it.
You can use this docker file:
FROM registry.redhat.io/ubi8/ubi
RUN curl https://packages.microsoft.com/config/rhel/8/prod.repo > /etc/yum.repos.d/mssql-release.repo
RUN yum search odbc
RUN yum search msodbcsql17
ENV ACCEPT_EULA=Y
RUN yum install -y unixODBC unixODBC-devel
RUN yum download -y msodbcsql17
RUN rpm -Uvh --nodeps msodbcsql17*rpm
#daark thank you for posting your solution. Your solution got me over the problem I was facing. I ended modifying your solution to the following (in case it helps anyone else):
FROM registry.access.redhat.com/ubi8/python-38
USER root
RUN yum update --assumeyes && \
yum install --assumeyes \
unixODBC-devel \
&& yum clean all
RUN curl https://packages.microsoft.com/config/rhel/8/prod.repo > /etc/yum.repos.d/mssql-release.repo
RUN yum download -y msodbcsql17
RUN ACCEPT_EULA=y rpm -Uvh --nodeps msodbcsql17*rpm
I tried to add this to the #daark's solution as a comment, but it was too difficult to display the code properly.
Good luck to anyone else facing this issue 🍀
Latest msodbcsql17 release fixes this issue. The documentation steps work smooth once again. See docs issue
I can confirm that installation on redhat/ubi8 works with msodbcsql17-17.8.1.2-1.x86_64.rpm.
FROM redhat/ubi8
COPY msodbcsql17-17.8.1.2-1.x86_64.rpm /tmp
RUN MSSQL_PID=Developer ACCEPT_EULA=Y yum -y localinstall /tmp/msodbcsql17-17.8.1.2-1.x86_64.rpm; rm /tmp/msodbcsql17-17.8.1.2-1.x86_64.rpm
I'm trying to install Adminer from Ubuntu repository using:
sudo apt install adminer
Installation works fine but can't find the file /etc/adminer/apache.conf to use with Apache server. The folder /etc/adminer/ is empty and can't find it anywhere with find command.
Any help?
Thanks in advance.
In the next few steps, I'll show you how I installed adminer for Ubuntu 18.04.1 LTS .
After installation with apt package manager change into the adminer directory.
cd /usr/share/adminer
There you will find a file called compile.php.
Run the following command and the adminer-X.X.X.php (X.X.X for your version) file will be created.
sudo php compile.php
Create the apache adminer configuration file.
sudo echo "Alias /adminer.php /usr/share/adminer/adminer-X.X.X.php" | sudo tee /etc/apache2/conf-available/adminer.conf
Now you'll need to activate the configuration.
cd /etc/apache2/conf-available/
sudo a2enconf adminer.conf
Reload your apache webserver.
sudo systemctl reload apache2.
Test in your browser of choice (localhost/adminer.php)
This source was really helpful:
https://www.linuxhelp.com/how-to-install-adminer-on-ubuntu-16-04/
Install Apache:
sudo apt-get install apache2
Install PHP:
sudo apt-get install php libapache2-mod-php php-mysql
Install Adminer:
sudo wget "http://www.adminer.org/latest.php" -O /var/www/html/adminer.php
Once the installation completes, restart Apache.
sudo service apache2 restart
At this point, the setup is complete. You can access Adminer at the following address.
http://[SERVER_IP]/adminer.php
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
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
I am trying to connect with a MSSQL database using laravel homestead. It always throws the exception:
PDOException in Connector.php line 55: could not find driver.
I've seen a lot of people talking about FreeTDS and Sybase drivers, but I can't seem to figure out what I need to enable laravel/php access to MSSQL databases from a Linux Web Server running the Laravel Homestead Vagrant box.
I did lots of R&D for resolving this issue. Finally found the solution:
Laravel vagrant is using php 7. For mssql support we have to enable the relevant sybase driver for linux :
To Enable MSSQL Support for PHP 7:
First, ssh into your box vagrant ssh from the Homestead folder.
Command : vagrant ssh
Install the Sybase package for enabling the support for PDO and Mssql.
Command : sudo apt-get install php7.0-sybase
then run php -m on the ssh to make sure pdo_dblib is enabled.
All Done!!!! Cheers
The following works for php72. You may try changing the version of php in the commands.
vagrant ssh
#set the default PHP version to 7.2
php72
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
exit
sudo apt-get update
sudo ACCEPT_EULA=Y apt-get install msodbcsql17
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
sudo apt-get install unixodbc-dev
sudo pecl config-set php_ini /etc/php/7.2/fpm/php.ini
sudo pecl install sqlsrv
sudo pecl install pdo_sqlsrv
sudo su
printf "; priority=20\nextension=sqlsrv.so\n" > /etc/php/7.2/mods-available/sqlsrv.ini
printf "; priority=30\nextension=pdo_sqlsrv.so\n" > /etc/php/7.2/mods-available/pdo_sqlsrv.ini
exit
sudo phpenmod -v 7.2 sqlsrv pdo_sqlsrv
sudo systemctl restart php7.2-fpm
# restart nginx, if needed:
sudo systemctl restart nginx.service
References:
https://learn.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-ver15#ubuntu17
https://learn.microsoft.com/en-us/sql/connect/php/installation-tutorial-linux-mac?view=sql-server-ver15#installing-the-drivers-on-ubuntu-1604-1804-and-2004
Laravel uses PDO extension to connect to database, you could check if the extension is enabled using
var_dump(class_exists('PDO'))
Also, if you could post the specific error, it would be great.
Thanks for this. Still works as of now with:
php7.2-sybase
using DB_HOST= IP_ADDRESS\NAMED_INSTANCE
Commenting out the PORT directive in both .env and database.php