Error: R cannot connect to MySQL

2019-06-19 12:06发布

I updated MySQL to latest version 8.0.11 yesterday and tried to use RMySQL to connect to MySQL, but it was not working and I could not find any solution.

My code:

library(RMySQL) 
con <- dbConnect(MySQL(), user="hello", password="hi", dbname = "webscrape", host="xx.xxx.xxx.xxx", port=xxxx)

Error:

Error in .local(drv, ...) : 
  Failed to connect to database: Error: Can't initialize character set unknown (path: compiled_in)

标签: mysql r rmysql
5条回答
走好不送
2楼-- · 2019-06-19 12:39

I had this problem as well and I noticed in MySQL 5.8, which was released recently, the default character set of the server and client connections have now been set to utf8mb4 by default. With MySQL 5.7 the default character set was set to utf8.

I tried playing around with the character sets of the server and and client, reverting the values to utf8 but I just couldn't get it to work so I reverted my installed MySql to 5.7. RMySQL worked then after.

It feels like RMySQL cannot support utf8mb4 at the moment and needs to be updated.

查看更多
兄弟一词,经得起流年.
3楼-- · 2019-06-19 12:44

Had the same problem. Tried changing character sets on the schema. Also tried reinstalling MySQL with the legacy authentication method option and still no luck with RMySQL.

Tried RMariaDB package and it worked. I would switch to RMariaDB rather than downgrading MySQL.

https://cran.r-project.org/web/packages/RMariaDB/

查看更多
太酷不给撩
4楼-- · 2019-06-19 12:46

Related to this problem, one solution that would prevent you from changing your R preferred library and/or downgrading MySQL can be found at this post.

You just need to change your my.cnf settings.

查看更多
SAY GOODBYE
5楼-- · 2019-06-19 12:54

I solved this problem like this:

User Windows

1.Error in .local(drv, ...) : Failed to connect to database: Error: Can't initialize character set unknown (path: compiled_in)

After adding these lines:

file path: %PROGRAMDATA%\MySQL\MySQL Server X.X\my.ini

    [mysqld]
    collation-server = utf8_unicode_ci
    init-connect='SET NAMES utf8'
    character-set-server = utf8
    skip-character-set-client-handshake

    [client]
    default-character-set   = utf8

    [mysql]
    default-character-set   = utf8

2- Authentication plugin 'caching_sha2_password' cannot be loaded

    ALTER USER 'yourusername'@'localhost' IDENTIFIED WITH mysql_native_password BY 'youpassword';

References:

MySQL my.ini location

Change MySQL default character set to UTF-8 in my.cnf?

Authentication plugin 'caching_sha2_password' cannot be loaded

查看更多
Emotional °昔
6楼-- · 2019-06-19 13:00

I have the same Pb :

Error in .local(drv, ...) : 
  Failed to connect to database: Error: Can't initialize character set unknown (path: compiled_in)

I try the solution of Hugo (modify the my.ini), David Guerin (using RMariaDB) but both are not working. After some research and experience this solution is working for me

Modify my.ini (this is the path to find C:\ProgramData\MySQL\MySQL Server 8.0) :

By opposit to the answer of Hugo, you need to put utf8mb4 (utf8 is not the good one)

[client]

default-character-set = utf8 (the old line "utf" to "utfmb4")
default-character-set = utf8mb4 (the new line with modification)

[mysql]

default-character-set = utf8 (the old line "utf" to "utfmb4")
default-character-set = utf8mb4 (the new line with modification)

[mysqld]

character-set-client-handshake = FALSE  (new line to add)
character-set-server = utf8mb4  (the old line "utf" to "utfmb4")        
collation-server = utf8mb4_unicode_ci   (new line to add)
skip-character-set-client-handshake (Line to remove)

In mysql, you need to change your password :

ALTER USER root@localhost IDENTIFIED WITH mysql_native_password BY 'mot-passe';
查看更多
登录 后发表回答