2

I'm trying to connect Laravel 5.6 to my SQLEXPRESS.

When I try to migrate tables from Laravel to SQL, I am getting this error:

SQLSTATE[08001]: [Microsoft][ODBC Driver 13 for SQL Server]TCP Provider: No connection could be made because the target machine actively refused it. (SQL: select * from sysobjects where type = 'U' and name = migrations)

I have installed the sqlsrv extionsion to php and tested it:

<?php
$serverName = "MYNAME\\SQLEXPRESS"; 

$connectionInfo = array( "Database"=>"Portal", "UID"=>"sa", "PWD"=>"mySecretPwd");
$conn = sqlsrv_connect( $serverName, $connectionInfo);

if( $conn ) {
     echo "Connection established.<br />";
}else{
     echo "Connection could not be established.<br />";
     die( print_r( sqlsrv_errors(), true));
}
?>

The connection is successfully established.

I also see the extension in the phpinfo();

But if switch to Laravel 5.6 with this settings:

my env:

DB_CONNECTION=sqlsrv
DB_HOST=MYNAME\\SQLEXPRESS
DB_PORT=1433
DB_DATABASE=Portal
DB_USERNAME=sa
DB_PASSWORD=mySecredPwd

my config/database:

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

 'connections' => [

        .....

        'sqlsrv' => [
            'driver' => 'sqlsrv',
            'host' => env('DB_HOST', 'MYNAME\\SQLEXPRESS'),
            'port' => env('DB_PORT', '1433'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            'prefix' => '',
        ],

    ],

I'm getting an error. My serverconfigurations manager looks like this:

enter image description here

I can also connect with SQL Server Management Studio 2017.

Zhorov
  • 28,486
  • 6
  • 27
  • 52
Greg Ostry
  • 1,161
  • 5
  • 27
  • 52
  • I assume it's a typo, but you have the DB_DATABASE parameter set to `forge` when it should be `Portal`. – Jacob H May 31 '18 at 13:05
  • @JacobH: That's just the default value if no value exists in `.env`. – Jonas Staudenmeir May 31 '18 at 13:11
  • Well that seems completely backwards, to provide a default value as a parameter to overwrite settings stored elsewhere. Anyway, maybe try without the instance? – Jacob H May 31 '18 at 13:19
  • Can you establish a connection with $serverName = "MYNAME\\SQLEXPRESS,1433" with your working PHP script? – Zhorov May 31 '18 at 13:19
  • @Zhorov. If i add the 1433 Port to the $serverName i get exactly the same error as in Laravel – Greg Ostry May 31 '18 at 19:53

1 Answers1

7

First, find port number of the default instance:

  • Check the port number. If you have MS SQL Server Management Studio, execute: xp_readerrorlog 0, 1, N'Server is listening on' in master database

  • With command prompt (there will be a lot of rows, but you must analyze): netstat -ano

Possible solutions:

  • Stop SQL Server Browser service and test again, always with port number. You can connect to the default instance of SQL Server with TCP/IP if it is running on port 1433
  • Set SQL Server not to use dynamic ports and set the port number of default instance.
  • Change config/database in Laravel to find a way to skip port number. I don't have experience with Laravel, but I think that you can try with 'port' => env('DB_PORT', null),

How to configure server to listen to specific port:

  • Open "Computer Management\Services\SQL Server Configuration Manager".
  • Expand "SQL Server Network Configuration\Protocols for [instance name]" and double click "TCP/IP". In the "TCP/IP Properties" dialog box, on the IP Addresses tab, go to IPAll. Leave "TCP Dynamci port" empty. Set "TCP Port" to desired port number (could be default 1433).
  • In the "SQL Server Services", double-click SQL Server Browser. In "Service" tab set "Start Mode" to "Manual".
  • In the "SQL Server Services", double-click SQL Server Browser. In "Log On" stop service.
  • In the "SQL Server Services", double-click SQL Server ([instance name]). In "Log On" restart service.
  • Set client connection string to "server\instance,port" format.

Good starting points are: SQL Server Browser Service and Configure a Server to Listen on a Specific TCP Port

I hope this can help you.

Zhorov
  • 28,486
  • 6
  • 27
  • 52
  • WOW ! thats helped me a lot. thank you. My port was wrong. I thought this is a default value 1433... my port is 61688. Do you know how can i change this to the default value ? – Greg Ostry Jun 01 '18 at 15:00
  • @GregOstry I've updated the answer. This is my approach of configuring SQL Server. – Zhorov Jun 10 '18 at 15:57
  • It's too much help me. I have to set tcp port 1433 manually. It was not settings as default at all. THANKS AGAIN. – bb14816 Oct 05 '18 at 16:15