简体   繁体   English

TCP 提供程序连接被拒绝 - Laravel 6 和 SQL 服务器 (Microsoft) 和 Laragon

[英]TCP Provider connection refused - Laravel 6 and SQL Server (Microsoft) and Laragon

I'm currently on a fresh install of Laravel 6 and trying to connect it to my Microsoft SQL Server, and I am serving it locally using Laragon.我目前正在全新安装 Laravel 6 并尝试将其连接到我的 Microsoft SQL 服务器,并且我正在使用 Laragon 在本地为其提供服务。

I have downloaded the ODBC driver for SQL Server.我已经为 SQL 服务器下载了 ODBC 驱动程序。

My database.php file contains:我的 database.php 文件包含:

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

...

'sqlsrv' => [
        'driver' => 'sqlsrv',
        'url' => env('DATABASE_URL'),
        'odbc' => true, // I added this line
        'odbc_datasource_name' => '{SQL Server}', // I added this line
        'host' => env('DB_HOST', 'localhost'),
        'port' => env('DB_PORT', '1433'),
        'database' => env('DB_DATABASE', 'forge'),
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'charset' => 'utf8',
        'prefix' => '',
        'prefix_indexes' => true,
 ],

And my.env contains the valid login credentials. my.env 包含有效的登录凭据。 To test that I can connect to the DB, I did a check in welcome.blade.php:为了测试我是否可以连接到数据库,我检查了 welcome.blade.php:

@php
    if (DB::connection()->getDatabaseName())
    {
        echo "connected successfully to database ".DB::connection()->getDatabaseName();

    }
@endphp

Which echos onto my page as:这在我的页面上回显为:

connected successfully to database testDatabase成功连接数据库testDatabase

I have gone into Tinker to see if I can get a Collection of all Employees from my model, ie Carer.php:我已经进入 Tinker 看看是否可以从我的 model 中获取所有员工的集合,即 Carer.php:


namespace App;

use Illuminate\Database\Eloquent\Model;

class Carer extends Model
{
    protected $table = "dbo.carer";

    protected $primaryKey = 'CarerID';
}

    ...

$carer = App\Carer::all(); $carer = App\Carer::all();

Which returns:返回:

Illuminate/Database/QueryException with message 'could not find driver (SQL: select * from [dbo].[carer])'用消息“找不到驱动程序(SQL:select * 来自 [dbo].[carer])”照亮/数据库/QueryException

Does this mean my check for connecting to the database is faulty if I cannot find a driver when attempting to retrieve model data?如果在尝试检索 model 数据时找不到驱动程序,这是否意味着我对连接数据库的检查有误? Do I have to do something specific in Laragon to get it to work for SQL Server?我是否必须在 Laragon 中做一些特定的事情才能让它在 SQL 服务器上工作?

Thank you.谢谢你。

UPDATE: Following the instructions from here and here , I have managed to add the PDO drivers to my php.ini file and get it running with Laragon.更新:按照此处此处的说明,我已设法将 PDO 驱动程序添加到我的 php.ini 文件中,并使其与 Laragon 一起运行。 When I print out phpinfo() I can now see the sql servers.当我打印出phpinfo()时,我现在可以看到 sql 服务器。

The error I am now getting is:我现在得到的错误是:

Illuminate/Database/QueryException with message '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 with message 'SQLSTATE[08001]: [Microsoft][ODBC Driver 17 for SQL Server]TCP Provider: 无法建立连接,因为目标机器主动拒绝了它。 (SQL: select top 1 * from [dbo].[carer])' (SQL:select 前 1 * 来自 [dbo].[carer])'

Answering this in case anybody else gets stuck on the same point.回答这个问题以防其他人陷入同一点。

My Microsoft SQL Server didn't have connection over TCP/IP enabled.我的 Microsoft SQL 服务器没有启用 TCP/IP 连接。

You want to:你想要:

  1. Open SQL Server Configuration Manager打开 SQL 服务器配置管理器

  2. Under SQL Server Network Configuration, select Protocols for MSSQLSERVER在 SQL 服务器网络配置下,MSSQLSERVER 的 select 协议

  3. Right click on TCP/IP and click Enable右键单击 TCP/IP,然后单击启用

  4. Restart SQL Server (MSSQLSERVER)重启 SQL 服务器(MSSQLSERVER)

Source 资源

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

相关问题 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 SQL连接被Laravel拒绝 - SQL Connection refused with Laravel Laravel Microsoft Sql Server 连接错误 - Laravel Microsoft Sql Server connection error 连接被拒绝 [tcp://127.0.0.1:6379] Laravel Predis - Connection refused [tcp://127.0.0.1:6379] Laravel Predis 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 SQLSTATE[08001]:[Microsoft][ODBC Driver 17 for SQL Server]TCP 提供程序:错误代码 0x2746 - SQLSTATE[08001]: [Microsoft][ODBC Driver 17 for SQL Server]TCP Provider: Error code 0x2746 TCP套接字中的“连接被拒绝”? - 'Connection refused' in tcp socket? 带有Laravel 5的Microsoft SQL Server - Microsoft SQL Server with Laravel 5 连接被拒绝 docker、laravel 和 mysql 服务器 - Connection refused docker, laravel and mysql server SQL Server 连接到 Laravel - SQL Server connection to Laravel
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM