简体   繁体   中英

RMySQL cannot connect to server from R

I want to connect to a MySQL database from R and I'm having some troubles. It is impossible to provide a reproducible example, but I hope someone can spot any mistake that I might be making.

This is the code I'm running in R:

library(methods)
library(DBI)
library(RMySQL)
DB_name = "myDBname"
drv <- dbDriver("MySQL")
con <- dbConnect(drv, user="myUser", password="myPassword", dbname="myDBname", 
                 host="my.host.something")

This is the error that I get:

Error in mysqlNewConnection(drv, ...) : 
  RS-DBI driver: (Failed to connect to database: Error: Can't connect to MySQL server on 'my.host.something' (110)

I'm able to connect from the command line without any problem.

Thanks!

First, create a conf file

gedit ~/.my.cnf
[someName]
user = myUser
password = myPassword
host = my.host.something
port=3306

Second, In R

library(methods)
library(DBI)
library(RMySQL)
DB_name = "myDBname"
drv <- dbDriver("MySQL")
  con <- dbConnect(drv, group= "someName", dbname=DB_name)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM