【问题标题】:Can't Connect in R to MySQL Server through SSH无法通过 SSH 在 R 中连接到 MySQL 服务器
【发布时间】:2018-08-21 19:35:47
【问题描述】:

背景

我正在尝试从云 MySQL 服务器中提取数据。我刚刚将数据库从共享服务器移动到专用云服务器。在旧位置一切正常。新位置需要 SSH 隧道。

研究/信息:

我发现的所有相似之处都是this SO article。它并没有真正适用,因为我从未使用过 localhost。

可以在 Sequel Pro 和 MySQL Workbench 中使用相同的凭据成功连接,但我无法让 R 脚本发挥作用。

可以在命令行连接mysql。

我确实没有在本地安装了 mysql。 (除非它与 High Sierra 一起提供;我不知道。无论如何,如果有本地 MySQL 实例,它就不会运行。)

代码:

以下是失败的原因:

con <- dbConnect(RMariaDB::MariaDB(),
                 user = 'soundings_app',
                 password = keyringr::decrypt_kc_pw("Soundings_app"),
                 host = '127.0.0.1',
                 port = 3306,
                 dbname='UFO')

我也尝试使用实际密码作为字符串,并得到了同样的错误。

错误信息

Error in connection_create(host, username, password, dbname, as.integer(port),  : 
  Failed to connect: Can't connect to MySQL server on '127.0.0.1' (57)

我唯一的猜测

我唯一能想到的是,SequelPro 和 Workbench 都有一个地方可以输入我的私钥 (~/.ssh) 的位置。

你能解释一下我做错了什么吗?

更新

我将主机添加到我的 ./ssh/config 文件中,如下所示:

Host XXX.XXX.130.0
  LocalForward 3306 localhost:3306

这导致我的其他连接失败,所以我删除了它。但是当我尝试连接 ~/.ssh/config 时,Sequel Pro 给了我以下信息:

Used command:  /usr/bin/ssh -v -N -S none -o ControlMaster=no -o ExitOnForwardFailure=yes -o ConnectTimeout=10 -o NumberOfPasswordPrompts=3 -i /Users/steves2018air/.ssh/id_rsa -o TCPKeepAlive=no -o ServerAliveInterval=60 -o ServerAliveCountMax=1 ubuntu@XXX.XXX.130.0 -L 49700:127.0.0.1:3306

OpenSSH_7.6p1, LibreSSL 2.6.2
debug1: Reading configuration data /Users/steves2018air/.ssh/config
debug1: /Users/steves2018air/.ssh/config line 1: Applying options for XXX.XXX.130.0
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 48: Applying options for *
debug1: Control socket " none" does not exist
debug1: Connecting to XXX.XXX.130.0 [XXX.XXX.130.0] port 22.
debug1: fd 3 clearing O_NONBLOCK
debug1: Connection established.
debug1: identity file /Users/steves2018air/.ssh/id_rsa type 0
debug1: key_load_public: No such file or directory
debug1: identity file /Users/steves2018air/.ssh/id_rsa-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_7.6
debug1: Remote protocol version 2.0, remote software version OpenSSH_7.2p2 Ubuntu-4ubuntu2.4
debug1: match: OpenSSH_7.2p2 Ubuntu-4ubuntu2.4 pat OpenSSH* compat 0x04000000
debug1: Authenticating to XXX.XXX.130.0:22 as 'ubuntu'

更新二

一时兴起,我尝试将主机从“127.0.0.1”更改为“localhost”,只是想看看会发生什么,以及这是否能说明问题。略有不同的错误:

Error in connection_create(host, username, password, dbname, as.integer(port),  : 
  Failed to connect: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

仍然没有解决方案或解释——你能帮忙吗?

【问题讨论】:

    标签: mysql r ssh r-dbi


    【解决方案1】:

    我花了很长时间,但这是我解决这个问题的方法:

    我将此添加到 /etc/mysql/.my.cnf:

    [destination]
     user=`username`
     port=3308
     proto=TCP
    

    然后我使用以下连接到云服务器:

    ssh ubuntu@xxx.xxx.xxx.x -L 3308:127.0.0.1:3306
    

    我可以在 R 外部的终端窗口中运行它,或者我可以在 RStudio 的终端窗口中运行它——无论哪种方式,以下工作:

    con <- dbConnect(RMariaDB::MariaDB(),
                     user = '`username`',
                     password = decrypt_kc_pw("`pw_profile_name`"),
                     groups = "destination",
                     default.file = "/etc/mysql/.my.cnf",
                     host = '127.0.0.1',
                     port = 3308,
                     dbname='`db_name`')
    

    【讨论】:

    • 谢谢!在我的情况下,它无需编辑 /etc/mysql/.my.cnf
    【解决方案2】:

    史蒂夫,谢谢你 - 经过一些小改动后,它对我来说非常有效。

    我的保存在 /etc/mysql/mariadb.cnf 文件中。

    con <- dbConnect(RMariaDB::MariaDB(),
    user = "yourusername",
    password = "yourpassword",
    groups = "destination",
    default.file = "/etc/mysql/mariadb.cnf",
    host = '127.0.0.1',
    port = 3308,
    dbname="yourdb_name")
    

    【讨论】:

      猜你喜欢
      • 2013-04-09
      • 2010-10-02
      • 2017-07-17
      • 1970-01-01
      • 2019-10-19
      • 2020-11-02
      • 2018-07-21
      相关资源
      最近更新 更多