[英]R SSH tunnel to MySQL database via Shiny App Access Denied or Can't connect to MySQL server on '127.0.0.1'
I've looked through other posts without success, so here we are.我已经浏览了其他帖子但没有成功,所以我们到了。 The following code snippet runs as expected on a Windows 10 laptop in RStudio.以下代码片段在 RStudio 中的 Windows 10 笔记本电脑上按预期运行。
library(RMySQL)
library(ssh)
library(callr)
rs <- r_session$new(wait = TRUE, wait_timeout = 3500)
rs$call(function(){
session <- ssh::ssh_connect('<ssh-user>@<remote-ip-address>', keyfile = "<path-to-.pem-file>", verbose = FALSE)
ssh::ssh_tunnel(session, port=3306, target = "127.0.0.1:3306")
})
# Connect to TRAQ DB.
Sys.sleep(1)
db-connection <- dbConnect(MySQL(), user = '<remote-user-name>', password = '<remote-password>', dbname = '<remote-database>', port=3306, host='127.0.0.1')
The snippet runs runs successfully too when dbConnect() has host='localhost'当 dbConnect() 具有 host='localhost' 时,该代码段也会成功运行
I am attempting to run the snippet on a virtual machine running Ubuntu 22 within an R shiny app.我正在尝试在 R shiny 应用程序中运行 Ubuntu 22 的虚拟机上运行该片段。 I receive the following messages while trying to load the app我在尝试加载应用程序时收到以下消息
Using localhost:使用本地主机:
ERROR 1044 (42000): Access denied for user '<remote-user-name>'@'localhost'
Using 127.0.0.1:使用 127.0.0.1:
ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1' (111)
I have received the (115) error code too.我也收到了(115)错误代码。
If run directly from the R terminal, the snippet must be modified as follows for success.如果直接从 R 终端运行,则必须将代码段修改如下才能成功。
ssh::ssh_tunnel(session, port=3306, target = "127.0.0.1:3306")
to至
ssh::ssh_tunnel(session, port=3307, target = "127.0.0.1:3306")
AND和
db-connection <- dbConnect(MySQL(), user = '<remote-user-name>', password = '<remote-password>', dbname = '<remote-database>', port=3306, host='127.0.0.1')
to至
db-connection <- dbConnect(MySQL(), user = '<remote-user-name>', password = '<remote-password>', dbname = '<remote-database>', port=3307, host='127.0.0.1')
If the host is set to 'localhost' or ports are left at 3306, an Access denied message is displayed.如果主机设置为“localhost”或端口保留为 3306,则会显示拒绝访问消息。
Unclear if this is due to a MySQL discrepancy or an operating system discrepancy.不清楚这是由于 MySQL 差异还是操作系统差异造成的。 I don't believe it to be an issue on the remote server being SSH'd into since it works on the Windows laptop.我不认为这是通过 SSH 连接到的远程服务器上的问题,因为它可以在 Windows 笔记本电脑上运行。
select user,host from mysql.user; select 用户,主机来自 mysql.user; Check your legal username and host and replace it.检查您的合法用户名和主机并替换它。 In addition, there is a difference between 127.0.0.1 and localhost.另外,127.0.0.1 和 localhost 是有区别的。 You should pay attention to whether the /etc/hosts file is mapped.您应该注意/etc/hosts 文件是否被映射。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.