带有 mssql (sqlsrv) 的 Laravel 未连接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29443371/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:17:18  来源:igfitidea点击:

Laravel with mssql (sqlsrv) not connecting

phplaravellaravel-4mssql-jdbc

提问by Nagesh Acharya

I was working on laravel with mysql it was working fine. But I have another scenario now. I need to connect the laravel to ms sql database which is reside on windows server. I have used the following code to connect to the DB app/config/database.php

我正在使用 mysql 处理 laravel,它工作正常。但我现在有另一种情况。我需要将 Laravel 连接到驻留在 Windows 服务器上的 ms sql 数据库。我使用以下代码连接到 DB app/config/database.php

'default' => 'sqlsrv',

'default' => 'sqlsrv',

'sqlsrv' => array(
        'driver'   => 'sqlsrv',
        'host'     => 'IP ADDRESS',
        'database' => 'DB Name',
        'username' => 'Username',
        'password' => 'password',
        'prefix'   => '',
    ),

but its showing error saying that

但它的显示错误说

PDOException could not find driver

PDOException 找不到驱动程序

So please can any one help me to solve this ? Thank you

所以请有人能帮我解决这个问题吗?谢谢

回答by vasco_silva

Are you using homestead? I had that exact same problem, using MAMP. The driver was missing from my server, so I ended up switching to homestead and installing freetdsthere.

你在用宅基地吗?我有完全相同的问题,使用 MAMP。我的服务器上缺少驱动程序,所以我最终切换到 homestead 并在那里安装了freetds

回答by AmitChaudhary

For Linux homestead:First, ssh into your box vagrant ssh from the Homestead folder.

对于 Linux homestead:首先,从 Homestead 文件夹 ssh 进入你的盒子 vagrant ssh。

Command : vagrant ssh

命令:流浪 ssh

Install the Sybase package for enabling the support for PDO and Mssql. Command : sudo apt-get install php7.0-sybase

安装 Sybase 软件包以启用对 PDO 和 Mssql 的支持。命令:sudo apt-get install php7.0-sybase

then run php -m on ssh to make sure pdo_dblib is enable.

然后在 ssh 上运行 php -m 以确保启用 pdo_dblib。

For Windows:you need to manually install this extension. You can download the driver from [Microsoft's website][1].

对于 Windows:您需要手动安装此扩展。您可以从 [Microsoft 网站][1] 下载驱动程序。

After extracting the files, please copy them to your PHP's ext directory. To make PHP load the extension, just add the following line to your PHP.ini (this is for the non-thread safe version of PHP, which you are most likely using when you have installed PHP to use IIS FastCGI, which we recommend):

解压文件后,请将它们复制到 PHP 的 ext 目录中。要使 PHP 加载扩展,只需在 PHP.ini 中添加以下行(这是针对 PHP 的非线程安全版本,您在安装 PHP 以使用 IIS FastCGI 时最有可能使用的版本,我们推荐) :

extension=php_sqlsrv.dll

扩展名=php_sqlsrv.dll

And restart your Apache

并重新启动你的 Apache

回答by Jamie Poole

To extend @AmitChaudhary's answer:

扩展@AmitChaudhary 的回答:

For Mac OSX using MAMP

对于使用 MAMP 的 Mac OSX

  1. Download and install pre-requisites using Homebrew (simplest option):
    FreeTDS: brew install freetds
    autconf: brew install autoconf

  2. Download PHP version source code (I used 5.6.27): http://php.net/releases/

  3. Extract source, open Terminal, and go to folder in command-line

  4. (Optional) Temporarily add your MAMP's PHP installation to the Path:
    export PATH=/Applications/MAMP/bin/php/php5.6.27/bin:$PATH

  1. 使用 Homebrew 下载并安装先决条件(最简单的选项)
    brew install freetds
    FreeTDS:autconf:brew install autoconf

  2. 下载PHP版本源码(我用的是5.6.27):http://php.net/releases/

  3. 提取源代码,打开终端,然后在命令行中转到文件夹

  4. (可选)暂时将您的 MAMP 的 PHP 安装添加到路径:
    export PATH=/Applications/MAMP/bin/php/php5.6.27/bin:$PATH

Make sure you change the version number to whatever you're using.

确保将版本号更改为您正在使用的任何版本。

  1. Type which phpizeto confirm that it is using your MAMP installation's PHP

  2. Configure and Make downloaded PHP source:
    ./configure --without-iconv && make

  3. Go to the Extensions folder of the downloaded PHP source:
    cd ext

  4. Go to the MSSQL Extension folder:
    cd mssql

  5. Run phpize

  6. Configure Extension using FreeTDS:
    ./configure --with-mssql=/usr/local/Cellar/freetds/1.00.23/ && make

  1. 输入which phpize以确认它正在使用您的 MAMP 安装的 PHP

  2. 配置和制作下载的 PHP 源代码:
    ./configure --without-iconv && make

  3. 转到下载的 PHP 源代码的 Extensions 文件夹:
    cd ext

  4. 转到 MSSQL 扩展文件夹:
    cd mssql

  5. phpize

  6. 使用 FreeTDS 配置扩展:
    ./configure --with-mssql=/usr/local/Cellar/freetds/1.00.23/ && make

Confirm if this is your correct FreeTDS version, it will give you an error if it can't find it.

确认这是否是您正确的 FreeTDS 版本,如果找不到,它会给您一个错误。

  1. Go to the Modules folder:
    cd modules

  2. Copy the mssql.somodule to your MAMP's Extension folder (for me this was) at:
    cp mssql.so /Applications/MAMP/bin/php/php5.6.27/lib/php/extensions/no-debug-non-zts-20131226/

  3. Within MAMP, look for the Edit Templates menu option (depending what version of MAMP, it should be under the MAMP menu or available in MAMP Pro via a button)

  4. Find ; Extensionsand at the bottom of this section add:
    extension=mssql.so

  5. Restart your MAMP Apache server and you should be good to go!

  1. 转到模块文件夹:
    cd modules

  2. mssql.so模块复制到您的 MAMP 的扩展文件夹(对我来说是):
    cp mssql.so /Applications/MAMP/bin/php/php5.6.27/lib/php/extensions/no-debug-non-zts-20131226/

  3. 在 MAMP 中,查找“编辑模板”菜单选项(取决于 MAMP 的版本,它应该在 MAMP 菜单下或通过按钮在 MAMP Pro 中可用)

  4. 找到; Extensions并在本节底部添加:
    extension=mssql.so

  5. 重新启动您的 MAMP Apache 服务器,您应该一切顺利!

I literally just did this but struggled to find a decent walkthrough to job my memory so hopefully this helps someone else (or probably me) in the future.

我真的只是这样做了,但很难找到一个像样的演练来工作我的记忆,所以希望这在未来可以帮助其他人(或者可能是我)。

Cheers GB.

干杯 GB。