Cannot Connect to Azure SQL when deploying to Shinyapps.io - r

I have built an R shiny app/dashboard which runs perfectly on my local Windows 10 machine. I am now ready to deploy it to the web. I have chosen to do this using Shinapps.io. The app deploys, but immediately disconnects when trying to access it. Checking the log file yields this error:
Error : nanodbc/nanodbc.cpp:1021: 00000: [FreeTDS][SQL Server]Unable
to connect: Adaptive Server is unavailable or does not exist
[FreeTDS][SQL Server]Unable to connect to data source [FreeTDS][SQL
Server]Unknown host machine name.
My current implementation for connecting to the database is as follows:
conn_args <- config::get("dataconnection")
is_local<-Sys.getenv('SHINY_PORT')==""
message(paste("is_local: ", is_local))
if(is_local){
con <- odbc::dbConnect(odbc::odbc(),
Driver = "ODBC Driver 13 for SQL Server",
Server = conn_args$server,
Database = conn_args$database,
Uid = conn_args$uid,
PWD = conn_args$pwd,
Port = conn_args$port,
TrustServerCertificate="no")
} else {
#con <- RODBC::odbcDriverConnect("Driver=FreeTDS;TDS_Version=7.2;Server=tcp:#######;Port=1433;Database=######;Uid=######;Pwd=#######;Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30")
#con <- RODBC::odbcDriverConnect("Driver=FreeTDS;TDS_Version-7.2;Server=tcp:#######,1433;Database=###########;Uid=######;Pwd=######;Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30;")
con <- odbc::dbConnect(
odbc::odbc(),
Driver = "FreeTDS",
Server = conn_args$server,
Database = conn_args$database,
UID = conn_args$uid,
PWD = conn_args$pwd,
Port = 1433,
TrustServerCertificate="no",
TDS_Version = 7.4
)
}
As ShinyApps.io runs a Linux instance, I'm using FreeTDS as the driver on deployment. I have adjusted the TDS_Version with 7.0, 7.1, 7.2, 7.4, 8.0, and 9.0, as well as removing that parameter completely. Initally I tried using "SQLServer" as the driver name as per the Shinyapps.io documentation suggestion but that failed, too. I even tried using the RODBC package instead of odbc, but that went nowhere either.
I have set my Azure firewall to let in all necessary IP addresses. I even briefly opened the firewall completely, but that did not yield different results.
I have checked every thread and tutorial I can find, including:
Cannot connect to Microsoft Azure from shinyapps.io
Connecting Azure SQL databse to shinyapps.io
https://groups.google.com/g/shinyapps-users/c/hs4bQHsk9JU
https://docs.rstudio.com/shinyapps.io/applications.html#accessing-databases-with-odbc
Besides the arguments passed to the driver parameter(s), my connection arguments remain the same for local and web deployment. What am I missing? Is Azure SQL simply not compatible with Shinyapps.io?

Found the answer here: https://community.rstudio.com/t/unable-to-connect-to-azure-sql-database-in-shinyapps-io/45723
In the Azure portal, the ODBC connection string formats the server name like so: Server=tcp:servername.database.windows.net
Although this works locally, shinyapps.io does not like that format. I simply had to remove "tcp:" prefix from the server name, and the connection went through: Server=servername.database.windows.net
I also changed my driver to "SQLServer", although "FreeTDS" is still a viable option. If you use "FreeTDS", ensure that you have this extra parameter: TDS_Version=7.2

Related

pyodbc DSN-less connection with AS400 / IBM midrange

I want to connect via pyodbc with a AS400 Database.
I configured it via the DSN syntax and it is working, but I need it DSN less.
This my current code:
connection = pyodbc.connect(
driver='{iSeries Access ODBC Driver}',
system='192.***.***.***',
uid='U******',
pwd='p0*****')
But always I got the error:
('HY000', '[HY000] [IBM][System i Access ODBC Driver]Missing system name needed for connection. (30188) (SQLDriverConnect); [HY000] [IBM][System i Access ODBC Driver]Missing system name needed for connection. (30188)')
I already tried it with the older driver "IBM i Access ODBC Driver" but same error is comming up.
Did someone also had this error?
Thanks
in my case - I just need to use capital letters for the functions variables:
connection = pyodbc.connect(
DRIVER='{iSeries Access ODBC Driver}',
SYSTEM='192.***.***.***',
UID='U******',
PWD='p0*****')

ODBC Library in R - getting "Data source name not found and no default driver specified" Error

