如何解决无法通过SSH连接R中的MySQL服务器?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (461)

以下是有错的代码:

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

将主机添加到./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)
提问于
用户回答回答于

将它添加到/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`')

扫码关注云+社区

领取腾讯云代金券