php Laravel 到 SQL Server (sqlsrv)。[PDOException] 找不到驱动程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39860273/
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-08-26 02:01:29  来源:igfitidea点击:

Laravel to SQL Server (sqlsrv). [PDOException] could not find driver

phpsql-serverlaravelpdoodbc

提问by Vahn Marty Cagalawan

Intro. My laravel app was using mysql, now it needs to be hosted in the network of the company I am working ( I am a remote-worker). And this company are Microsoft peeps, so I need to integrate the laravel to their SQL Server.

介绍。我的 Laravel 应用程序使用的是 mysql,现在它需要托管在我工作的公司的网络中(我是一名远程工作者)。而这家公司是微软的窥视者,所以我需要将 Laravel 集成到他们的 SQL Server 中。

I have this in my .env

我的 .env 中有这个

DB_CONNECTION=sqlsrv
DB_HOST=ip.address.of.server
DB_PORT=3306
DB_DATABASE=my_db
DB_USERNAME=my_username
DB_PASSWORD=my_password

After using the php artisan migrate

使用后 php artisan migrate

Error:

错误:

  [PDOException]
  could not find driver

I am using Ubuntu, a remote box dedicated for me (from my employer). I have tried using sql server in my laravel app before (using my Windows PC). As far as I remember, I edited some texts in the xampp php.ini. As a newbie Linux user, it is too hard for me (since i was using only CLI).

我正在使用 Ubuntu,这是一个专为我设计的远程盒子(来自我的雇主)。我之前曾尝试在我的 Laravel 应用程序中使用 sql server(使用我的 Windows PC)。据我所知,我在 xampp php.ini 中编辑了一些文本。作为 Linux 新手,这对我来说太难了(因为我只使用 CLI)。

EDITED ( new version )

已编辑(新版本)

So I already got the connectivity from Ubuntu to the Database server. I used the the sqlcmd -S <host> -U <username>

所以我已经获得了从 Ubuntu 到数据库服务器的连接。我使用了sqlcmd -S <host> -U <username>

and I tested the queries (such as SELECT * from users_data) and it works.

我测试了查询(例如SELECT * from users_data)并且它有效。

Now, I modified the config/database.phpand I added this.

现在,我修改了config/database.php并添加了这个。

'sqlsrv' => [
                'driver'   => 'MSSQL',
                'host'     => env('DB_HOST', 'host.of.the.database'),
                'database' => env('DB_DATABASE', 'my_database'),
                'username' => env('DB_USERNAME', 'my_username'),
                'password' => env('DB_PASSWORD', 'my_pass'),
          'port'     => '1433',
                'prefix'   => '',
            ],

but I got an Error:

但我有一个错误:

[InvalidArgumentException]
  Unsupported driver [MSSQL]

"MSSQL" is the name I use to configure the FreeTDS.

“MSSQL”是我用来配置 FreeTDS 的名称。

回答by Ad NAN

For those who came after

对于后来者

Make sure of the PHP version you use (for me homestead currently using php 7.1, so I installed php7.1-sybase)

确保您使用的 PHP 版本(对于我目前使用 php 7.1 的宅基地,所以我安装了 php7.1-sybase)

sudo apt-get install freetds-common freetds-bin unixodbc php7.1-sybase

And the driver is

而司机是

'sqlsrv' => [
            'driver' => 'sqlsrv',
            'host' => env('DB_HOST', 'localhost'),
            'database' => env('DB_DATABASE', 'forge'),
            'port' => env('DB_PORT', '1433'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            'prefix' => '',
        ]

You can make sure that the connection information is correct using tsql

可以使用tsql确保连接信息正确

TDSVER=8.0 tsql -H Host -U Username -D DatabaseName -p 1433 -P Password

回答by zapdev

Also had the could not find drivererror, resolved the issue after installing following packages:

也有could not find driver错误,安装以下软件包后解决了问题:

sudo apt-get install freetds-common freetds-bin unixodbc php5-sybase

However i am using the sqlsrvdriver, here's my config/database.php:

但是我正在使用sqlsrv驱动程序,这是我的config/database.php

'sqlsrv' => [
        'driver'   => 'sqlsrv',
        'host'     => env('DB_HOST', 'localhost'),
        'database' => env('DB_DATABASE', 'forge'),
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'charset'  => 'utf8',
        'prefix'   => '',
    ],

回答by dardawk

When installing sybase, ensure that it matches up with the version that your VM uses.

安装 sybase 时,请确保它与您的 VM 使用的版本匹配。

Run

php --version

and then install the right sybase version:

然后安装正确的 sybase 版本:

sudo apt-get install freetds-common freetds-bin unixodbc php7.#-sybase

If you're getting encoding errors, you'll need to update your freedts and php.ini configuration as well. Change the /etc/freetds/freetds.conf so that it looks like:

如果您遇到编码错误,您还需要更新 freedts 和 php.ini 配置。更改 /etc/freetds/freetds.conf 使其看起来像:

[global]
        # TDS protocol version
        tds version = 8.0

        # Whether to write a TDSDUMP file for diagnostic purposes
        # (setting this to /tmp is insecure on a multi-user system)
;       dump file = /tmp/freetds.log
;       debug flags = 0xffff

        # Command and connection timeouts
;       timeout = 10
;       connect timeout = 10

        # If you get out-of-memory errors, it may mean that your client
        # is trying to allocate a huge buffer for a TEXT field.
        # Try setting 'text size' to a more reasonable limit
        text size = 64512
        client charset =  UTF-8

# A typical Sybase server
[egServer50]
        host = symachine.domain.com
        port = 5000
        tds version = 5.0

# A typical Microsoft server
[egServer70]
        host = ntmachine.domain.com
        port = 1433
        tds version = 7.0

[mssql]
        host =
        Port = 1433
        tds version = 8.0

Add this to your php.ini:

将此添加到您的 php.ini 中:

client charset = UTF-8

回答by Erich Meissner

Always run sudo apt-get upgradebefore trying to run these commands.

sudo apt-get upgrade在尝试运行这些命令之前始终运行。

Thanks, these answers helped great. Please let me explain what I did using laravel/homestead vagrant box version 7.1.0 and PHP 7.3.

谢谢,这些答案帮助很大。请让我解释一下我使用 laravel/homestead vagrant box 版本 7.1.0 和 PHP 7.3 做了什么。

I ran the command sudo apt-get install freetds-common freetds-bin unixodbc php7.3-sybasein my vagrant homestead virtual box.

sudo apt-get install freetds-common freetds-bin unixodbc php7.3-sybase在我的 vagrant homestead 虚拟框中运行了命令。

This command prompted me twiceto either keep the local php.ini configuration file or use the "installer's version". I chose to use the installer's version both times, and everything worked great. Here is my database.php file in my laravel config folder. Also, I set default => env('DB_CONNECTION', 'sqlsrv')

此命令两次提示我保留本地 php.ini 配置文件或使用“安装程序版本”。我两次都选择使用安装程序的版本,一切都很好。这是我的 laravel 配置文件夹中的 database.php 文件。另外,我设置default => env('DB_CONNECTION', 'sqlsrv')