简体   繁体   中英

RMySQL error connecting to Mysql Database

I'm starting learning to use MySQL and, more specifically, I'm trying to learn how to connect and make it interact with Access and R. I recently create an odbc connecting Access to R. I successfully used R libraries to retrieve it and use sql queries. So I moved to R + Mysql. Here starts my problem! After installing RODBC I installed RMySQL and tried to use "dbConnect". Here follows my code:

con <- dbConnect(dbDriver("MySQL"), user = "root", password = "mypwd", dbname = "mydbname")

where "mypwd" it's the password I inserted when created my MySQL ODBC Unicode Driver and "mydbname" it's the database name I connected. Just for sake of information, I already tested this odbc by transfering tables from Access and Mysql. So my doubts are related only to connection between Mysql and R.

So, let's see my error when running the code line above:

Error in .local(drv, ...) : 
  Failed to connect to database: Error: Plugin caching_sha2_password could not be loaded

I already read other posts about the need to change the Preferred Authentication Plugin. I found this line code as solution:

ALTER USER user
  IDENTIFIED WITH mysql_native_password 
  BY 'password';

So....please, tell me if I should change this code line into:

ALTER USER root
  IDENTIFIED WITH mysql_native_password 
  BY 'mypwd';

Is it correct? Or I completely missed the point. Sorry for my question but as I said, I'm a completely newbie. Thanks for your patience

Where is your host address ?

Try the code something like this

con <- dbConnect(dbDriver("MySQL"), host="192.100.XX.XX",db="df", user="root", password="mypwd")

Ok it seems I found out a workaround for my problem.

Instead of using "dbConnect" I used "odbcConnect". Here follows my working code:

channel <- odbcConnect("Myodbcname", uid="root")
df <- sqlQuery(channel, "SELECT * from mytable", as.is = TRUE)

"Myodbcname" is the name I saved for my DSN Name and DSN System. So, now I can use my sql queries on my table and work on it.

At this point I need to understand the difference between the function "odbcConnect" and "dbConnect". Because if I don't need the latter I can ignore for the moment. Maybe I have to open a new thread with a link to this one. Not sure what Stackoverflow prefers me to do.

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