Docker Microsoft SQL Server - PHP Artisan Migrate - Could not find driver - sql-server

I am running an Alpine Linux image and I've added this to the dockerfile:
RUN apk --no-cache add curl gnupg
RUN curl -O https://download.microsoft.com/download/e/4/e/e4e67866-dffd-428c-aac7-8d28ddafb39b/msodbcsql17_17.7.2.1-1_amd64.apk
RUN curl -O https://download.microsoft.com/download/e/4/e/e4e67866-dffd-428c-aac7-8d28ddafb39b/mssql-tools_17.7.1.1-1_amd64.apk
RUN curl -O https://download.microsoft.com/download/e/4/e/e4e67866-dffd-428c-aac7-8d28ddafb39b/msodbcsql17_17.7.2.1-1_amd64.sig
RUN curl -O https://download.microsoft.com/download/e/4/e/e4e67866-dffd-428c-aac7-8d28ddafb39b/mssql-tools_17.7.1.1-1_amd64.sig
RUN curl https://packages.microsoft.com/keys/microsoft.asc | gpg --import -
RUN gpg --verify msodbcsql17_17.7.2.1-1_amd64.sig msodbcsql17_17.7.2.1-1_amd64.apk
RUN gpg --verify mssql-tools_17.7.1.1-1_amd64.sig mssql-tools_17.7.1.1-1_amd64.apk
RUN apk add --allow-untrusted msodbcsql17_17.7.2.1-1_amd64.apk
RUN apk add --allow-untrusted mssql-tools_17.7.1.1-1_amd64.apk
This is based off what the Microsoft website said
In Laravel I have my database connection set to sqlsrv
When I shell into my app and run php artisan migrate:status I get the following error:
Illuminate\Database\QueryException
could not find driver (SQL: select * from sysobjects where type = 'U' and name = migrations)
Am I missing a config setting somewhere?
Thanks

This most probably because you only install Microsoft ODBC Driver for SQL Server, but did not install Microsoft ODBC driver for SQL Server for PHP:
Install prerequisites (Alpine)
Install the ODBC driver for Alpine by following the instructions on the Install the Microsoft ODBC driver for SQL Server (Linux). Make sure to also install the unixodbc-dev package (sudo apk add unixodbc-dev). It's used by the pecl command to install the PHP drivers.
Install the PHP drivers for Microsoft SQL Server (Alpine)
sudo pecl install sqlsrv
sudo pecl install pdo_sqlsrv
sudo su
echo extension=pdo_sqlsrv.so >> `php --ini | grep "Scan for additional .ini files" | sed -e "s|.*:\s*||"`/10_pdo_sqlsrv.ini
echo extension=sqlsrv.so >> `php --ini | grep "Scan for additional .ini files" | sed -e "s|.*:\s*||"`/00_sqlsrv.ini

Related

mssql-cli cannot connect to remote server

I am trying to connect to a remote SQL Server with mssql-cli, I use this command:
/usr/local/bin/mssql-cli -S server -U admin -d db -P ****
and I get this error:
Error message: A connection was successfully established with the server, but then an error occurred during the pre-login handshake. (provider: TCP Provider, error: 35 - An internal exception was caught)
However, I am able to connect using sqsh with no problem:
sqsh -S server -U admin -D db -P ****
Since I can connect with sqsh but not mssql-cli, and I am able to connect to a local instance of SQL Server, I'm not sure where to go from here.
I found the answer here: https://askubuntu.com/questions/1284658/how-to-fix-microsoft-odbc-driver-17-for-sql-server-ssl-provider-ssl-choose-cli
The answer is to update to the latest openssl:
wget https://www.openssl.org/source/latest.tar.gz -O openssl-1.1.1i.tar.gz
tar -zxvf openssl-1.1.1i.tar.gz
cd openssl-1.1.1i
./config
make
sudo make install
openssl version
sudo ldconfig
I also installed mssql-tools and unixodbc-dev - although I don't know if that had anything to do with it:
https://learn.microsoft.com/en-us/sql/linux/sql-server-linux-setup-tools?view=sql-server-ver15
These docs are for 16.04; so you have to change that to 20.04 if you're using mint 20 or ubuntu 20.04 like I am:
curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -
curl https://packages.microsoft.com/config/ubuntu/20.04/prod.list | sudo tee /etc/apt/sources.list.d/msprod.list
sudo apt install mssql-tools unixodbc-dev odbcinst1debian2 msodbcsql17 unixodbc libodbc1
It installs to /opt/mssql-tools/bin, so make sure add /opt/mssql-tools/bin/ to your PATH environment variable in a bash shell.

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

How to connect MSSQL in Laravel homestead?

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

How do I install/update to Postgres 9.4?

