R Oracle DB connection fails with dbPool but succeeds with dbConnect - r

I'm attempting to refactor older code to make use of DB pools using the pool package's dbPool function.
Historically I've been using the DBI package's dbConnect function without issue. I can successfully create a connection to my Oracle database with the below code (all credentials are faked):
conn <- DBI::dbConnect(
ROracle::Oracle(),
"database.abcd1234.us-east-1.rds.amazonaws.com/orcl",
username="username",
password="hunter2"
)
However, when I use the same credentials in the same development environment to attempt to create a pool like this:
pool <- pool::dbPool(
drv = ROracle::Oracle(),
dbname = "orcl",
host = "database.abcd1234.us-east-1.rds.amazonaws.com",
username = "username",
password = "hunter2"
)
I get an error:
Error in .oci.Connect(.oci.drv(), username = username, password = password, :
ORA-12162: TNS:net service name is incorrectly specified
I've used dbPool before but with Postgres databases instead of Oracle, and for Postgres, it just worked! I'm thinking that because my credentials work fine for dbConnect, there must be some small thing I'm missing that's needed for dbPool to work correctly too

orcl is the service name, not the database name.
Try:
pool <- pool::dbPool(
drv = ROracle::Oracle(),
host = "database.abcd1234.us-east-1.rds.amazonaws.com/orcl",
username = "username",
password = "hunter2"
)
Or
pool <- pool::dbPool(
drv = ROracle::Oracle(),
sid = "orcl",
host = "database.abcd1234.us-east-1.rds.amazonaws.com",
username = "username",
password = "hunter2"
)

Related

How can i establish a connection to a db no using credentials every time

I'm working with several R files that need connect to database but server has timeout so timeout=inf is not functional
What i was looking for is something like this:
RMySQL Database connection
(I'm the only user in R, and in total users just a few)
Inserting in my .RProfile all what is needed (credentials) and only in the .r programs that connect and disconnect when were necessary
In .RProfile:
con <- dbConnect(odbc::odbc(), Driver = "{MariaDB ODBC 3.1 Driver}",
Server = "{host}", database = "db", UID = "userid",
PWD = "pwd",
Port = 1234)
and in .R programs use something like this:
conn <- dbConnect(odbc::odbc(), group = "what i should use here?")
#using database
tbl(conn,"table")
#more code
dbDisconnect(conn)
I was too looking for other option, pool
In .RProfile
library(pool)
pool<- dbPool(odbc::odbc(), Driver = "{MariaDB ODBC 3.1 Driver}",
Server = "{host}", database = "db", UID = "userid",
PWD = "pwd",
Port = 1234)
.Last <- function(){
poolClose(pool)
}
But i'm not sure if it works or the previous option is better.

Connect SQL Server with RStudio and access data in the environment

I have a connection to a mysql server like this:
mydb <- DBI::dbConnect(
drv = MySQL(),
host = "host",
port = 1111,
user = "user_1",
password = "password",
dbname = "database_name"
)
and then I do queries to that data table using this code
query1 <- fetch(dbSendQuery(mydb, "select * from table_1"), n = Inf)
so the result is that I have a query1 table in the R environment.
Now I have other databases in SQL Server, so I'm trying to do the same. I'm establishing the connection doing this:
con <- dbConnect(odbc(),
Driver = "SQL Server",
Server = "server",
Database = "database_2",
UID = "user_2",
PWD = "password",
Port = 2222)
and it seems that works, because in the Connection tab appears the database, but when I navigate and try to see the data an error occurs. Besides this, I'm looking for functions that do the same like the previous (fetch with dbSendQuery), having this way the data frames available in the environment.
I finally solved the problem using this code:
con <- dbConnect(odbc(),
Driver = "ODBC Driver 17 for SQL Server",
Server = "server",
Database = "database_2",
UID = "user_2",
PWD = "password",
Port = 2222)
So the problem was in the Driver.

ssl connection for RJDBC

My company is instituting an ssl requirement soon for database connections.
I previously connected to our Vertica database via DBI and RJDBC packages. I have tried adding an sslmode='require' parameter to my connection. But adding this parameter has no effect. I can still connect to the database but the connection is not ssl.
Can anyone advise on how to enable ssl connection for DBI? In PyCharm I merely had to set ssl to true in the driver properties.
DBI::dbConnect(
drv = RJDBC::JDBC(
driverClass = driver_class,
classPath = class_path
),
url = url,
UID = user_id,
PWD = password,
sslmode = 'require'
)
}
A different ssl parameter was required. I am having connection success with the function below that uses ssl = 'true'
DBI::dbConnect(
drv = RJDBC::JDBC(
driverClass = driver_class,
classPath = class_path
),
url = url,
UID = user_id,
PWD = password,
ssl = 'true'
)

Connect with RJDBC like with odbc

I am trying to connect to my MS SQL database with RJDBC and I don't know what to fill in url argument. With odbc this was enough:
dbConnect(odbc::odbc(),
Driver = "SQL Server",
dsn = "MyDsn",
uid = "User",
pwd = "123456",
server = "myserver123456\\myserver1",
database = "MyDatabase")
When I swap the driver from odbc to jdbc then it fails:
dbConnect(RJDBC::JDBC(classPath = "C:/jdbc/mssql-jdbc-7.0.0.jre8.jar"),
Driver = "SQL Server",
dsn = "MyDsn",
uid = "User",
pwd = "123456",
server = "myserver123456\\myserver1",
database = "MyDatabase")
error: Error in .jcall("java/sql/DriverManager", "Ljava/sql/Connection;", "getConnection", :
argument "url" is missing, with no default
What should i write in url argument? How to get know?
RJDBC uses different arguments for the dbConnect function: a driver definition and a connection URL (the piece you are missing). For example (from https://www.rforge.net/RJDBC/), to connect to a MySQL Database, your code would look like the following:
library(RJDBC)
drv <- JDBC("com.mysql.jdbc.Driver",
"/etc/jdbc/mysql-connector-java-3.1.14-bin.jar",
identifier.quote="`")
conn <- dbConnect(drv, "jdbc:mysql://localhost/test", "user", "pwd")
Loads a JDBC driver for MySQL (adjust the path to the driver's JAR file as necessary) and connects to the local database "test". The connection handle conn is used for all subsequent operations.
For SQL Server, your code will look something like the following (from https://www.r-bloggers.com/connecting-to-sql-server-from-r-using-rjdbc/):
require(RJDBC)
drv <- JDBC("com.microsoft.sqlserver.jdbc.SQLServerDriver",
"C:/jdbc/mssql-jdbc-7.0.0.jre8.jar")
conn <- dbConnect(drv, "jdbc:sqlserver://serverName", "userID", "password")

No database selected with RMySQL

I'm trying to connect to remote publicly-accessible MySQL server EnsEMBL public server using RMySQL, but when I try to list the tables, an error occurs:
library(RMySQL)
mydb = dbConnect(MySQL(),
user = 'anonymous',
port = 5306,
host = 'asiadb.ensembl.org')
dbListTables(mydb)
Error in .local(conn, statement, ...) :
could not run statement: No database selected
Is there a a way to find out the name? Or Am I making a completely different mistake altogether?
You have to specify the name of the db in the dbConnect call. e.g. :
mydb = dbConnect(MySQL(),
user = 'anonymous',
port = 5306,
host = 'asiadb.ensembl.org',
db = 'homo_sapiens_core_83_38')
dbListTables(mydb)
It is weird that database = 'testdb' executed with dbExecute in R
db <- dbConnect(RMySQL::MySQL(),
user = 'root',
password = 'pwd123',
host = 'localhost',
database = 'testdb'
)
dbExecute(db, MySQLStatement) # Executed Without Error
But when used dbListTables(db) showing no databases selected.
Changed database into db worked as expected
db <- dbConnect(RMySQL::MySQL(),
user = 'root',
password = 'pwd123',
host = 'localhost',
db = 'testdb'
)

Resources