I have the driver working everything because i am using Power BI perfectly. I am switching to R for some of the calculations and now this error is creeping up
install.packages("odbc")
library(odbc)
con<- dbConnect(odbc::odbc(),
driver = "[Cloudera ODBC Driver for Apache Hive]",
host = "[Confidential]",
Schema = "[Confidential]",
user = ("Confidential"),
password = ("Confidential"),
port = 8443)
My ODBC connection is 64 bit
Is there a specific connection string that i need to use here?
There are four issues to consider here:
Originally, the driver had brackets around it, those needed to be removed.
# original
con<- dbConnect(odbc::odbc(),
driver = "[Cloudera ODBC Driver for Apache Hive]", ...)
# fixed
con<- dbConnect(odbc::odbc(),
driver = "Cloudera ODBC Driver for Apache Hive", ...)
I suspect that the wrong labels user= and password= need to be changed. Per https://db.rstudio.com/databases/hive/, I think they should have been UID= and PWD=, respectively.
Since the connection error suggested connection-encryption (SSL) problems, then further arguments should be provided to DBI::dbConnect to set the correct options. I don't know what they are, to be honest, and I could not find documentation on what they are, so I ignored this problem. On to issue 4 ...
You said that you configured this within your "ODBC Data Source Administrator (64 Bit)", which is a system-wide configuration for named connections, and that you use this configuration for Power BI (meaning that the connection there is good). In your code above, you are not using that system configuration, you are defining it from-scratch. Since the system configuration is known to work, you can use that instead of re-defining, as in
con <- DBI::dbConnect(odbc::odbc(), dsn = "myhive", UID = "myuser", PWD = "mypass")
where myhive is the name you assigned the configuration within your ODBC data source administration.

Cannot connect to Microsoft Azure from shinyapps.io

I built an Rshiny application that pulls data from Microsoft Azure. My application works locally when I use the 'SQL Server' driver in my connection string, but does not work when I publish the app on shinyapps.io.
Based on a suggestion from here, I have been trying to use the 'FreeTDS' driver to connect with Azure when publishing on shinyapps.io but I am not having any luck.
Here is my connection string:
con <- dbConnect(odbc::odbc(),
Driver = "FreeTDS",
Server = "servername",
Database = "databasename",
Uid = "uid",
Pwd = "pwd",
Port = 1433,
TDS_Version = 9.0)
I receive the following error message when using the 'FreeTDS' driver:
Error in value[[3L]](cond) :
nanodbc/nanodbc.cpp:950: 08001: [unixODBC][FreeTDS][SQL Server]Unable to connect to data source
I have tried using TDS_Version 7.0, 7.2, 7.4, 9.0 - none have worked. Can anyone help me decode this error message? Thank you!
Note: I am working with R 3.6 on Windows 10. I have whitelisted shinyapps.io IP addresses on Azure, so that is not the issue.
For connecting both on local Windows and Shinyapps.io, this has worked for me:
library(RODBC)
is_local<-Sys.getenv('SHINY_PORT')==""
dbConnector <- function(local=FALSE){
if(local){dbConn <- odbcDriverConnect("Driver=ODBC Driver 13 for SQL Server;Server=xxx.database.windows.net,1433;Database=xxxxxxxxx;Uid=xxxx;Pwd=xxxxxxxxx;Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30")
} else {dbConn <- odbcDriverConnect("Driver=FreeTDS;TDS_Version=8.0;Server=xxxx.database.windows.net; Port=1433;Database=xxxxx;Uid=xxxxxx;Pwd=xxxxxxxxx;Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30")}
}
dbConn <- dbConnector(is_local)
The driver requires DSN and PWD to be specified only on the connection string, they cannot be specified in the DSN. http://www.freetds.org/userguide/odbcconnattr.htm
For more details, refer Similar GitHub issue, which addresses similar issue.
Hope this helps.

R Pool Order of Connections

I am using the R Pool package to connect to both SQL Server and Oracle databases within the same application. If I create the Oracle pool first I am unable to connect to SQL Server and get the following error:
Error in .verify.JDBC.result(jc, "Unable to connect JDBC to ", url) :
but if I create the SQL Server pool first I have no problems and can subsequently connect to Oracle.
wd <- getwd()
driver1 = JDBC("com.microsoft.sqlserver.jdbc.SQLServerDriver", classPath = file.path(wd,"mssql-jdbc-7.2.2.jre8.jar",":",wd,"jtds-1.3.1a.jar"))
connection1 = dbPool(drv=driver1, url=**, user=**, password=**, minsize=1, maxsize=5)
driver2 = JDBC("oracle.jdbc.driver.OracleDriver", classPath = file.path(wd,"ojdbc6.jar"))
connection2 = dbPool(drv=driver2, url=**, user=**, password=**, minsize=1, maxsize=5)
Has anyone experienced something similar?

