无法运行语句:查询期间与MySQL服务器的连接丢失

时间:2018-05-28 13:37:04

标签: r mariadb rmysql

将R数据帧传输到MySQL(MariaDB)数据库表,我收到以下错误:Lost connection to MySQL server during query

可以使用此命令

在R中加载示例数据
cntxt <- read.delim("http://ec.europa.eu/eurostat/estat-navtree-portlet-prod/BulkDownloadListing?sort=1&file=comext%2FCOMEXT_METADATA%2FCLASSIFICATIONS_AND_RELATIONS%2FENGLISH%2FCN.txt", header = FALSE, quote = "", stringsAsFactors = FALSE)

我使用RMySQL包将数据框传输到数据库:

con <-  RMySQL::dbConnect(RMySQL::MySQL(), dbname = "test")    
RMySQL::dbWriteTable(con, "cntxt", cntxt, row.names = FALSE, overwrite = TRUE)

数据库写操作在我的笔记本电脑上可以正常使用任何大小的表。但是在服务器上它会返回错误。只有足够大的表(1000行以上)才会出现错误:

dbWriteTable()成功获得1000行数据

RMySQL::dbWriteTable(con, "cntxt", head(cntxt,1000), row.names = FALSE, overwrite = TRUE)
# [1] TRUE

dbWriteTable()无法获得2000行数据

RMySQL::dbWriteTable(con, "cntxt", head(cntxt,2000), row.names = FALSE, overwrite = TRUE)
# Error in .local(conn, statement, ...) : 
#  could not run statement: Lost connection to MySQL server during query

基于related questions,我检查了max_allowed_pa​​cket的值:

mysql> SHOW VARIABLES LIKE 'max_allowed_packet';
| max_allowed_packet | 16777216 |

16Mb对于2000行数据应该足够了。 错误来自哪里? 在mysql中没有任何可见的错误日志/var/log/mysql/error.log

服务器版本:10.1.26-MariaDB-0 + deb9u1 Debian 9.1

1 个答案:

答案 0 :(得分:1)

RMySQL package替换为较新的RMariaDB package

install.packages("RMariaDB")

然后可以再次传输超过2000行的表。

con <- RMariaDB::dbConnect(RMariaDB::MariaDB(), dbname="test")
cntxt <- read.delim("http://ec.europa.eu/eurostat/estat-navtree-portlet-prod/BulkDownloadListing?sort=1&file=comext%2FCOMEXT_METADATA%2FCLASSIFICATIONS_AND_RELATIONS%2FENGLISH%2FCN.txt", header = FALSE, quote = "", stringsAsFactors = FALSE)
RMariaDB::dbWriteTable(con, "cntxt", cntxt, row.names = FALSE, overwrite = TRUE)