18

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.

  • 1
    This might give you some clues http://www.easysoft.com/products/data_access/odbc-sql-server-driver/getting-started.html – RiggsFolly Oct 04 '16 at 19:38
  • And this may even be a duplicate http://stackoverflow.com/questions/149395/what-are-some-ways-of-accessing-microsoft-sql-server-from-linux – RiggsFolly Oct 04 '16 at 19:39
  • I have been following this http://askubuntu.com/questions/578934/mssql-connection-from-ubuntu , but it uses MSSQL rather than sqlsrv. And I can't locate the /usr/local/etc/odbcinst.ini – Vahn Marty Cagalawan Oct 04 '16 at 19:48
  • 1
    For me, odbcinst.ini was in /etc/odbcinst.ini I don't know if you've found a solution for this but I'm having the same problem as you. I can connect fine to my database with the command line but php doesn't see the driver. – Marc Boisvert Nov 03 '16 at 07:27
  • 2
    Hi @VahnMartyCagalawan, I have this exact same problem. Have you been abled to solve it. – Fokwa Best Nov 29 '16 at 09:23
  • Hope this one helps: https://stackoverflow.com/questions/52261535/laravel-and-ms-sql-server-database-connection-is-throwing-the-error-3-3-query/52262110 –  Oct 10 '18 at 08:25
  • I solved a similar issue by installing the Microsoft's PHP driver (https://learn.microsoft.com/en-us/sql/connect/php/download-drivers-php-sql-server?view=sql-server-ver15) and configured the .env file correctly. Note that for MSSQL your port number should be 1433 not 3306. – endo64 Feb 11 '20 at 11:52

4 Answers4

28

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

Note: After that you need to re-run command php artisan serve.

AnasSafi
  • 5,353
  • 1
  • 35
  • 38
Ad NAN
  • 321
  • 4
  • 6
7

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'   => '',
    ],
zapdev
  • 161
  • 3
  • 10
3

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
dardawk
  • 456
  • 4
  • 8
1

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

Erich Meissner
  • 205
  • 2
  • 5