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

心已入冬 提交于 2019-12-01 02:36:10

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)

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

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

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

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

However i am using the sqlsrv driver, here's my 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'   => '',
    ],

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

Run

php --version

and then install the right sybase version:

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:

[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:

client charset = UTF-8

Always run sudo apt-get upgrade before trying to run these commands.

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.

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

This command prompted me twice to 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')

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!