通过 SSH golang 连接到 mongoDB

Connection to mongoDB via SSH golang

我需要通过 ssh 连接到远程 mongoDB 服务器,我之前用 mysql 这样做,它看起来像:

sshcon, err := ssh.Dial("tcp", fmt.Sprintf("%s:%d", sshHost, sshPort), sshConfig)
    if err == nil {
        defer sshcon.Close()
        mysql.RegisterDial("mysql+tcp", (&ViaSSHDialer{sshcon}).Dial)

mgo包里有没有类似mysql.RegisterDial的功能?

在代码中添加隧道功能确实没有意义。一个简单的包装器 shell 脚本使您不必重新发明轮子:

#!/bin/bash

ssh $USER@$MONGO_HOST -L $LOCAL_PORT:127.0.0.1:27017
./yourApplication "$*"

这样,您就可以像往常一样通过包装器调用您的程序了。