如何将R连接到MySQL?无法连接到数据库:错误:无法加载插件caching_sha2_password

时间:2019-01-08 21:07:08

标签: mysql r rmysql r-dbi

我最近在计算机上安装了MySQL,并试图将RStudio连接到MySQL。我遵循了书中的说明以及说明here。但是,每当我在dbConnect()中使用src_mysqlRStudio时,都会出现以下错误消息:

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

例如,我可以使用Windows中的命令提示符登录

mysql -u username -p

并按如下所示创建数据库

CREATE DATABASE myDatabase;

,然后在RStudio中:

library(RMySQL)
db <- dbConnect(MySQL(), dbname = "myDatabase", user = "username", 
           password = "password", host = "localhost")

,我的回答始终是上面列出的错误消息。

如果需要的话: sessionInfo()

R version 3.5.2 (2018-12-20)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

2 个答案:

答案 0 :(得分:5)

R mysql库取决于libmysqlclient / libmariadbclient。缺少的caching_sha2_password似乎表明未安装旧的mysqlclient版本或libmariadbclient。 caching_sha2_password get added to mariadb (3.0.8)

一种替代方法,例如this answer,是在mysql中更改用户以使用其他身份验证机制:

您将用户设置回mysql_native_password:

ALTER USER 'username'@'localhost'
IDENTIFIED WITH mysql_native_password BY 'password'

要将所有新创建用户的默认设置更改为my.cnf / my.ini设置default_authentication_plugin=mysql_native_password

答案 1 :(得分:0)

第1步:打开mySql 8.0命令客户端

第2步:要列出数据库中的所有用户,请键入以下命令, select host,user from mysql.user;

第3步:现在将当前用户密码重置为,
set password for 'root'@'localhost'='yourpassword';

第3步:最后一步

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'yourpassword'; 查询正常,受影响的0行(0.12秒)