【问题标题】:Connection lost when connect R to MySQL using collect使用 collect 将 R 连接到 MySQL 时连接丢失
【发布时间】:2017-11-05 03:04:51
【问题描述】:

我想使用dplyrRMySQL 来处理我的大数据。 dplyr 代码没有问题。问题(我认为)是关于将数据从MySQL 导出到R。每次即使我在collect 中使用n=Inf,我的连接也会断开。从理论上讲,我的数据应该有超过 50K 的行,但我只能得到大约 15K。任何建议表示赞赏。

方法一

library(dplyr)
library(RMySQL)

# Connect to a database and select a table 
my_db <- src_mysql(dbname='aermod_1', host = "localhost", user = "root", password = "")   
my_tbl <- tbl(my_db, "db_table") 
out_summary_station_raw <- select(my_tbl, -c(X, Y, AVERAGE_CONC))
out_station_mean_local <- collect(out_summary_station_raw)

方法二:使用Pool

library(pool)
library(RMySQL)
library(dplyr)

pool <- dbPool(
  drv = RMySQL::MySQL(),
  dbname = "aermod_1",
  host = "localhost",
  username = "root",
  password = ""
)

out_summary_station_raw <- src_pool(pool) %>% tbl("aermod_final") %>% select(-c(X, Y, AVERAGE_CONC))

out_station_mean_local <- collect(out_summary_station_raw, n = Inf)

警告信息(两种方法):

Warning messages:
1: In dbFetch(res, n) : error while fetching rows
2: Only first 15,549 results retrieved. Use n = Inf to retrieve all. 

更新:

检查了日志,从服务器端看起来很好。在我的示例中,slow-log 表示 Query_time: 79.348351 Lock_time: 0.000000 Rows_sent: 15552 Rows_examined: 16449696,但 collect 无法检索完整数据。我可以使用MySQL Bench 复制相同的动作。

【问题讨论】:

  • 您确定这是 R 问题而不是 MySQL 问题吗?
  • @ssdecontrol,这是可能的。或者更准确地说,如何通过 R 配置几个 MySQL 参数?例如,我在Workbench 中增加了DBMS connection read time out,但不确定通过R 的等效方式是什么。
  • 我认为配置客户端超时的唯一方法是更改​​ my.cnf 文件中的相关选项。
  • @ssdecontrol,感谢您的建议。将interactive_timeout = 60000 wait_timeout = 60000 net_read_timeout = 60000 connect_timeout = 60000 添加到my.ini 并验证这些变量正在使用中。但仍然可以获得完整的数据...
  • 好的,太好了。您还必须考虑 server 端超时(如果有)。也许您可以检查来自mysqld 的调试输出以获取线索。否则,这个问题似乎无法回答,因为我从来没有遇到过这个问题,也不知道如何轻松地重现它。

标签: mysql r dplyr rmysql


【解决方案1】:

在最近一次 RMySQL 更新之后,我注意到我无法 collect() 来自大视图的数据,我将其报告为 an issue。您的问题可能与此有关。

要尝试的一件事是回滚到上一个版本。

devtools::install_version("RMySQL", version = "0.10.9", 
                          repos = "http://cran.us.r-project.org")

【讨论】:

  • 非常感谢!这确实解决了问题。
【解决方案2】:

【讨论】:

  • 感谢您的建议。刚刚从 0.5.0 升级到 0.7.0 但仍然遇到同样的错误。 attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] pool_0.1.0 RMySQL_0.10.11 DBI_0.6-1 dplyr_0.7.0 loaded via a namespace (and not attached): [1] magrittr_1.5 assertthat_0.1 R6_2.2.1 tools_3.3.2 glue_1.0.0 [6] tibble_1.3.3 Rcpp_0.12.9 rlang_0.1.1 dbplyr_1.0.0
猜你喜欢
  • 2021-08-22
  • 2019-12-31
  • 2020-09-21
  • 2016-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-11
  • 1970-01-01
相关资源
最近更新 更多