3

I'm learning R language. I want to establish a connection with MySQL using R. I've studied how to do from here, here, tutorial and from many other websites. I've followed all the steps to connect with MySQL, but still I'm unable to connect with MySQL, and getting an error:

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

Here is screenshot of what I did: work done and error

I'm using R version-3.2.2 and MySQL version-5.6 and MySQL database is running on localhost:1527. Please help me to remove the error and tell me what I'm doing wrong.

INFO: I'm able to connect above MySQL database with java.

Sachin Kumar
  • 781
  • 1
  • 9
  • 28
  • 2
    Please read [this SO post](http://stackoverflow.com/questions/10292326/how-to-connect-r-with-mysql-or-how-to-install-rmysql-package), as there appears to be a bit more configuration than what you have done above. – Tim Biegeleisen Sep 30 '15 at 06:42
  • 1
    Intuitively, it should make sense that the R package won't know where to look for your MySQL database out of the box without you telling it where to look. – Tim Biegeleisen Sep 30 '15 at 06:42
  • Do you mean I've to set classpath variables. (Sorry for asking silly question) – Sachin Kumar Sep 30 '15 at 06:45
  • 1
    Your question isn't silly actually. Yes, I believe you have to configure some environment variables. Work through that post and then come back here if you still can't get it to work. It's good that your JDBC code works, because at least we know that there is nothing wrong with your MySQL instance. – Tim Biegeleisen Sep 30 '15 at 06:46
  • 1
    maybe run `Sys.getenv()` to see the `MySQL` path – Keniajin Sep 30 '15 at 07:10
  • @TimBiegeleisen I've followed all the steps mentioned in your link but still can't... – Sachin Kumar Sep 30 '15 at 07:24
  • What output do you get in R console from this: `Sys.getenv(c("R_HOME", "MYSQL_HOME", "PATH"))` ? – Tim Biegeleisen Sep 30 '15 at 07:32
  • I got following output R_HOME "C:/PROGRA~1/R/R-32~1.2" MYSQL_HOME "C:\\\\Program Files\\\\MySQL\\\\MySQL Server 5.6\"" along with my complete path – Sachin Kumar Sep 30 '15 at 09:48
  • @TimBiegeleisen thanks for ur support... – Sachin Kumar Sep 30 '15 at 10:00

1 Answers1

2

you say that your mysql is running on localhost:1527 - but you don't specify that port in dbConnect, then it probably uses the default port, 3306, so it should not be able to connect... add the port argument to your dbConnect

dbConnect(MySQL(), user="user", password="password", dbname="dbname", host="localhost", port="1527")
anders
  • 814
  • 7
  • 12