user2066880
user2066880

Reputation: 5024

The server principal is not able to access the database under the current security context

This seems to be a fairly common question but looking at the help & solutions for other related threads hasn't helped me.

I'm using the PHP code block here (http://pastebin.com/xSJYyC3y) to try to connect and query a SQL Server database. The connection seems to work, but when making a query, I get the following error message:

[Microsoft][SQL Server Native Client 11.0][SQL Server]The server principal "my_username" is not able to access the database "database_name" under the current security context.

I only get this error when connecting to the server via PHP, if I do it manually through the MS SQL Management Studio, then everything works fine.

I am using Windows 7 x64, SQL Server 2008 SP1 x64, and PHP 5.4.

Thanks for looking at my problem!

Upvotes: 1

Views: 4831

Answers (1)

user806549
user806549

Reputation:

If you can run the query through SSMS, but get a security issue through PHP, you are most likely experiencing an issue with wrong impersonation.

Generally, you'll have two different security issues in play:

  • The SQL Server's internal security - if you are logging on to the server with a SQL Server account and you are able to run the query from within SSMS using the same account as from PHP, the SQL Server security should be ok. If you cannot use the same account, start looking into roles and security on the server, temporarily relaxing the security for the account used by PHP.
  • Windows security: If the connection from PHP is not configured properly, you may see problems with impersonation. The most thorough explanation of this I've seen can be found through Brian Swans answer here.

Do note that if your query touches several databases, or especially if it includes something that explicitly touches the filesystem (perhaps on a shared folder), you need to check rights on all servers and all folders that are in play.

Also, if you are executing a stored procedure, make sure that you don't have the issue with EXECUTE AS OWNER. One example of this can be seen here

If that doesn't help you could still look into the security of the database files on your Windows box and try to - temporarily - relax the security to the folder to see if that changes anything.

Upvotes: 1

Related Questions