Search code examples
gosshmgo

Connection to mongoDB via SSH golang


I need to connect to remote mongoDB server via ssh, i was doing this earlier with mysql and its looks like :

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)

Is there a similar function like mysql.RegisterDial in mgo package?


Solution

  • It really does not make sense to add the tunneling function into your code. A simple wrapper shell script prevents you from the necessity to reinvent the wheel:

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

    With this, you can call your program via the wrapper as usual.