How to Connect to MySQL with R in Wndows (using RMySQL)

This is a quick step-by-step guide on how to connect to MySQL via R. If your wondering why you might want to do that, think of all the math that could be done in R, the results of which could be saved into a MySQL DB.

RMySQL is the R package that you will need to connect to MySQL; it was made by Omegahat.

Steps:

  1. Open RGui (Assuming you have installed R already).
  2. Goto Packages -> Install Package(s)...
  3. Pick a server at a location near by.
  4. Choose 'RMySQL' and install.
  5. Run the following commands to connect:

> library(RMySQL)
> drv = dbDriver("MySQL")
> con = dbConnect(drv,host="mars",dbname="sat133",user="sat133",pass="T0pSecr3t")
> album = dbGetQuery(con,statement="select * from tasks")
> a

In the query above, if the host, database, username, and password all exist, then the code above should return a table of results =]

© Erik Vold 2007-2012. Contact Erik Vold. Top ^