I'm connecting SQL Database to a PHP website (Both are on Azure),
Everything in PHP seems to be fine, and Connection String is also Perfect.
When I try to connect to a database it connects and does all the operations, but when I try to connect to another database it show the following error.
Array (
[0] => Array (
[0] => 08001
[SQLSTATE] => 08001
[1] => 5
[code] => 5
[2] => [Microsoft][ODBC Driver 11 for SQL Server]Named Pipes Provider: Could not open a connection to SQL Server [5].
[message] => [Microsoft][ODBC Driver 11 for SQL Server]Named Pipes Provider: Could not open a connection to SQL Server [5].
)
[1] => Array (
[0] => HYT00
[SQLSTATE] => HYT00
[1] => 0
[code] => 0
[2] => [Microsoft][ODBC Driver 11 for SQL Server]Login timeout expired
[message] => [Microsoft][ODBC Driver 11 for SQL Server]Login timeout expired
)
[2] => Array (
[0] => 08001
[SQLSTATE] => 08001
[1] => 5
[code] => 5
[2] => [Microsoft][ODBC Driver 11 for SQL Server]A network-related or
instance-specific error has occurred while establishing a
connection to SQL Server. Server is not found or not accessible.
Check if instance name is correct and if SQL Server is configured
to allow remote connections. For more information see SQL Server
Books Online.
[message] => [Microsoft][ODBC Driver 11 for SQL Server]A network-related
or instance-specific error has occurred while establishing a
connection to SQL Server. Server is not found or not accessible.
Check if instance name is correct and if SQL Server is configured to
allow remote connections. For more information see SQL Server Books
Online.
)
)
And also,
I'm seeing this Lock sign on the database which is not getting connected.
This Lock doesn't appear on the database, which connects successfully.
And also All three (Two Databases and a Web App) are on Same Resource Group.
I'm connecting both database using Connection String stored on website's Application Settings.
Actually, this is TDE (Transparent Data Encryption) icon rather than a Lock sign. See Transparent Data Encryption with Azure SQL Database.
And according to the error message above, the most likely cause of this issue is that the Server name you are using is incorrect. Here are some proposals for you to troubleshoot this issue:
Make sure the hostname from Connection String exactly equals to the Server name in the Azure portal.
Use the sample code for PHP to test whether the connection could be established or not. Do not forget to replace with your own password.
Related
I've done so much research trying to get my PHP code hosted on IIS to connect to my MSSQL database. I just can't seem to figure out the issue. Has anyone come across this before?
<?php
$serverName = 'AEGIS-PC\SQLEXPRESS';
$connectionInfo=array('Database'=>'tttb_db');
$con = sqlsrv_connect($serverName, $connectionInfo);
if($con){
echo 'Connection established<br />';
}
else {
echo 'Connection failed<br />';
die(print_r(sqlsrv_errors(), TRUE));
}
?>
Update, we have a new error:
Connection failed
Array ( [0] => Array ( [0] => 28000 [SQLSTATE] => 28000 [1] => 18456 [code] => 18456 [2] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Login failed for user 'NT AUTHORITY\IUSR'. [message] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Login failed for user 'NT AUTHORITY\IUSR'. ) [1] => Array ( [0] => 42000 [SQLSTATE] => 42000 [1] => 4060 [code] => 4060 [2] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Cannot open database "tttb_db" requested by the login. The login failed. [message] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Cannot open database "tttb_db" requested by the login. The login failed. ) [2] => Array ( [0] => 28000 [SQLSTATE] => 28000 [1] => 18456 [code] => 18456 [2] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Login failed for user 'NT AUTHORITY\IUSR'. [message] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Login failed for user 'NT AUTHORITY\IUSR'. ) [3] => Array ( [0] => 42000 [SQLSTATE] => 42000 [1] => 4060 [code] => 4060 [2] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Cannot open database "tttb_db" requested by the login. The login failed. [message] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Cannot open database "tttb_db" requested by the login. The login failed. ) )
Our SQL server is using Windows Authentication and our server name is AEGIS-PC\SQLEXPRESS with the user name and password blocks are greyed out. I can not think of reason our login would be failing. What are we doing wrong?
There are a few things that could cause such problems:
1.) Your modules aren't loaded because its VC9 instead if VC11. Check which compiler version your system use and install the correct driver.
2.) Check your PHP Version and use the correct driver for your PHP-Version your can check that in your phpinfo().
3.) Don't forget to install the MSSQL Native Client otherwise you can't connect to your database that is the problem what I have every time.
Your code looks good and if your get the error message that sqlsrv_connect isn't found that is a signal that the module is not loaded.
https://www.microsoft.com/en-us/download/details.aspx?id=20098
It's just a simple solution I found to mine. checkout http://blog.sqlauthority.com/2009/08/20/sql-server-fix-error-cannot-open-database-requested-by-the-login-the-login-failed-login-failed-for-user-nt-authoritynetwork-service/
it's just a few steps.
Go to SQL Server >> Security >> Logins and right click on NT AUTHORITY\NETWORK SERVICE and select Properties
In newly opened screen of Login Properties, go to the “User Mapping” tab. Then, on the “User Mapping” tab, select the desired database – especially the database for which this error message is displayed. On the lower screen, check the role db_owner. Click OK.
this should fix your problem
You need to add a service account for the 'NT AUTHORITY\IUSR' system account to let IIS access the database.
This is the key part of the error string:
[Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Login failed for user 'NT AUTHORITY\IUSR'
When someone hits the website and the php script tries to access the SQL Server, it does it using IIS's system account, not the user on the website's account. You can have IIS use windows authentication for the DB request. See below for enabling Windows Authentication with IIS:
https://technet.microsoft.com/en-us/library/cc754628%28v=ws.10%29.aspx
Fair warning, it only works if the user hitting the web page is in the same AD domain as the web server.
Be careful what privileges you assign to the IIS system account, if you go that route at all.
1- Go to SQL Server >> Security >> Logins and right click on NT AUTHORITY\NETWORK SERVICE and select Properties
2- In newly opened screen of Login Properties, go to the “User Mapping” tab. Then, on the “User Mapping” tab, select the desired database – especially the database for which this error message is displayed. On the lower screen, check the role db_owner. Click OK.
I've done so much research trying to get my PHP code hosted on IIS to connect to my MSSQL database. I just can't seem to figure out the issue. Has anyone come across this before?
<?php
$serverName = 'AEGIS-PC\SQLEXPRESS';
$connectionInfo=array('Database'=>'tttb_db');
$con = sqlsrv_connect($serverName, $connectionInfo);
if($con){
echo 'Connection established<br />';
}
else {
echo 'Connection failed<br />';
die(print_r(sqlsrv_errors(), TRUE));
}
?>
Update, we have a new error:
Connection failed
Array ( [0] => Array ( [0] => 28000 [SQLSTATE] => 28000 [1] => 18456 [code] => 18456 [2] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Login failed for user 'NT AUTHORITY\IUSR'. [message] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Login failed for user 'NT AUTHORITY\IUSR'. ) [1] => Array ( [0] => 42000 [SQLSTATE] => 42000 [1] => 4060 [code] => 4060 [2] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Cannot open database "tttb_db" requested by the login. The login failed. [message] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Cannot open database "tttb_db" requested by the login. The login failed. ) [2] => Array ( [0] => 28000 [SQLSTATE] => 28000 [1] => 18456 [code] => 18456 [2] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Login failed for user 'NT AUTHORITY\IUSR'. [message] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Login failed for user 'NT AUTHORITY\IUSR'. ) [3] => Array ( [0] => 42000 [SQLSTATE] => 42000 [1] => 4060 [code] => 4060 [2] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Cannot open database "tttb_db" requested by the login. The login failed. [message] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Cannot open database "tttb_db" requested by the login. The login failed. ) )
Our SQL server is using Windows Authentication and our server name is AEGIS-PC\SQLEXPRESS with the user name and password blocks are greyed out. I can not think of reason our login would be failing. What are we doing wrong?
There are a few things that could cause such problems:
1.) Your modules aren't loaded because its VC9 instead if VC11. Check which compiler version your system use and install the correct driver.
2.) Check your PHP Version and use the correct driver for your PHP-Version your can check that in your phpinfo().
3.) Don't forget to install the MSSQL Native Client otherwise you can't connect to your database that is the problem what I have every time.
Your code looks good and if your get the error message that sqlsrv_connect isn't found that is a signal that the module is not loaded.
https://www.microsoft.com/en-us/download/details.aspx?id=20098
It's just a simple solution I found to mine. checkout http://blog.sqlauthority.com/2009/08/20/sql-server-fix-error-cannot-open-database-requested-by-the-login-the-login-failed-login-failed-for-user-nt-authoritynetwork-service/
it's just a few steps.
Go to SQL Server >> Security >> Logins and right click on NT AUTHORITY\NETWORK SERVICE and select Properties
In newly opened screen of Login Properties, go to the “User Mapping” tab. Then, on the “User Mapping” tab, select the desired database – especially the database for which this error message is displayed. On the lower screen, check the role db_owner. Click OK.
this should fix your problem
You need to add a service account for the 'NT AUTHORITY\IUSR' system account to let IIS access the database.
This is the key part of the error string:
[Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Login failed for user 'NT AUTHORITY\IUSR'
When someone hits the website and the php script tries to access the SQL Server, it does it using IIS's system account, not the user on the website's account. You can have IIS use windows authentication for the DB request. See below for enabling Windows Authentication with IIS:
https://technet.microsoft.com/en-us/library/cc754628%28v=ws.10%29.aspx
Fair warning, it only works if the user hitting the web page is in the same AD domain as the web server.
Be careful what privileges you assign to the IIS system account, if you go that route at all.
1- Go to SQL Server >> Security >> Logins and right click on NT AUTHORITY\NETWORK SERVICE and select Properties
2- In newly opened screen of Login Properties, go to the “User Mapping” tab. Then, on the “User Mapping” tab, select the desired database – especially the database for which this error message is displayed. On the lower screen, check the role db_owner. Click OK.
I've searched through the Internet with respect to this error and I've not found anyone who is having this problem on an intermittent basis. It seems my application will run for an extended period of time with no problem, then all of a sudden it stops working and I get this error. Then after some time(I have no idea how long this is) the application will start working again. But lately to correct the problem I just restart the IIS server. Could there be a problem with the application pool? There are no errors in any of my event logs.
My server configurations are as follows Windows 2008 64-bit running PHP 5.4. I'm connecting to a SQL Server 2005 on Windows 2003.
Here is my error.
Thanks for any help
Connection could not be established.
Array ( [0] => Array ( [0] => 08001 [SQLSTATE] => 08001 [1] => 53 [code] => 53 [2] => [Microsoft][SQL Server Native Client 11.0]Named Pipes Provider: Could not open a connection to SQL Server [53]. [message] => [Microsoft][SQL Server Native Client 11.0]Named Pipes Provider: Could not open a connection to SQL Server [53]. ) [1] => Array ( [0] => HYT00 [SQLSTATE] => HYT00 [1] => 0 [code] => 0 [2] => [Microsoft][SQL Server Native Client 11.0]Login timeout expired [message] => [Microsoft][SQL Server Native Client 11.0]Login timeout expired ) [2] => Array ( [0] => 08001 [SQLSTATE] => 08001 [1] => 53 [code] => 53 [2] => [Microsoft][SQL Server Native Client 11.0]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online. [message] => [Microsoft][SQL Server Native Client 11.0]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online. ) )
Is it possible that your Server's IP address lease expires, get's renewed but the DNS record does not get refreshed? It sounds like your server does allow remote connections but for whatever reason, it looks like it is not being found.
I think I solved my problem. I changed my new Web server to only use my internal DNS. I also entered values into my WINS settings for the web server. The error has not occurred since. I hope this helps anybody else who is having the same problem.
I have faced this problem many times and found that this happens because of improper DNS configuration at server level. Hence, namedpipe fails. The solution i discovered is to create odbc (# client) using tcp/ip address instead of domain name of sqlserver.
Named database (default catalog) does not exist on the specified server (host). I get this error on attempt to connect to a linux environment that has no SqlServer at all.
I have WAMP and am trying to connect to to my SQL Server 2008 database by entering a domain user and password using the following PHP command:
$serverName = "MySQLServer\Instance"; //serverName\instanceName
$connectionInfo = array( "Database"=>"dbName",
"UID"=>"myADUserName", "PWD"=>"MyPassword");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
As I understand it, it is trying to authenticate with a SQL Server created user and not from the Active Directory, so I am getting the following error message:
Array ( [0] =>
Array ( [0] => 28000 [SQLSTATE] => 28000
[1] => 18456 [code] => 18456
[2] => [Microsoft][SQL Server Native Client 10.0]
[SQL Server]Login failed for user 'myADUserName'.
[message] => [Microsoft]
[SQL Server Native Client 10.0]
[SQL Server]Login failed for user 'myADUserName'.
)
[1] =>
Array ( [0] => 28000 [SQLSTATE] => 28000
[1] => 18456 [code] => 18456
[2] => [Microsoft][SQL Server Native Client 10.0]
[SQL Server]Login failed for user 'myADUserName'.
[message] => [Microsoft]
[SQL Server Native Client 10.0]
[SQL Server]Login failed for user 'myADUserName'.
)
)
Is this possible in PHP with WAMP?
I've read about changing the log on account in the wampapache service to myADUserName, but the user doesn't have access administrator access on the server to allow this and I wouldn't want to tie one account to all my PHP applications.
When you assign "UID"=>"myADUserName", "PWD"=>"MyPassword" you are using sql-authentication and not Windows authentication.
But you can create that user/pwd combination into the sql-server as sql-login -> SQL Server Authentication.
When you are want to use windows authentication you don't need to specify uid and pwd. the authentication is taken from the running apache process. Of curse that one needs to be run into that windows account.
See Microsoft documentation
Remarks If values for the UID and PWD keys are not specified in the
optional $connectionInfo parameter, the connection will be attempted
using Windows Authentication. For more information about connecting to
the server, see How to: Connect Using Windows Authentication and How
to: Connect Using SQL Server Authentication.
It looks strange, but this one worked for me,
$dbName='dB-Name';
$dbUser='User1';
$dbPass='B4X345ZXsaoi0omkLH';
$connectionInfo = array( "Database"=>$dbName, "UID"=>$dbUser, "PWD"=>$dbPass);
If you are using SQLSERVER this wont work with php 5.5(I'm using this version didn't test any other)
$connectionInfo = array( "Database"=>"DBName", "UID"=>"User1", "PWD"=>"B4X345ZXsaoi0kLH");
I know this is not a new question because i found lots of similar questions but not the one which solves my issue.
Well my question is how do i connect to a remote sql 2000 server using other domain windows credentials (not the one im logged in with) with php running on apache (xampp)... and the server doesnt run named instance.
These the things i tried,
installed SQL Native client for my php 5.3
got the .dll files to the ext folder
made sure i got the sqlsrv up and running via phpinfo
mssql.secureconnections is on
edited the php.ini and restart the apache server
So, i tried looking at various posts here and elsewhere but couldnt actually solve my problem.
i tried several codes, but here is the one which im currently trying with
$server = "XXX_Server";
$connInfo = array( "Database"=>"DB_Server", "UID"=>"username", "PWD"=>"password");
$conn = sqlsrv_connect($server, $connInfo);
if( $conn )
{
echo "Connection established.";
}
else
{
echo "Connection could not be established.";
die( print_r( sqlsrv_errors(), true));
}
and i get error as shown below,
Connection could not be established.
Array ( [0] => Array ( [0] => 28000 [SQLSTATE] => 28000 [1] => 18456 [code] => 18456 [2] => [Microsoft][SQL Server Native Client 10.0][SQL Server]Login failed for user 'xx\username'. [message] => [Microsoft][SQL Server Native Client 10.0][SQL Server]Login failed for user 'xx\username'. ) [1] => Array ( [0] => 28000 [SQLSTATE] => 28000 [1] => 18456 [code] => 18456 [2] => [Microsoft][SQL Server Native Client 10.0][SQL Server]Login failed for user 'xx\username'. [message] => [Microsoft][SQL Server Native Client 10.0][SQL Server]Login failed for user 'xx\username'. ) )
where xx = domain
And please note that , the client has given me a windows credentials which has access to the server and must use only this. And when i hardcoded my credentials (which has access too) in the script, it didnt work and got the error above, but when i use QueryExpress / SQL Studio, im able to access the db using windows credentials (mine).
So to make it simple, i need to write a script where i need to hardcode the windows credentials and connect to a SQL DB (Stuck here) and make the script accessible for only few using windows ad authentication (which i know how to do)
Please provide me some tips and advice as im stuck and dont know how to proceed. Whatever i research and try, i end up doing the same thing, facing a wall :P
Thanks in advance