简体   繁体   中英

Problem while trying to connect from R to SQL server using a service account not the default Windows authentication

I am in a corporate environment. Using packages DBI and odbc I can easily establish a connection between R and a SQL database like that:

con <- DBI::dbConnect(
  odbc::odbc(),
  driver = SQL.driver,
  server = SQL.server,
  database = SQL.database,
  encoding = SQL.encoding
)

I understood that, without specific UID/PWD, the Windows authentication is used by DBI .

Since this R code will have to run on an external server (in fact, it is a shiny application behind), I need to specify a UID/PWD, and for that I requested and get "service account" from my IT department.

However, as soon as I specify the service account UID/PWD to DBI::dbConnect() I get a login failure:

con <- DBI::dbConnect(
  odbc::odbc()
  ,driver = SQL.driver
  ,server = SQL.server
  ,database = SQL.database
  ,encoding = SQL.encoding
  ,UID = SQL.UID
  ,PWD = SQL.PWD
)
# Error: nanodbc/nanodbc.cpp:1021: 28000: [Microsoft][ODBC SQL Server Driver][SQL Server]Login failed for user 'xxxxxxxx'.

If I manually open MS SQL server and try to connect with this service account using the "SQL server authentication method" instead of "Windows authentication" I also have the same error.

According to the IT department, this is "normal" and I think I am supposed to launch MS SQL server using this service account as user, then to use the Windows authentication to connect. Funny thing is that we are not allowed to "run as" another user.

Therefore, assuming this mechanic would work, how one can mimic this behavior in R please?

I have a similar set up at work with personal creds and service acct creds to a MS SQLServer db. I use the following to connect with the service account...

Note that you will have to download the mssql jdbc jar file .

username <= "myusername"
password <- "mypassword"
drv <- RJDBC::JDBC("com.microsoft.sqlserver.jdbc.SQLServerDriver",
                   'mssql-jdbc-7.0.0.jre8.jar')
connection_string <- paste0("jdbc:sqlserver://10.10.10.01\\srv1;databaseName=dbname;user=", 
                            username, ";password=", password, ";")
conn <- RJDBC::dbConnect(drv, connection_string)

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