Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connect to MySQL database with RMySQL

Tags:

r

rmysql

I am making the move from RSQLite to RMySQL and I am confused by the user and password fields. FWIW, I'm running Windows 7, R 2.12.2, MySQL 5.5 (all 64 bit), and RMySQL 0.7-5.

I installed RMySQL as prescribed in this previous SO question, and as far as I know it works (i.e., I can load the package with library(RMySQL)). But when I try to run the tutorial from the R data import guide, I get a "failed to connect to database..." error. This is the code from the tutorial from the guide:

library(RMySQL) # will load DBI as well
## open a connection to a MySQL database
con <- dbConnect(dbDriver("MySQL"), user = "root", password = "root", dbname = "pookas")
## list the tables in the database
dbListTables(con)
## load a data frame into the database, deleting any existing copy
data(USArrests)
dbWriteTable(con, "arrests", USArrests, overwrite = TRUE)
dbListTables(con)
## get the whole table
dbReadTable(con, "arrests")
## Select from the loaded table
dbGetQuery(con, paste("select row_names, Murder from arrests",
                      "where Rape > 30 order by Murder"))
dbRemoveTable(con, "arrests")
dbDisconnect(con)

On the second line I get the following error:

> con <- dbConnect(dbDriver("MySQL"), user = "richard", password = "root", dbname = "pookas")
Error in mysqlNewConnection(drv, ...) : 
  RS-DBI driver: (Failed to connect to database: Error: Access denied for user 'richard'@'localhost' (using password: NO)
)

I have tried with and without user and password and with admin as user. I have also tried using a dbname that I made before with the command line and with one that doesn't exist.

Any tips? Is there a good reference here? Thanks!

like image 277
Richard Herron Avatar asked Mar 25 '11 11:03

Richard Herron


People also ask

Can we connect MySQL with Visual Studio?

To create a connection to an existing MySQL database: Start Visual Studio and open the Server Explorer by clicking Server Explorer from the View menu. Right-click the Data Connections node and then select Add Connection.

Can you use Heroku with MySQL?

Heroku does not offer a native MySQL add-on but instead supplies it through a third party, ClearDB. If it has not been added to your application already, you can install it from the Heroku Add-Ons Page. Once installed, it will appear in your Add-Ons list in your Resources tab as ClearDB MySQL .


1 Answers

That is most likely a setup issue on the server side. Make sure that networked access is enabled.

Also, a local test with the command-line client is not equivalent as that typically uses sockets. The mysql server logs may be helpful.

like image 74
Dirk Eddelbuettel Avatar answered Oct 06 '22 05:10

Dirk Eddelbuettel