Getting Unknown MySQL Error while executing queries - mysql

Configurations:
R version: 3.6.3
Aurora MySQL Cluster: 5.7
Library: RMySQL
Dbconnect Method
con <- dbConnect(
RMySQL::MySQL(),
dbname = dbName,
host = "127.0.0.1",
user = user,
port = port,
password = pass
)
Connecting to the Aurora DB using SSH:
ssh -N -L <local_port>:<amazon_rds_cluster_url>:<sql_port> -i <pem_file> ubuntu#<ip>
Query and result:
query <- "SELECT column_name FROM table_name;"
result <- dbGetQuery(con, query)
When I am calling the query directly, it is executed as intended.
When I am calling it from a method in a package, I am getting the following error:
Error in .local(conn, statement, ...) :
could not run statement: Unknown MySQL error
I am not sure why I am seeing the error.
EDIT:
method <- function(con, query) {
output <- dbGetQuery(con, query)
return(output)
}
result <- package::method(con, query)

try this, I removed some parameters from you code.
mydb = dbConnect(MySQL(), user='root', password='password', dbname='my_database', host='localhost')

Related

How to connect to sql database located locally in r shiny

I am trying to connect to SQL database located in the same directory as in my app.R file but I get the following error if I try to connect.
con <- dbConnect(RMySQL::MySQL(), dbname = "super_data")
Error in .local(drv, ...) :
Failed to connect to database: Error: Can't connect to MySQL server on 'localhost' (0)
mydb = dbConnect(MySQL(), user='root', password='', dbname='super_data.sql', host='localhost')
Error in .local(drv, ...) :
Failed to connect to database: Error: Can't connect to MySQL server on 'localhost' (0)
Try with the following code:
conn <- RMySQL::dbConnect(RMySQL::MySQL(),
user = "root",
password = "mypassword",
dbname = "mydatabase",
host = "127.0.0.1", # Instead of localhost
port = 3306) # Default MySQL port
Hope this can help

unable to find an inherited method for function ‘dbReadTable’ for signature ‘"character", "missing"’

I am trying to read the table data in R with MySQL package.
Getting error:
unable to find an inherited method for function ‘dbReadTable’ for signature ‘"character", "missing"’
code:
library(RMySQL)
library(dbConnect)
mydb = dbConnect(MySQL(), user='user',
password='password', dbname='blank_copy',
host='ip address', port = port_number)
dbListTables(mydb)
dbListFields(mydb, 'SELECT * FROM table_name')
dbReadTable("table_name")
the DBIConnection is missing
dbReadTable(mydb,"table_name")

Connecting MySQL with R

This is how I am trying to connect MySQL to R.
db <- dbConnect(MySQL(), user='username', password='pwd',dbname=dbx, host = 'local', port = 3306)
But I'm getting this error:
Error in .local(drv, ...) : Failed to connect to database: Error:
Plugin caching_sha2_password could not be loaded: The specified module
could not be found.
What to do?
You can create a function to retrieve the query.
library(RMySQL)
sqlQuery <- function (query) {
# creating DB connection object with RMysql package
DB <- dbConnect(MySQL(), user="user", password="password",
dbname="databaseName", host="host")
# close db connection after function call exits
on.exit(dbDisconnect(DB))
# send Query to obtain result set
rs <- dbSendQuery(DB, query)
# get elements from result sets and convert to dataframe
result <- fetch(rs, -1)
# return the dataframe
return(result)
}
And then just:
new_dataframe <- sqlQuery("SELECT * from table")
Hope that helps

R and MySQL set up

So I'm trying to set up a MySQL database in R, and after reading through many tutorials and StackOverflow posts I'm still not able to setup a connection. I'm retrieving financial data for healthcare companies via the Quandl web API, and want to store some of the data in a SQL database and retrieve. Here is my code:
#Install and load Quandl package
install.packages("Quandl")
library(Quandl)
#Install and load SQL package
install.packages("RMySQL")
library(RMySQL)
#Authenicate API key
Quandl.api_key("________")
#Load top 10 biggest Healthcare compaines in Fortune 500 according to
#http://fortune.com/2015/06/20/fortune-500-biggest-healthcare-companies/
CVS <- Quandl("WIKI/CVS")
McKesson <- Quandl("WIKI/MCK")
UnitedHealthGroup <- Quandl("WIKI/UNH")
AmerisourceBergern <- Quandl("WIKI/ABC")
ExpressScriptsHolding <- Quandl("WIKI/ESRX")
CardinalHealth <- Quandl("WIKI/CAH")
Walgreens <- Quandl("WIKI/WBA")
Johnson_Johnson <- Quandl("WIKI/JNJ")
Anthem <- Quandl("WIKI/ANTM")
Aetna <- Quandl("WIKI/AET")
con <- dbConnect(MySQL(),
user = 'root',
password = 'password',
host = 'localhost',
dbname = 'healthcare')
I've installed mysql-5.7.17-winx64 and it's in the same directory that I'm working in in RStudio. When I try to run the dbConnect() command, I get an error saying:
'Error in .local(drv, ...) :
Failed to connect to database: Error: Can't connect to MySQL server on 'localhost' (0)'
Is there something I need to do with the SQL files? What is going wrong?

RMySQL does not work as part of a script

When I run
library(RMySQL)
mydb = dbConnect(MySQL(), user = "XX", password = "XX", dbname = "XX", host = "XX")
on the R console it works,
however when I put save it as a yy.R file and run it as R CMD BATCH yy.R, or as source("yy.R") it says
Error in mysqlNewConnection(drv, ...) :
RS-DBI driver: (Failed to connect to database: Error: Can't connect to MySQL server on 'XX' (111)
)
Calls: dbConnect -> dbConnect -> mysqlNewConnection -> .Call
Execution halted
could I check if RMySQL only runs on the console? Thanks!
Perhaps you can specify the driver with RMySQL::MySQL(), this works for me in scripts.