简体   繁体   English

如何在 Laravel Homestead 中连接 MSSQL?

[英]How to connect MSSQL in Laravel homestead?

I am trying to connect with a MSSQL database using laravel homestead.我正在尝试使用 laravel homestead 连接 MSSQL 数据库。 It always throws the exception:它总是抛出异常:

PDOException in Connector.php line 55: could not find driver. Connector.php 第 55 行中的 PDOException:找不到驱动程序。

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.我看到很多人在谈论 FreeTDS 和 Sybase 驱动程序,但我似乎无法弄清楚我需要什么才能从运行 Laravel Homestead Vagrant box 的 Linux Web 服务器启用 laravel/php 访问 MSSQL 数据库。

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 : Laravel vagrant 使用的是 php 7。对于 mssql 支持,我们必须为 linux 启用相关的 sybase 驱动程序:

To Enable MSSQL Support for PHP 7:为 PHP 7 启用 MSSQL 支持:

First, ssh into your box vagrant ssh from the Homestead folder.首先,从 Homestead 文件夹 ssh 进入你的盒子 vagrant ssh。

Command : vagrant ssh命令: vagrant ssh

Install the Sybase package for enabling the support for PDO and Mssql.安装 Sybase 软件包以启用对 PDO 和 Mssql 的支持。

Command : sudo apt-get install php7.0-sybase命令: sudo apt-get install php7.0-sybase

then run php -m on the ssh to make sure pdo_dblib is enabled.然后在 ssh 上运行php -m以确保启用了pdo_dblib

All Done!!!!大功告成!!!! Cheers干杯

Laravel uses PDO extension to connect to database, you could check if the extension is enabled using Laravel 使用 PDO 扩展连接到数据库,你可以检查扩展是否启用使用

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:至今仍适用于:

  1. php7.2-sybase php7.2-sybase

  2. using DB_HOST= IP_ADDRESS\\NAMED_INSTANCE使用 DB_HOST= IP_ADDRESS\\NAMED_INSTANCE

  3. Commenting out the PORT directive in both .env and database.php在 .env 和 database.php 中注释掉 PORT 指令

The following works for php72.以下适用于 php72。 You may try changing the version of php in the commands.您可以尝试在命令中更改 php 的版本。

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://docs.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-ver15#ubuntu17 https://docs.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-ver15#ubuntu17

  2. https://docs.microsoft.com/en-us/sql/connect/php/installation-tutorial-linux-mac?view=sql-server-ver15#installing-the-drivers-on-ubuntu-1604-1804-and-2004 https://docs.microsoft.com/en-us/sql/connect/php/installation-tutorial-linux-mac?view=sql-server-ver15#installing-the-drivers-on-ubuntu-1604-1804-和-2004

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM