AmitChaudhary
AmitChaudhary

Reputation: 161

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.

Upvotes: 4

Views: 7854

Answers (4)

Muhammad Abu ul Fazal
Muhammad Abu ul Fazal

Reputation: 139

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:

  1. https://learn.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-ver15#ubuntu17

  2. 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

Upvotes: 1

Levy
Levy

Reputation: 36

Thanks for this. Still works as of now with:

  1. php7.2-sybase

  2. using DB_HOST= IP_ADDRESS\NAMED_INSTANCE

  3. Commenting out the PORT directive in both .env and database.php

Upvotes: 0

AmitChaudhary
AmitChaudhary

Reputation: 161

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

Upvotes: 13

ankittiwaari
ankittiwaari

Reputation: 106

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.

Upvotes: 0

Related Questions