问题
I have IIS7 installed and configured on my laptop and with a python cgi interface. I can run the python script from within Eclipse and get the results I am looking for from the database. But when I run the script from the webpage I get an authentication error. It seems that the connection string is not passing the credentials to the SQL server. Any thoughts?
import pyodbc
import cgi
import cgitb; cgitb.enable()
cnxn = pyodbc.connect(driver='{SQL Server}',
server='SERVER\INSTANCE',
Trusted_Connection='yes',
database='Test_Chad',
uid='SERVER\username',
pwd='password')
def getRow():
cursor = cnxn.cursor()
cursor.execute("select user_id, user_name from users")
row = cursor.fetchone()
print 'name:', row.user_id
print 'name:', row.user_name
getRow()
Exception:
<class 'pyodbc.Error'>: ('28000', "[28000] [Microsoft][ODBC SQL Server Driver][SQL Server]Login failed for user 'NT AUTHORITY\\ANONYMOUS LOGON'.
(18456) (SQLDriverConnect); [28000] [Microsoft][ODBC SQL Server Driver][SQL Server]Login failed for user 'NT AUTHORITY\\ANONYMOUS LOGON'. (18456)")
args = ('28000', r"[28000] [Microsoft][ODBC SQL Server Driver][SQL ... for user 'NT AUTHORITY\ANONYMOUS LOGON'. (18456)")
message = ''
回答1:
Decide which form of authentication you want to use, database or Windows. You can't use both.
If you want to use Windows authentication, change your connection setup to the following:
cnxn = pyodbc.connect(driver='{SQL Server}', server='SERVER\INSTANCE',
database='Test_Chad', trusted_connection='yes')
Connecting a web application to a database via Windows authentication requires some other configuration. If your web application uses anonymous authentication in IIS, the database connection will be made using the identity of the application pool in which the web app runs. Here is a screenshot of where this identity is set:
If you want to use simple database authentication, provide the appropriate values for uid
and pwd
, and remove Trusted_Connection=yes
:
cnxn = pyodbc.connect(driver='{SQL Server}', server='SERVER\INSTANCE',
database='Test_Chad', uid='username', pwd='password')
回答2:
I use below code with Identity: as shown as,but not work:
cnxn = pyodbc.connect(
"Driver={SQL Server Native Client 11.0};Server=MyServerName;Database=MyDbName;Trusted_Connection=yes;")
Then I just change my connection to below code and work perfect with Identity= Localsystem
cnxn = pyodbc.connect(
"Driver={SQL Server Native Client 11.0};Server=MyServerName;Database=MyDbName; uid=MyUsernameLoginDb; pwd=MyDbPasswordLoginToDB; ")
回答3:
If you have you application pool security set correctly (i.e. the Application Pool user has login and access rights on SQl Server) then you need to configure Anonymous Access to use the Application Pool Identity instead of its default value (IUSR). Go to the Authentication section IIS 7 --> Anonymous Authentication --> Right-click to edit --> select the option to use the Application pool identity.
回答4:
Simply create one MYSQL user and pass it to uid and pwd.
CREATE LOGIN MyUser WITH PASSWORD = 'pass@123'; CREATE USER MyUser FOR LOGIN MyUser;
It will work, Don't forget to provide the required schema access.
来源:https://stackoverflow.com/questions/12736248/pyodbc-connection-to-mssql-2005-server-via-iis7