Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.7k views
in Technique[技术] by (71.8m points)

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

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.

I have this in my .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

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

EDITED ( new version )

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

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

Now, I modified the config/database.php and I added this.

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...