R mssql Invalid Connection String - r

I have a docker container that contains a database which I'm able to access with the user id and password below and am running into an error when attempting to connect using this code:
con <- DBI::dbConnect(odbc::odbc(),
Driver = "ODBC Driver 17 for SQL Server",
Port = 1433,
Server = "localhost",
Database = "master",
UID = "sa",
PWD = "password!23")
The error is:
Error: nanodbc/nanodbc.cpp:1021: 00000: [Microsoft][ODBC Driver 17 for SQL Server]Client unable to establish connection [Microsoft][ODBC Driver 17 for SQL Server]Invalid connection string attribute
Any ideas on what the issue could be?
Computer specs:
MacBook Pro (16-inch 2019)
OS: macOS Monterey (version 12.2.1)
Processor: 2.3 GHz 8-Core Intel Core i9
Memory: 16GB 2667MHz DDR4
Startup Disk: Macintosh HD
Graphics: AMD Radeon Pro 5500M 4 GB
Rstudio Version: 2021.09.0 Build 351
OpenSSL Version: 1.1
EDIT:
Switching the code such that the server references 127.0.0.1, 1433 fixed the latter error, but now I'm receiving the former:
Error: nanodbc/nanodbc.cpp:1021: 00000: [Microsoft][ODBC Driver 17 for SQL Server]Client unable to establish connection
Code used to attempt to create connection:
con <- DBI::dbConnect(odbc::odbc(),
driver = "ODBC Driver 17 for SQL Server",
server = "127.0.0.1,1433",
database = "master",
uid = "sa",
pwd = "password!23")
Docker command used to run the container:
docker run -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=password!23' -p '1433:1433' --name sql3 -d mcr.microsoft.com/mssql/server:2017-latest
Container attributes (docker ps --no-trunc):
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f755291fe369f9ad3339626528999bb7cef3d7947e68045b60fc4591f865aace mcr.microsoft.com/mssql/server:2017-latest "/opt/mssql/bin/nonroot_msg.sh /opt/mssql/bin/sqlservr" 24 minutes ago Up 41 seconds 0.0.0.0:1433->1433/tcp sql3

I don't have incontrovertible proof that it was Monterey 12.2.1 that was giving me problems, but downgrading to Big Sur 11.6.4 did resolve my issue (and gave me an opportunity to install dependencies in a more conscientious way).
Thanks everyone for your guidance!

Related

Can't open lib 'ODBC Driver 18 for SQL Server' in R on MacOS (CPU M2)

