I'm running Ubuntu 18.04 with nginx & php-fpm
In laravel I try to connect to a external MS SQL Server.
My .env database settings:
DB_CONNECTION=sqlsrv
DB_HOST=127.20.2.10
DB_PORT=1433
DB_DATABASE=DATA
DB_USERNAME=saWeb
DB_PASSWORD='PASSWORD'
My config/database.php:
'default' => env('DB_CONNECTION', 'sqlsrv'),
'sqlsrv' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST', '127.20.2.10'),
'port' => env('DB_PORT', '1433'),
'database' => env('DB_DATABASE', 'DATA'),
'username' => env('DB_USERNAME', 'saWeb'),
'password' => env('DB_PASSWORD', 'PASSWORD'),
'charset' => 'utf8',
'prefix' => '',
'prefix_indexes' => true,
],
Laravel got the error:
SQLSTATE[HYT00]: [unixODBC][Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired
I reinstalled the sql drivers (pdo_sqlsrv & sqlsrv).
I made a file: sqltest.php:
<?php
$host = "172.20.2.10";
$user = "saWeb";
$password = "PASSWORD";
$dbname="DATA";
$options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_CASE => PDO::CASE_NATURAL,
PDO::ATTR_ORACLE_NULLS => PDO::NULL_EMPTY_STRING
];
try {
$connection = new PDO("sqlsrv:Server=$host,1433; Database=$dbname", $user, $password);
} catch(PDOException $e) {
die("Database connection failed: " . $e->getMessage());
exit;
}
echo"Connection Successful";
?>
The output is: Connection Successful
Another try:
<?php
$serverName = "172.20.2.10";
$connectionOptions = array(
"Database" => "DATA",
"Uid" => "saWeb",
"PWD" => "PASSWORD"
);
$conn = sqlsrv_connect($serverName, $connectionOptions);
$tsql= "SELECT @@Version as SQL_VERSION;";
$getResults= sqlsrv_query($conn, $tsql);
if ($getResults == FALSE)
die(FormatErrors(sqlsrv_errors()));
?>
<h1> Results : </h1>
<?php
while ($row = sqlsrv_fetch_array($getResults, SQLSRV_FETCH_ASSOC)) {
echo ($row['SQL_VERSION']);
echo ("<br/>");
}
sqlsrv_free_stmt($getResults);
function FormatErrors( $errors )
{
/* Display errors. */
echo "Error information: <br/>";
foreach ( $errors as $error )
{
echo "SQLSTATE: ".$error['SQLSTATE']."<br/>";
echo "Code: ".$error['code']."<br/>";
echo "Message: ".$error['message']."<br/>";
}
}
?>
The output: Microsoft SQL Server 2014 (SP1-GDR) (KB4019091) - 12.0.4237.0 (X64) Jul 5 2017 22:03:42 Copyright (c) Microsoft Corporation Standard Edition (64-bit) on Windows NT 6.3 (Build 9600: ) (Hypervisor)
I can connect with the SQL Server without Laravel, what's wrong with my Laravel configuration?
You have 2 ips in your tests tests 172.20.2.10 and 127.20.2.10.. Probably a typo and the one you have to set in your .env and config/database.php is 172.20.2.10
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With