RMySQL does not work as part of a script - mysql

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.

Related

R - Error trying to connect to a MySQL server

I'm trying to connect to a MySQL database using this code:
con <- dbConnect(RMySQL::MySQL(),
dbname = "XX",
host = "YY",
port = ZZ,
user = "AA",
password = "BB"
)
And I'm getting this error:
Error in .local(drv, ...) :
Failed to connect to database: Error: Can't connect to MySQL server on 'YY' (0)
I've tried with this other code:
pool <- dbPool(drv = RMySQL::MySQL(),
dbname = "XX",
host = "YY",
username = "AA",
password = "BB",
port = ZZ
)
And get the same error:
Error in .local(drv, ...) :
Failed to connect to database: Error: Can't connect to MySQL server on 'YY' (0)
I can connect to this database using Workbench, so I guess it's a problem with R.
Any solution to this problem?
Edit: something I didn't say, the server is remote, so I had to use a SSH key to make the tunnel to the server to be able to access from Workbench. So maybe this should be considered to solve the issue.

Getting Unknown MySQL Error while executing queries

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')

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

How to connect with MySQL from R?

I am trying to connect with MySQL from R (version R-3.3.3). Unfortunately, I get the error. I tried to change the version to R-3.4.0 but the error is persistent.
library(DBI)
library(RMySQL)
db <- dbConnect(MySQL(), dbname = "example", host = "localhost",
port = 3306, user = "root",
password = "****")
Error: Error in .local(drv, ...) : Failed to connect to database:
Error: Lost connection to MySQL server at 'reading authorization
packet', system error: 10060
Please help!

Cannot connect to MySql through R. (Error with host argument)

I opened SQL clicked on the database I wanted (named bb) then at the top I choose Database then Manage Connections. This showed me the Hostname as 127.0.0.1, port as 3306, username as root and there should be no password. Plugged that into to dbConnect and it is giving the following errors
This is the code I have and I am trying to open a connection.
> con <- dbConnect(RMySQL::MySQL(), dbname = "bb", username = "root", host =
"127.0.0.1:3306")
Error in .local(drv, ...) :
Failed to connect to database: Error: Unknown MySQL Server Host
'127.0.0.1:3306' (11001)
> con <- dbConnect(RMySQL::MySQL(), dbname = "bb", username = "root", host =
"127.0.0.1", port = 3306)
Error in .local(drv, ...) :
Failed to connect to database: Error: Can't connect to MySQL server on
'127.0.0.1' (0)
> con <- dbConnect(RMySQL::MySQL(), dbname = "bb", username = "root", host =
"127.0.0.1", port = "3306")
Error in .local(drv, ...) : Argument port must be an integer value
All of them are returning errors. I don't know why it is not working.
Please Help and Thank you.