3

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

Cricketer
  • 399
  • 1
  • 3
  • 20

0 Answers0