Laravel MS SQL登录超时已过期

时间:2019-02-22 12:29:02

标签: php sql-server laravel

我正在使用nginx和php-fpm运行Ubuntu 18.04

在laravel中,我尝试连接到外部MS SQL Server。

我的.env数据库设置:

DB_CONNECTION=sqlsrv
DB_HOST=127.20.2.10
DB_PORT=1433
DB_DATABASE=DATA
DB_USERNAME=saWeb
DB_PASSWORD='PASSWORD'

我的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出现错误:

  

SQLSTATE [HYT00]:[unixODBC] [Microsoft] [用于SQL的ODBC驱动程序17   服务器]登录超时已过期

我重新安装了SQL驱动程序(pdo_sqlsrv和sqlsrv)。

我制作了一个文件: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";
    ?>

输出为:连接成功

另一种尝试:

<?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/>";
    }
}
?>

输出: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)

我可以在没有Laravel的情况下连接SQL Server,我的Laravel配置有什么问题?

1 个答案:

答案 0 :(得分:1)

您的测试中有2个ip,分别是172.20.2.10和127.20.2.10。。可能是拼写错误,您必须在.env和config / database.php中设置的是172.20.2.10