1

On a Linux/Ubuntu box, I'm trying to connect to an external SQL Server database, but I only get errors. I know that the hostname, db name and credentials are correct, and I can't change them.

$odbc="odbc:Driver={SQL Server};Server=$server;Database=$database;";
$db = new PDO($odbc, $user, $password);

This just gives me could not find driver. I have tried every tutorial I could find, and installed tons of packages, and restarted nginx over and over again. I don't know what to do next.

0

1 Answer 1

1

You need to install drivers for php: https://learn.microsoft.com/en-us/sql/connect/php/download-drivers-php-sql-server?view=sql-server-2017

Then update config/database.php to use the sqlserv connection and driver:

'default' => env('DB_CONNECTION', 'sqlsrv'),

'connections' => [

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

    //[...]
],

After you've done that, you can use php artisan tinker to confirm the driver is available:

>>> DB::availableDrivers()
=> [
     0 => "mysql",
     2 => "sqlite",
   ]

and test your connection:

>>> DB::connection()
=> Illuminate\Database\MySqlConnection {#161}

(Mine doesn't show sqlserv because I don't use that driver)

Sign up to request clarification or add additional context in comments.

7 Comments

Well no. It's Windows thing, and this is a Ubuntu server.
this should work, if you setup SQL Server on your local Ubuntu
The original url had links to linux drivers as well, but I've updated it with a new one specific to installing for Ubuntu. Either way you need to install a pdo driver in php in order to be able to connect to SQL Server
@TravisBritz I have tried that too. But then I just get this Name too long for LOGINREC field (severity 2) (SQL: SELECT * FROM User). It's because the DB name is 49 chars long, and I can't change that.
That sounds like it connected and you have a new error, which sounds like progress to me
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.