简体   繁体   中英

Login Failed for ' '. when using DSN with Rstudio connecting to MS SQL 2016

Running into a odd error when trying to move a SQL connection details from R code to the DSN and using the DSN as the connection string.

Using the ODBC package, I can build a connection through RStudio using:

con <- DBI::dbConnect(odbc::odbc(),
                  Driver = "SQL Server Native Client 11.0",
                  Server = "XXX",
                  Database = "YYY",
                  uid = "username",
                  pwd = "password",
                  port = 1443)

This code above works correctly and allows the SQL connection. When moving this to the DSN (have attempted both user and system DSN) an error is generated. Both user and system DSN's have been tried and when connections are tested from the ODBC application in Windows (Win 10), the test is successful.

When using the code below in R, the connection fails.

con <- DBI::dbConnect(odbc::odbc(), "ZZZ")

Error: nanodbc/nanodbc.cpp:950: 28000: [Microsoft][SQL Server Native Client 11.0][SQL Server]Login failed for user ''

This would seem to indicate that the username is unknown but my information is that all of the connection parameters are stored in the DSN entry. My initial setup was generated from: https://support.rstudio.com/hc/en-us/articles/214510788-Setting-up-R-to-connect-to-SQL-Server-

Following the further discussion at the bottom of the page, only appears to discuss the code version of the connection. https://db.rstudio.com/databases/microsoft-sql-server/

This link discusses the DSN component as well as ensuring the driver is available. https://db.rstudio.com/getting-started/connect-to-database This confirms the code structure when trying to use the DSN.

I also verified this code snippet via: https://www.r-bloggers.com/setting-up-an-odbc-connection-with-ms-sql-server-on-windows/

I attempted to use the SQL Server Driver versus the Native Client version and received the same error except the ODBC SQL Server Driver identifier instead of the Native Client 11.0. This seems to indicate that the dbconnect call is at least identifying the DSN entry.

Using the code below I confirmed that the Driver for both SQL Server and native Client 11.0 were available.

sort(unique(odbcListDrivers()[[1]]))

Add trusted connection attribute

con <- DBI::dbConnect(odbc::odbc(),
                  Driver = "SQL Server Native Client 11.0",
                  Server = "XXX",
                  Database = "YYY",
                  Trusted_Connection = "True",
                  uid = "username",
                  pwd = "password",
                  port = 1443)

Not sure if you're encountering the same issue, but the credentials used during DSN creation (Windows account) were different from those used in the dbConnect function call (somehow, my Microsoft account).

The user in the error message did not exist in the Security roles within SQL Server, thus not granting access to connect to the database.

Granting the user in the error message access to the required SQL server/database fixed the issue*.

CREATE LOGIN [MicrosoftAccount\xyz@outlook.com] FROM WINDOWS WITH DEFAULT_DATABASE=[master], DEFAULT_LANGUAGE=[us_english]
ALTER SERVER ROLE [sysadmin] ADD MEMBER [MicrosoftAccount\xyz@outlook.com]

* though additional security measures are advised in role creation, off-topic here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM