I would like to connect MySQL with the Shiny (R). I have been trying to run the code since last couple of days but it is not working. Following is my code:
library(shiny)
library(leaflet)
library(DBI)
library(RMySQL)
saveData <- function(firstname, lastname) {
db <- dbConnect(MySQL(), dbname = "example", host = "localhost",
port = 3306, user = "root",
password = "xxxx")
query <- sprintf(
"INSERT INTO %s (%s) VALUES ('%s, %s')",
name,
paste(names(firstname), collapse = ", "),
paste(data, collapse = "', '"),
paste(names(lastname), collapse = ", "),
paste(data, collapse = "', '")
)
dbGetQuery(db, query)
dbDisconnect(db)
}
ui <- fluidPage(
textInput("Firstname", "Enter your first name:", ""),
textInput("Surname", "Enter your last name:", ""),
actionButton("UpdateView", icon("refresh"))
)
server <- function(input, output, session) {
isolate(observeEvent(input$UpdateView, saveData(input$Firstname,
input$Surname)))
}
shinyApp(ui, server)
Error in .local(drv, ...) :
Failed to connect to database: Error: Lost connection to MySQL server at 'reading authorization packet', system error: 10060
Stack trace (innermost first):
72: .local
71: dbConnect
70: dbConnect
69: saveData [#3]
68: observeEventHandler [#8]
4: <Anonymous>
3: do.call
2: print.shiny.appobj
1: <Promise>
ERROR: [on_request_read] connection reset by peer
The following error is consistent. I tried to google it and I found that Firewall might be the one causing this trouble but the steps are not provided about what settings to change in the Firewall. If the solution is something else than I am open to making those changes. Thanks in advance
Related
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')
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")
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
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?
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.