RMySQL Database connection

2.2k Views Asked by At

I'm trying to set up the initial database connection between R and MySQL. I installed RMySQL. When I try to connect to my database, this is the error I receive.

Any direction in fixing it is much appreciated! The package is still quite new so help on this is a little difficult to find. (I'm also new to R).

con <- dbConnect(RMySQL::MySQL(), group = "Mydatabase") 

Error in .local(drv, ...) Failed to connect to database Error: Can't connect to MySQL server on 'localhost' (0)

1

There are 1 best solutions below

3
On

You have to make sure you have a proper MySQL database set up and you need to pass the config options of that database to the dbConnect call. Is there a MySQL database on that machine?

I think if you use the group parameter, then RMySQL will assume you have a configuration file with settings for a Mydatabase database. I forget how to set up that file, but this is how I connect to RMySQL:

  db <- dbConnect(MySQL(), dbname = DB_NAME,
                  host = options()$mysql$host,
                  port = options()$mysql$port,
                  user = options()$mysql$user,
                  password = options()$mysql$password)

(in my .Rprofile I set up all these options). After you have all these values (dbname, host, port, user, pw) for your database, try running this command