On a MacOS Ventura with a Apple Silicon M2 CPU, I have installed the Microsoft ODBC driver 18 for SQL Server following the instructions provided by Microsoft at: https://learn.microsoft.com/en-us/sql/connect/odbc/linux-mac/install-microsoft-odbc-driver-sql-server-macos
When using standard DBI connection string in R:
db <- config::get("localhost")
conn <- DBI::dbConnect(odbc::odbc(),
Driver = "ODBC Driver 18 for SQL Server",
Server = "localhost",
UID = db$uid,
PWD = db$pwd,
Port = 1433,
Database = db$database
When running this code I get the following error:
Error: nanodbc/nanodbc.cpp:1021: 00000: [unixODBC][Driver Manager]Can't open lib 'ODBC Driver 18 for SQL Server' : file not found
I have the feeling that this indicates that there is something wrong with the connection between my R and the driver register in macOS. Unfortunately, I have not been able to identify how to correct the problem.
Does anybody have experience with this and can advise me on how to connect?
I appreciate your assistance!
Try:
odbcinst -j
Then you will see:
unixODBC 2.3.4
DRIVERS............: /etc/odbcinst.ini
SYSTEM DATA SOURCES: /etc/odbc.ini
FILE DATA SOURCES..: /etc/ODBCDataSources
USER DATA SOURCES..: /Users/emehex/.odbc.ini
SQLULEN Size.......: 8
SQLLEN Size........: 8
SQLSETPOSIROW Size.: 8
Please check your ODBC version in /etc/odbcinst.ini (you should see):
[ODBC Driver 17 for SQL Server]
Description=Microsoft ODBC Driver 17 for SQL Server
Driver=/usr/local/lib/libmsodbcsql.17.dylib
UsageCount=1
if not, please upgrade to ODBC Driver 18

How do I connect to an SQL Server d atabase using R and Rstudio? (Mac)

Disclaimer: I'm a statistician/bioinformatician by training, so I'm quite new to networks, servers, and databases.
System: Macbook Pro (M1 chip).
I'm trying to connect to an SQL Server database remotely via R and RStudio.
To start, I ran the following commands in terminal (as seen here https://learn.microsoft.com/en-us/sql/connect/odbc/linux-mac/install-microsoft-odbc-driver-sql-server-macos?view=sql-server-ver16):
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
brew tap microsoft/mssql-release https://github.com/Microsoft/homebrew-mssql-release
brew update
HOMEBREW_NO_ENV_FILTERING=1 ACCEPT_EULA=Y brew install msodbcsql18 mssql-tools18
The code I'm running in RStudio is as follows (as seen here https://db.rstudio.com/getting-started/connect-to-database):
library(DBI)
library(odbc)
con <- DBI::dbConnect(odbc::odbc(),
Driver = "ODBC Driver 18 for SQL Server",
Server = "xxx.xxx.xxx.xx",
Database = "Dbname",
UID = "username",
PWD = "password",
Port = 3306,
.connection_string ="TrustServerCertificate=yes")
The above gives me the following error:
Error: nanodbc/nanodbc.cpp:1021: 00000: [unixODBC][Driver Manager]Data
source name not found and no default driver specified
I can't find any help related to the errors I'm getting at https://db.rstudio.com/getting-started.
Slightly different piece of code gives me a different error:
con <- DBI::dbConnect(odbc::odbc(),
.connection_string = "Driver={ODBC Driver 18 for SQL
Server};Uid=username;Pwd=password;Host=xxx.xxx.xxx.xx;Port=3306;Database=Dbname;TrustServerCertificate=yes;")
Error: nanodbc/nanodbc.cpp:1021: 00000: [Microsoft][ODBC Driver 18 for
SQL Server]Neither DSN nor SERVER keyword supplied [Microsoft][ODBC
Driver 18 for SQL Server]Invalid connection string attribute
What is a Server Keyword as referred to in the second error? Is the server supposed to be an IP address as I've indicated in the code?
Does the use of ODBC Driver matter? How can I tell if I'm using the right one?
Am I off the mark with any of the information I'm feeding into dbConnect()?
Any tips welcome.
Thanks.
Thanks to #AlwaysLearning for pointing at the mistake in my second chunk of code. Host= should have been Server=.
As for the first chunk, I don't know why it wouldn't work.

Problems Connecting to Database within Docker Image via RStudio using DBI

I'm attempting to connect to a database within a local Docker image I've spun up using DBI and am running into some pretty cryptic errors... I've successfully started the Docker container (below shows result of docker ps -a):
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
131ab042012c mcr.microsoft.com/mssql/server:2017-latest "/opt/mssql/bin/nonr…" 27 minutes ago Up 27 minutes 127.0.0.1:1433->1433/tcp admiring_northcutt
I then try to connect via:
pool<- pool::dbPool(odbc::odbc(),
Driver = "ODBC Driver 17 for SQL Server",
Server = "localhost",
Database = "master",
UID = "sa",
PWD = "***********",
Port = 1433)
and receive the error:
Error: nanodbc/nanodbc.cpp:1021: 00000: [Microsoft][ODBC Driver 17 for SQL Server]Client unable to establish connection [Microsoft][ODBC Driver 17 for SQL Server]Invalid connection string attribute
Checking my odbc driver using odbc::odbcListDrivers() returns the output below, so the drivers seem to be present.
name attribute value
1 ODBC Driver 17 for SQL Server Description Microsoft ODBC Driver 17 for SQL Server
2 ODBC Driver 17 for SQL Server Driver /usr/local/lib/libmsodbcsql.17.dylib
3 ODBC Driver 17 for SQL Server UsageCount 2
In addition, executing the container and attempting to connect to the database via the commands:
docker exec -it <container-name> /bin/bash
/opt/mssql-tools/bin/sqlcmd -S 127.0.0.1 -U sa -P '<password>'
EDIT: The above method now works and I'm able to actively query within the database. I know that the error can't be due to invalid login credentials because I'm using the same exact ones as I ran above in the container.
This same process works for a coworker, so I'm wondering if it's a local issue with my set up. We already compared our .ini files as suggested in this link and they're identical, I've clean re-installed Docker, and I've even ran the coworker's identical container with no luck. Any ideas what could be happening here?
Computer specs:
MacBook Pro (16-inch 2019)
OS: macOS Monterey (version 12.2.1)
Processor: 2.3 GHz 8-Core Intel Core i9
Memory: 16GB 2667MHz DDR4
Startup Disk: Macintosh HD
Graphics: AMD Radeon Pro 5500M 4 GB
Rstudio Version: 2021.09.0 Build 351
OpenSSL Version: 1.1

Can't connect to PostgreSQL in Rstudio, possible permissions issue?

I've followed the documentation on db.rstudio.com and other sites, and I'm not able to connect to my Postgres database from Rstudio. I'm on macos 10.14. Running:
con <- DBI::dbConnect(odbc::odbc(),
Driver = "PostgreSQL Driver",
Database = "mydatabase",
UID = "myuser",
PWD = "mypassword",
Host = "localhost",
Port = 5432)
Gives the error:
Error: nanodbc/nanodbc.cpp:983: 00000:
At first, I got an empty list when running odbcListDrivers(), however I was able to get it to show the postgres driver location/value by copying odbcinst.ini from the global /etc to my user (cp /etc/odbcinst.ini ~/.odbcinst.ini).
Similarly, odbcListDataSources() returned nothing until I did the same thing (cp /etc/odbc.ini ~/.odbc.ini)
The odbc.ini and odbcinst.ini are locked from editing via Finder, but can be edited via nano/terminal.
I also tried to solve the problem by installing the database drivers via Postgres' stack builder, but it didn't change anything.
Overall I'm suspecting that it's a permissions issue, but I'm not quite sure where to look next.
Thanks for your help!
The permission for the odbc.ini was:
-rw-rw-rw-# 1 root wheel 139B Mar 31 12:57 odbc.ini
Which I changed to:
-rwxr-xr-x# 1 myuser wheel 139B Mar 31 12:57 odbc.ini
But that didn't solve the problem, the error is the same.
The content of odbcinst.ini is:
[PostgreSQL Driver]
Driver = /usr/local/lib/psqlodbcw.so
The content of odbc.ini is:
[PostgreSQL]
Driver = PostgreSQL Driver
Database = mydatabase
Servername = localhost
UserName = myuser
Password = mypassword
Port = 5432
odbc::odbcListDrivers() gives:
name attribute
1 PostgreSQL Driver Driver
2 ODBC Drivers Amazon Redshift
3 Amazon Redshift Driver
value
1 /usr/local/lib/psqlodbcw.so
2 Installed
3 /opt/amazon/redshift/lib/universal/libamazonredshiftodbc.dylib
odbc::odbcListDataSources() gives:
name description
1 PostgreSQL PostgreSQL Driver

Connect R to Redshift with RPostgreSQL on Mac 10.11.3

[Original Post]
I have seen a couple of similar SO questions but I don't think any of them are related to OS.
Long story short, this is what I see when I try to establish a ssl connection to Redshift.
library(RPostgreSQL)
drv <- dbDriver("PostgreSQL")
con <- dbConnect(drv,
host = "XXXXXXXX.us-east-1.redshift.amazonaws.com",
port = 5439,
user = "XXXXXXXX",
password = "XXXXXXXX",
dbname = "db1")
> Error in postgresqlNewConnection(drv, ...) :
RS-DBI driver: (could not connect XXXXXXXX#XXXXXXXX.us-east-1.redshift.amazonaws.com on dbname "db1")
I have tried the following and none of them worked.
Connect to Redshift via SSL using R
Importing files from PostgreSQL to R
Connect to Postgres via SSL using R
https://groups.google.com/forum/#!topic/rpostgresql-dev/ELnVUJqjDbk
I have an EC2 node (Ubuntu) in AWS and another Mac Pro in office, so I tried to use the same code to connect from both.
The Linux system works with copying & pasting the exact same code. Here is the Sys.info() on the Linux server:
sysname Linux
release 3.13.0-48-generic
version #80-Ubuntu SMP Thu Mar 12 11:16:15 UTC 2015
nodename ip-xx-xxx-xx-xx
machine x86_64
login unknown
user bcui
effective_user bcui
However, the Mac Pro failed with the exact same error message. Here is the Mac Sys.info() (same as my Mac laptop):
sysname Darwin
release 15.3.0
version Darwin Kernel Version 15.3.0:
Thu Dec 10 18:40:58 PST 2015;
root:xnu-3248.30.4~1/RELEASE_X86_64
nodename bcui-MBP.local
machine x86_64
login bcui
user bcui
effective_user bcui
I am wondering what could be different between the Mac and Linux configuration that causes this error message?
[Update 2016/02/10]
I have tried to uninstall R and all related files, and then reinstall R from homebrew:
brew tap homebrew/science
brew install R
Same error message:
> Error in postgresqlNewConnection(drv, ...) :
RS-DBI driver: (could not connect XXXXXXXX#XXXXXXXX.us-east-1.redshift.amazonaws.com on dbname "db1")
FYI, I can connect to other non-ssl Redshift clusters using the same code, so it could be something related to openssl on Mac.
[Update 2016/02/11]
More info from #MasashiMiyazaki's comment:
I have used psql via CLI to connect and it works fine. In addition, I can also connect successfully using the Python module psycopg2. However, I will have to disable this line from .bash_profile in order for them to work:
export DYLD_LIBRARY_PATH=/usr/lib:$DYLD_LIBRARY_PATH
Taking a shot in the dark here. Have you tried RPostgres?
Install it like so
# install.packages("devtools")
devtools::install_github("RcppCore/Rcpp")
devtools::install_github("rstats-db/DBI")
devtools::install_github("rstats-db/RPostgres")
Then test it out
library(DBI)
library(RPostgres)
con <- dbConnect(RPostgres::Postgres(),
host = "XXXXXXXX.us-east-1.redshift.amazonaws.com",
port = 5439,
user = "XXXXXXXX",
password = "XXXXXXXX",
dbname = "db1")

Resources