I just installeed Postgres, but it seems to have installed 9.3 and I'd like to start with 9.4
I simply did apt-get install postgresql from a new Ubuntu 14.04.1 machine.
http://www.postgresql.org/download/linux/ubuntu/
says you can do:
apt-get install postgresql-9.4
but when I try that I get:
E: Couldn't find any package by regex 'postgresql-9.4
Okay, so I try the section below where you add the PostgreSQL Apt Repository but that can't find anything either.
Is 9.4 not in the package managers yet? Am I doing something horribly wrong?
You can add it from the instructions in the page
http://www.postgresql.org/download/linux/ubuntu/
Create the file /etc/apt/sources.list.d/pgdg.list, and add a line for the repository
deb http://apt.postgresql.org/pub/repos/apt/ trusty-pgdg main
Import the repository signing key, and update the package lists
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
sudo apt-get update && sudo apt-get install postgresql-9.4
postgresql-9.4 is not available in 14.04 "Trusty". It was added in 14.10 "Utopic". It may be back ported in the future.
The directions on the PostgreSQL Ubuntu Download page are missing a command. Their wiki guide has the correct procedure. You must run apt-get update before trying to install. This will cause the system to read changes to the sources.
If you're trying to install on Ubuntu 14.04 "Trusty", you can follow these steps:
To check your version:
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 14.04.3 LTS
Release: 14.04
Codename: trusty
1) Create new apt repo file for postgres
$ echo "deb http://apt.postgresql.org/pub/repos/apt/ trusty-pgdg main" > /etc/apt/sources.list.d/pgdg.list
2) Import repository signing key and update packages list
$ sudo wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
$ sudo apt-get update
3) Install Postgres
$ sudo apt-get install postgresql-9.4
credit: http://ubuntuhandbook.org/index.php/2014/02/install-postgresql-ubuntu-14-04/
Below are steps to install PostgreSQL 9.4 on Ubuntu 14.04.
Reference taken from this Article:
First, check the version of Ubuntu:
lsb_release -sc
You need to add the latest PostgreSQL repository for the latest version, otherwise It will install PostgreSQL 9.3. This is for trusty version.
sudo add-apt-repository "deb https://apt.postgresql.org/pub/repos/apt/ trusty-pgdg main"
Update and Install PostgreSQL 9.4:
sudo apt-get update
sudo apt-get install postgresql-9.4
Default postgres super user and postgres database is created. You need to set a password for the postgres super user.
ubuntu#:~$ sudo passwd postgres
Enter new UNIX password:****
Retype new UNIX password:****
passwd: password updated successfully
If service is not started, you can start the PostgreSQL service.
sudo service postgresql start
Connect PostgreSQL server using postgres user:
ubuntu#:~$ su postgres
Password:****
Create a sample database:
createdb database_name
Connect to that database:
psql -d database_name
Follow these steps to install postgresql. Open the terminal (Ctrl + Alt + t) and then write down the following command line
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ `lsb_release -cs`-pgdg main" >> /etc/apt/sources.list.d/pgdg.list'
wget -q https://www.postgresql.org/media/keys/ACCC4CF8.asc -O - | sudo apt-key add -
sudo apt-get update
sudo apt-get install postgresql-9.6
If postgresql installed successfully then it will return this after writing this command
psql --version
psql (PostgreSQL) 9.6.3
PostgreSQL is an open source object-relational database system. It is one of leading database server used for production servers. PostgreSQL allows us to execute stored procedures in various programming languages, like PHP, C/C++, Python, Java, Perl, Ruby and its own PL/pgSQL, which is similar to Oracle’s PL/SQL.
Postgres database is used the persistent store of data
Install Postgres
yum install postgres
(Note : remember the password for the postgres user – you need it later)
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install postgresql-9.6
Setting up Postgres
Launch pgAdmin.
Connect to the local server. Use localhost for the server name, postgres for the username and the password you used when you installed Postgres.
You need to be root to perform this command.Note: If you did not set password during installation (sudo apt-get install postgresql), then you can set it as follows:

sudo -u postgres psql postgres
On the postgres client prompt, use the following command to set the password.
alter user postgres with password 'postgres';
Connect to PostgreSQL
After installing PostgreSQL database server, by default,, it creates a user ‘postgres’ with role ‘postgres’. It also creates a system account with same name ‘postgres’. So to connect to postgres server, log in to your system as user postgres and connect database.
$ sudo su - postgres
$ psql
Now you are logged in to PostgreSQL database server. To check login info use following command from database command prompt.
postgres-# \conninfo
To disconnect from PostgreSQL database command prompt just type below command and press enter. It will return you back to Ubuntu command prompt.
postgres-# \q

Resources