另一台服务器上的PHP和SQL 2012 Express数据库 - 连接错误

As the title, i have problem with connection from Linux server to server with MSSQL 2012 Express.

Server with MSSQL:

  • Windows 8
  • MSSQL 2012 Express, turned on and running correctly;
  • IP on dyndns.org (ports on the router are correctly forwarded, 1433 is open and 'listening');
  • Windows firewall is disabled;
  • Router firewall is disabled;

Server with Linux:

  • PHP 5.2;

Script:

$server   = 'xxxxxxx.dyndns.org';
$username = 'xxxxxxx';
$password = 'xxxxxxx';
$database = 'xxxxx';

if (mssql_connect($server, $username, $password))
{
    echo "CONNECTION SUCCESS!";
}
else
{
    echo "CONNECTION FAIL";
}

Result:

Warning: mssql_connect() [function.mssql-connect]: Unable to connect to server: xxxxxxx.dyndns.org in (....) on line 8


Connection failed: A connection attempt failed because the connected party did 
not properly respond after a period of time, or established connection failed 
because connected host has failed to respond Connection failed: A connection 
attempt failed because the connected party did not properly respond after a 
period of time, or established connection failed because connected host has failed 
to respond xxx.xxx.xxx.xxx:1433

I had this exact same error just now. With me it was TCP Dynamic port being used, which was not allowed through the firewall. Since it's a dynamic port, it's hard to allow just that port.

You can check by restarting your SQL server instance, and then look in the Event Viewer what port SQL Server is listening on if you have the same issue.

Sql server listening on wrong port

Open the SQL Server Configuration Manager

Then open SQL Server Network Configuration > Protocols for SQLEXPRESS(or whatever db engine you're using) > TCP/IP > IP Adresses

Where to browse in SQL server configuration manager

Make sure that in your IP adresses your TCP Port is set to the correct port you wish to use(Not entirely sure this is needed, but doesn't hurt)

Now when you come to the last item, IPAll, set TCP Dynamic ports to empty. This will disable dynamic port selection, and fill in the TCP Port you wish to use under TCP Port

Set TCP Dynamic ports to empty

Open the windows advanced firewall and allow your selected port to accept connections, if there isn't already a rule for it.

You should add a rule under Inbound rules, name it SQL port or something. Under Action "Allow the connection" setting up firewall rule

On the tab protocols and ports select Protocol type: "TCP", Local port "specific ports" "1433"(or your port number), Remote port "All ports"

Defining ports

and on scope any IP Adresses for local, for remote you can either choose "Any" or add a list of ip's you wish to add

Where to allow connections from

Then go to your SQL Server Management Studio and hit right click on the server instant and select restart to restart the database server.

Restart SQL Server

You'll get two prompts, one from uac. Then one from sql server if you're really sure you wish to restart. The second one can vanish behind other windows and can take some time to appear. So after hitting restart, be patient and don't click on stuff except the uac message.