使用RMySQL关闭活动连接

使用RMySQL关闭活动连接,mysql,r,Mysql,R,根据,我怀疑我有一个未关闭的连接问题,它阻止数据注入我的MySQL数据库。数据被允许进入当前未使用的表中(因此我怀疑许多打开的连接阻止了上传到该特定表中) 我在Ubuntu服务器上使用RMySQL将数据上传到MySQL数据库 我正在寻找一种方法a)确定连接是否打开b)关闭连接(如果打开)。来自SQL命令行的命令exec sp_who和exec sp_who2返回SQL代码错误 另一个注意事项:我能够连接、完成上传过程并成功结束R过程,并且当我仅尝试该表时,服务器上没有数据(通过SQL命令行检查)

根据,我怀疑我有一个未关闭的连接问题,它阻止数据注入我的MySQL数据库。数据被允许进入当前未使用的表中(因此我怀疑许多打开的连接阻止了上传到该特定表中)

我在Ubuntu服务器上使用RMySQL将数据上传到MySQL数据库

我正在寻找一种方法a)确定连接是否打开b)关闭连接(如果打开)。来自SQL命令行的命令
exec sp_who
exec sp_who2
返回SQL代码错误

另一个注意事项:我能够连接、完成上传过程并成功结束R过程,并且当我仅尝试该表时,服务器上没有数据(通过SQL命令行检查)


(顺便问一下,如果所有其他方法都失败了,那么简单地删除该表并创建一个同名的新表可以解决问题吗?这会很痛苦,但可行。)

a
dbListConnections(dbDriver(drv=“MySQL”))

b
dbDisconnect(dbListConnections(dbDriver(drv=“MySQL”))[[要关闭的MySQLConnection索引])
。关闭所有:
lappy(dbListConnections(dbDriver(drv=“MySQL”)),dbDisconnect)

是的,您可以重写该表,当然会丢失所有数据。或者您可以指定
dbWriteTable(,…,overwrite=TRUE


我还会使用其他选项,如
行.名称
标题
字段.类型
引用
sep
下线
。我在RMySQL中也有很多奇怪的行为。我记不清具体细节,但似乎在我做错事情时没有收到错误消息,比如忘记设置row.names。HTH

关闭所有活动连接:

dbDisconnectAll <- function(){
  ile <- length(dbListConnections(MySQL())  )
  lapply( dbListConnections(MySQL()), function(x) dbDisconnect(x) )
  cat(sprintf("%s connection(s) closed.\n", ile))
}

dbDisconnectAll关闭连接

您可以将dbDisconnect()与dbListConnections()一起使用,以断开RMySQL正在管理的那些连接:

    all_cons <- dbListConnections(MySQL())
    for(con in all_cons) 
      dbDisconnect(con)
您还可以终止允许的任何连接(不仅仅是由RMySQL管理的连接):

mydb所在的位置

    mydb = dbConnect(MySQL(), user='user_id', password='password', 
                      dbname='db_name', host='host')
关闭特定的连接

    dbGetQuery(mydb, "kill 2")
    dbGetQuery(mydb, "kill 5")
最简单的:

lapply(dbListConnections( dbDriver( drv = "MySQL")), dbDisconnect)
列出所有连接并按Lappy断开连接在当前版本中,“dbListConnections”功能已被弃用,DBI不再要求驱动程序维护连接列表。因此,上述解决方案可能不再有效。例如,在RMariaDB中,上述解决方案会产生错误

lapply(dbListConnections(MySQL()), dbDisconnect)
我使用了以下替代方案,该方案使用MySQL服务器的功能,并且应适用于当前的DBI/驱动程序版本:

### listing all open connection to a server with open connection
query <- dbSendQuery(mydb, "SHOW processlist;")
processlist <- dbFetch(query)
dbClearResult(query)

### getting the id of your current connection so that you don't close that one
query <- dbSendQuery(mydb, "SELECT CONNECTION_ID();")
current_id <- dbFetch(query)
dbClearResult(query)

### making a list with all other open processes by a particular set of users
# E.g. when you are working on Amazon Web Services you might not want to close 
# the "rdsadmin" connection to the AWS console. Here e.g. I choose only "admin" 
# connections that I opened myself. If you really want to kill all connections,
# just delete the "processlist$User == "admin" &" bit.
queries <- paste0("KILL ",processlist[processlist$User == "admin" & processlist$Id != current_id[1,1],"Id"],";")

### making function to kill connections
kill_connections <- function(x) {
  query <- dbSendQuery(mydb, x)
  dbClearResult(query)
}

### killing other connections
lapply(queries, kill_connections)

### killing current connection
dbDisconnect(mydb)
####列出与具有打开连接的服务器的所有打开连接

查询您可以尝试
显示完整的进程列表
查看打开的连接。请解释为什么您认为此解决方案能够造福社区。此答案比公认的答案要好得多
lapply(dbListConnections(MySQL()), dbDisconnect)
### listing all open connection to a server with open connection
query <- dbSendQuery(mydb, "SHOW processlist;")
processlist <- dbFetch(query)
dbClearResult(query)

### getting the id of your current connection so that you don't close that one
query <- dbSendQuery(mydb, "SELECT CONNECTION_ID();")
current_id <- dbFetch(query)
dbClearResult(query)

### making a list with all other open processes by a particular set of users
# E.g. when you are working on Amazon Web Services you might not want to close 
# the "rdsadmin" connection to the AWS console. Here e.g. I choose only "admin" 
# connections that I opened myself. If you really want to kill all connections,
# just delete the "processlist$User == "admin" &" bit.
queries <- paste0("KILL ",processlist[processlist$User == "admin" & processlist$Id != current_id[1,1],"Id"],";")

### making function to kill connections
kill_connections <- function(x) {
  query <- dbSendQuery(mydb, x)
  dbClearResult(query)
}

### killing other connections
lapply(queries, kill_connections)

### killing current connection
dbDisconnect(mydb)