简体   繁体   English

sqlsrv_connect 工作正常,但 laravel 失败并显示:TCP 提供程序:无法建立连接,因为目标计算机主动拒绝它

[英]sqlsrv_connect works fine but laravel fails with : TCP Provider: No connection could be made because the target machine actively refused it

unfortuantely i have to make laravel talk to sql server.. i normally use mysql.. and have developed a swift hatred for sql server.. unfortuantely i have to make laravel talk to sql server.. i normally use mysql.. and have developed a swift hatred for sql server..

i'm running xampp on localhost for development.我在本地主机上运行 xampp 进行开发。 I can connect and query to local sql server fine with navicat and with sql server management studio.我可以使用navicat和sql服务器管理工作室连接和查询本地sql服务器。

sqlsrv_connect works fine in php, and i can run queries manually with no problem. sqlsrv_connect 在 php 中工作正常,我可以毫无问题地手动运行查询。 mssql_connect works fine in php, and i can run queries manually with no problem. mssql_connect 在 php 中工作正常,我可以毫无问题地手动运行查询。

but laravel fails.但 laravel 失败。

in env:在环境中:

DB_CONNECTION=sqlsrv
DB_HOST=localhost
DB_DATABASE=handheld2
DB_USERNAME=handheld
DB_PASSWORD=thepasswordhere
DB_PORT=1433

and in laravel database config:在 laravel 数据库配置中:

'sqlsrv' => [
            'driver' => 'sqlsrv',
            'url' => env('DATABASE_URL', 'localhost'),
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', '1433'),
            'database' => env('DB_DATABASE', 'handheld2'),
            'username' => env('DB_USERNAME', ''),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            'prefix' => '',
            'prefix_indexes' => true,
]

i'm not clear what 'url' is for?我不清楚'url'是干什么用的? how is this different to db_host?这与 db_host 有何不同?

i receive我收到

Illuminate\Database\QueryException SQLSTATE[08001]: [Microsoft][ODBC Driver 17 for SQL Server]TCP Provider: No connection could be made because the target machine actively refused it. Illuminate\Database\QueryException SQLSTATE[08001]: [Microsoft][ODBC Driver 17 for SQL Server]TCP Provider: 无法建立连接,因为目标机器主动拒绝。

both php_sqlsrv_72_ts_x64.dll and php_pdo_sqlsrc_72_x64.dll are enabled in php php_sqlsrv_72_ts_x64.dll 和 php_pdo_sqlsrc_72_x64.dll 都在 ZE1BFD762321E409CEE4AC0B6E841963 中启用

am i missing something simple and obvious?我错过了一些简单而明显的东西吗?

edit: note that this code works编辑:请注意,此代码有效

$connectionInfo = array(
    "Database"=>"handheld2",
    "UID" => "handheld",
    "PWD" => "thepasswordhere",
);
$conn = sqlsrv_connect("localhost", $connectionInfo);
if ($conn === false) {
    echo "Could not connect.\n";
    die(print_r(sqlsrv_errors(), true));
}
$stmt = sqlsrv_query($conn, 'SELECT top 1 * from users where users.id = 1');
if($stmt === false) {
    die( print_r( sqlsrv_errors(), true) );
}

while( $row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC) ) {
    print_r($row);
}

and prints out record from db.. so why does it work here but not though laravel???并从 db 打印出记录.. 那么为什么它在这里工作但不是 laravel 呢?

debugging in laravels files in vendor.. turns out it was trying to connect to 'andheld2' instead of 'handheld2'在供应商的 laravel 文件中调试.. 结果是它试图连接到“andheld2”而不是“handheld2”

changing改变

'url' => env('DATABASE_URL', 'handheld2'), 'url' => env('DATABASE_URL', 'handheld2'),

to

'url' => env('DATABASE_URL', '/handheld2'), 'url' => env('DATABASE_URL', '/handheld2'),

in the database config makes it work.. it apparently ignores the first character.. for some reason?在数据库配置中使它工作..它显然忽略了第一个字符..出于某种原因? whatever.任何。

If anyone else finds themselves here maybe this will help.如果其他人发现自己在这里,也许这会有所帮助。

After hours of banging my head against the wall, the fix for me in the end was extremely simple.在把我的头撞在墙上几个小时之后,最后对我的修复非常简单。 I was working with a fresh install of everything (including Sql Server) and I had to open Sql Server Configuration Manager ( which itself is a pain in the butt in Windows 10 ...I ended up having to just search in the windows folder for SQLServerManager because I didn't know the exact version number, which was 15). I was working with a fresh install of everything (including Sql Server) and I had to open Sql Server Configuration Manager ( which itself is a pain in the butt in Windows 10 ...I ended up having to just search in the windows folder for SQLServerManager 因为我不知道确切的版本号,即 15)。

Then > SQL Server Network Configuation > Protocols for MSSQLSERVER > TCP/IP was disabled.然后 > SQL 服务器网络配置 > MSSQLSERVER 的协议 > TCP/IP 被禁用。 Enabled this, restart SQLSERVER in Windows Services and viola, laravel works.启用此功能,在 Windows 服务和 viola 中重新启动 SQLSERVER,laravel 工作。

It's an old question but we had the same problem with the sqlsrv php driver.这是一个老问题,但我们在使用 sqlsrv php 驱动程序时遇到了同样的问题。 This was caused by the dynamic ports of the sql server - switching to specific port solved the problem for us.这是由 sql 服务器的动态端口引起的 - 切换到特定端口为我们解决了问题。

https://docs.microsoft.com/en-us/sql/database-engine/configure-windows/configure-a-server-to-listen-on-a-specific-tcp-port https://docs.microsoft.com/en-us/sql/database-engine/configure-windows/configure-a-server-to-listen-on-a-specific-tcp-port

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PHP Laravel:无法建立连接,因为目标机器主动拒绝它 - PHP Laravel: No connection could be made because the target machine actively refused it 无法建立连接,因为目标计算机主动拒绝了它Laravel 5 - No connection could be made because the target machine actively refused it Laravel 5 无法建立连接,因为目标计算机主动拒绝了它的LARAVEL 4错误 - No connection could be made because the target machine actively refused it LARAVEL 4 error SQLSTATE[08001]:[Microsoft][ODBC Driver 13 for SQL Server]TCP Provider:无法建立连接,因为目标机器主动拒绝它 - SQLSTATE[08001]: [Microsoft][ODBC Driver 13 for SQL Server]TCP Provider: No connection could be made because the target machine actively refused it zend SMTP无法建立连接,因为目标计算机被主动拒绝 - zend SMTP No connection could be made because the target machine actively refused 由于目标机器主动拒绝连接,因此无法建立连接 - No connection could be made because the target machine actively refused it Mysql 无法连接,因为目标机器主动拒绝 - Mysql No connection could be made because the target machine actively refused it PHP-无法建立连接,因为目标计算机主动拒绝了它 - PHP- No connection could be made because the target machine actively refused it 无法建立连接,因为目标机器主动拒绝它。 WordPress的 - No connection could be made because the target machine actively refused it. wordpress 警告:mysql_connect():由于目标计算机主动拒绝连接,因此无法建立连接 - Warning: mysql_connect(): No connection could be made because the target machine actively refused it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM