I've found myself in a Job where I have to work with a windows server (2012) - I've never had problems with establishing DB connection, but now I dont seem to find any right solution.
我发现自己在一个工作中,我必须使用Windows服务器(2012) - 我从来没有建立数据库连接的问题,但现在我似乎找不到任何正确的解决方案。
I'll show you my connecting php code:
我将告诉你我的连接PHP代码:
error_reporting(-1); ini_set('display_errors', 1); $DB = array ('dbname'=>"test" , 'user'=> '***' , 'passwort'=> '***', 'host'=>'somelocalnetwork ip 192.**'); $connect = "mysql:dbname=".$DB['dbname']."; host=".$DB['host']; try { $dbh = new PDO($connect,$DB['user'], $DB['passwort']); echo "Connection established.
"; $dbh = null; } catch (PDOException $e) { echo $e -> getMessage(); }
This is the result, that i get in my browser:
这是我进入浏览器的结果:
SQLSTATE[HY000] [2002] Es konnte keine Verbindung hergestellt werden, da der Zielcomputer die Verbindung verweigerte.
SQLSTATE [HY000] [2002] Es konnte keine Verbindung hergestellt werden,da der Zielcomputer die Verbindung verweigerte。
translated into english :
翻译成英文:
No connection could be made because the target machine actively refused it .
无法建立连接,因为目标计算机主动拒绝它。
NOTE: I downloaded mssql drivers and sqlsrv drivers and extracted them to the /ext/ direcoty , included them in the php ini file. But when checking the php_info() i dont see any mssql nor sqlsrv parts. I don't know if thats relevant
注意:我下载了mssql驱动程序和sqlsrv驱动程序并将它们解压缩到/ ext / direcoty,将它们包含在php ini文件中。但是当检查php_info()时,我没有看到任何mssql或sqlsrv部分。我不知道那是否相关
The Windows Server is set as WebServer and as normal Microsoft SQL Server
Windows Server设置为WebServer和普通Microsoft SQL Server
2 个解决方案
#1
2
To connect to sql sever using sqlsrv:
使用sqlsrv连接到sql服务器:
<?php
error_reporting(E_ALL);
$serverName = "SuperComputer-PC";
$connectionInfo = array('Database'=>'RiverDatabase', "UID"=>"username", "PWD"=>"password");
$conn = sqlsrv_connect($serverName, $connectionInfo);
if($conn) {
"Connection established.<br />";
}else {
"Connection could not be established.<br />";
die(print_r(sqlsrv_errors(), true));
}
?>
Download php SQL Server from: http://www.microsoft.com/en-za/download/details.aspx?id=20098
从http://www.microsoft.com/en-za/download/details.aspx?id=20098下载php SQL Server
and copy the relevant drivers to your php ext folder. add the extensions to the php.ini file, for example:
并将相关的驱动程序复制到您的php ext文件夹。将扩展添加到php.ini文件中,例如:
I am using 5.4, but this should work for 5.5, I am not sure if a driver exists for 5.6 yet, but if it does, that's great.
我使用5.4,但这应该适用于5.5,我不确定是否存在5.6的驱动程序,但如果确实存在,那就太好了。
#2
-1
it might be 1) Authorization of pc needed Or 2)port number(lesser chances).
它可能是1)需要pc的授权或2)端口号(较少的机会)。
<?php
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>
Or Try
try
{
if ($db = mysqli_connect($hostname_db, $username_db, $password_db))
{
//do something
}
else
{
throw new Exception('Unable to connect');
}
}
catch(Exception $e)
{
echo $e->getMessage();
}
For Connection Problem:
对于连接问题:
If this happens always, it literally means that the machine exists but that it has no services listening on the specified port, or there is a firewall stopping you.
如果这种情况总是发生,它实际上意味着机器存在,但它没有服务侦听指定的端口,或者有防火墙阻止你。
If it happens occasionally - you used the word "sometimes" - and retrying succeeds, it is likely because the server has a full 'backlog'.
如果它偶尔发生 - 你使用“有时”这个词 - 并重试成功,很可能是因为服务器有一个完整的'积压'。
When you are waiting to be accepted on a listening socket, you are placed in a backlog. This backlog is finite and quite short - values of 1, 2 or 3 are not unusual - and so the OS might be unable to queue your request for the 'accept' to consume.
当您等待在侦听套接字上接受时,您将被置于待办事项中。这个积压是有限的,非常短 - 值为1,2或3并不罕见 - 因此操作系统可能无法将您的'accept'请求排队。
The backlog is a parameter on the listen function - all languages and platforms have basically the same API in this regard, even the C# one. This parameter is often configurable if you control the server, and is likely read from some settings file or the registry. Investigate how to configure your server.
积压是listen函数的一个参数 - 所有语言和平台在这方面基本上都是相同的API,甚至是C#one。如果您控制服务器,此参数通常是可配置的,并且可能从某些设置文件或注册表中读取。研究如何配置服务器。
If you wrote the server, you might have heavy processing in the accept of your socket, and this can be better moved to a separate worker-thread so your accept is always ready to receive connections. There are various architecture choices you can explore that mitigate queuing up clients and processing them sequentially.
如果您编写了服务器,则可能需要在接受套接字时进行繁重的处理,这可以更好地移动到单独的工作线程中,以便您的接受始终可以接收连接。您可以探索各种体系结构选项,以减少排队客户端并按顺序处理它们。
Regardless of whether you can increase the server backlog, you do need retry logic in your client code to cope with this issue - as even with a long backlog the server might be receiving lots of other requests on that port at that time.
无论您是否可以增加服务器积压,您都需要在客户端代码中重试逻辑以应对此问题 - 即使长时间积压,服务器可能会在此时在该端口上接收大量其他请求。
There is a rare possibility where a NAT router would give this error should it's ports for mappings be exhausted. I think we can discard this possibility as too much of a long shot though, since the router has 64K simultaneous connections to the same destination address/port before exhaustion.
如果NAT路由器的映射端口用尽,则NAT路由器很少会出现此错误。我认为我们可以放弃这种可能性,因为路由器在耗尽之前有64K同时连接到相同的目标地址/端口。
#1
2
To connect to sql sever using sqlsrv:
使用sqlsrv连接到sql服务器:
<?php
error_reporting(E_ALL);
$serverName = "SuperComputer-PC";
$connectionInfo = array('Database'=>'RiverDatabase', "UID"=>"username", "PWD"=>"password");
$conn = sqlsrv_connect($serverName, $connectionInfo);
if($conn) {
"Connection established.<br />";
}else {
"Connection could not be established.<br />";
die(print_r(sqlsrv_errors(), true));
}
?>
Download php SQL Server from: http://www.microsoft.com/en-za/download/details.aspx?id=20098
从http://www.microsoft.com/en-za/download/details.aspx?id=20098下载php SQL Server
and copy the relevant drivers to your php ext folder. add the extensions to the php.ini file, for example:
并将相关的驱动程序复制到您的php ext文件夹。将扩展添加到php.ini文件中,例如:
I am using 5.4, but this should work for 5.5, I am not sure if a driver exists for 5.6 yet, but if it does, that's great.
我使用5.4,但这应该适用于5.5,我不确定是否存在5.6的驱动程序,但如果确实存在,那就太好了。
#2
-1
it might be 1) Authorization of pc needed Or 2)port number(lesser chances).
它可能是1)需要pc的授权或2)端口号(较少的机会)。
<?php
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>
Or Try
try
{
if ($db = mysqli_connect($hostname_db, $username_db, $password_db))
{
//do something
}
else
{
throw new Exception('Unable to connect');
}
}
catch(Exception $e)
{
echo $e->getMessage();
}
For Connection Problem:
对于连接问题:
If this happens always, it literally means that the machine exists but that it has no services listening on the specified port, or there is a firewall stopping you.
如果这种情况总是发生,它实际上意味着机器存在,但它没有服务侦听指定的端口,或者有防火墙阻止你。
If it happens occasionally - you used the word "sometimes" - and retrying succeeds, it is likely because the server has a full 'backlog'.
如果它偶尔发生 - 你使用“有时”这个词 - 并重试成功,很可能是因为服务器有一个完整的'积压'。
When you are waiting to be accepted on a listening socket, you are placed in a backlog. This backlog is finite and quite short - values of 1, 2 or 3 are not unusual - and so the OS might be unable to queue your request for the 'accept' to consume.
当您等待在侦听套接字上接受时,您将被置于待办事项中。这个积压是有限的,非常短 - 值为1,2或3并不罕见 - 因此操作系统可能无法将您的'accept'请求排队。
The backlog is a parameter on the listen function - all languages and platforms have basically the same API in this regard, even the C# one. This parameter is often configurable if you control the server, and is likely read from some settings file or the registry. Investigate how to configure your server.
积压是listen函数的一个参数 - 所有语言和平台在这方面基本上都是相同的API,甚至是C#one。如果您控制服务器,此参数通常是可配置的,并且可能从某些设置文件或注册表中读取。研究如何配置服务器。
If you wrote the server, you might have heavy processing in the accept of your socket, and this can be better moved to a separate worker-thread so your accept is always ready to receive connections. There are various architecture choices you can explore that mitigate queuing up clients and processing them sequentially.
如果您编写了服务器,则可能需要在接受套接字时进行繁重的处理,这可以更好地移动到单独的工作线程中,以便您的接受始终可以接收连接。您可以探索各种体系结构选项,以减少排队客户端并按顺序处理它们。
Regardless of whether you can increase the server backlog, you do need retry logic in your client code to cope with this issue - as even with a long backlog the server might be receiving lots of other requests on that port at that time.
无论您是否可以增加服务器积压,您都需要在客户端代码中重试逻辑以应对此问题 - 即使长时间积压,服务器可能会在此时在该端口上接收大量其他请求。
There is a rare possibility where a NAT router would give this error should it's ports for mappings be exhausted. I think we can discard this possibility as too much of a long shot though, since the router has 64K simultaneous connections to the same destination address/port before exhaustion.
如果NAT路由器的映射端口用尽,则NAT路由器很少会出现此错误。我认为我们可以放弃这种可能性,因为路由器在耗尽之前有64K同时连接到相同的目标地址/端口。