Using RODBC in R to connect to Azure SQL

I am using R 3.2.4 and El captain, I wanted to connect to Azure SQL using the RODBC package in R, I did the following to install it
brew install unixodbc
install.packages("RODBC",type="source")
And also
brew install freetds --with-unixodbc
The output of above command:
Warning: unixodbc-2.3.4 already installed
Warning: freetds-0.95.80 already installed
But whenever I try to connect using the following:
library("RODBC")
con = odbcDriverConnect(
'driver = {SQL Server};
Server = xxxxxx;
Database = xxxxx;
User Id= xxxxx;
Password= xxxxx;')
I get the following Error:
[RODBC] ERROR: state IM007, code 59478176, message [iODBC][Driver Manager]No data source or driver specified, dialog prohibited
This is my first post, so please be forgiving. I have got this set up working for a windows environment and an Azure SQL server that use Active Directory.
Plan of attack for this problem:
First try to connect to the Azure database using the SQL server management Studio.
Secondly, try to connect to the Azure database using the odbcad32. If that works you can create a User DSN, which for the example's sake I will call example.
Finally use odbcDriverConnect("DSN=example;") in R
First of all try to login to the Azure database using Sql Server Management Studio. This involves two substeps.
On the target database your user domain and name may be different from the user domain and name on the machine from which you are trying to connect to the target database. Should that be the case then create a credential using the credential manager in windows.
Creating a windows credential to match your identity with that used by the Azure database
Verify the method of authentication from the dialog you get when connecting with SQL server management studio. On my machine I thus saw that the Azure SQL server uses a thing called 'Active Directory Integration' .
Select the method of authentication that applies
Secondly instead of directly testing using RODBC calls it is more convenient to use the ODBC administrator. Run 'odbcad32'. You have to choose the driver.
If you want to use Active Directory for Authentication you need the 'ODBC Driver 13 for SQL Server'. The other drivers, such as 'SQL Server' and the 'Native Client' do not support Active Directory.
Create a user DSN and test it.
The end result should look like this.
If you do not get the desired success you may instead get the following error messages.
If this occurs simply go ahead and install the required software
Go ahead and install the Sign in assistant.
Another error that may pop up is the error saying that you have not installed the Active Directory Authentication Library for SQL Server. Surely this is not true if you have the set up working via SQL server management studio. The problem is that odbcad32 cannot find adalsql.dll. The solution is to add a few registry entries.
Which registry entries to add
Addendum: Below some people note they got it working with the 11.0 driver instead of the 13.0 one. They are perfectly right, but that is because they use user / password authentication. If your server uses active directory integration you do really need the 13.0 or higher versions of the ODBC driver.
So after a lot of trail and error, here is what I have:
1) Ditch RODBC and use RSQLServer
install.packages("RSQLServer")
Create a config file (use sublime) called 'sql.yaml', with the following contents:
SQL_PROD:
server: 11.1.111.11
type: &type sqlserver
port: &port 1433
domain: &domain companyname
user: &user winusername
password: &pass winpassword
useNTLMv2: &ntlm true
SQL_DEV:
server: 11.1.111.15
type: *type
port: *port
domain: *domain
user: *user
password: *pass
useNTLMv2: *ntlm
AW:
server: <yourservername>
type: sqlserver
user: <username>
password: <password>
port: 1433
2) Save this YAML file in the following location(run the following in R: Sys.getenv("HOME")) e.g.: if username is dave its "/Users/dave"
3)
#############
# DBI #
#############
# Note we do not attach the RSQLServer package.
library(DBI)
# Connect to AW server in ~/sql.yaml
aw <- dbConnect(RSQLServer::SQLServer(), "AW", database = 'db')
# RSQLServer only returns tables with type TABLE and VIEW.
dbListTables(aw)
I installed the "odbc" library and setup the connection as described on the RStudio pages. The 11.0 driver worked for me.
library(odbc)
con <- dbConnect(odbc(),
Driver = "SQL Server Native Client 11.0",
Server = "servername",
Database = "databasename",
UID = "username",
#PWD = rstudioapi::askForPassword("Database password"),
PWD = "pwd",
Port = 1433)

Resources