Documentation
¶
Overview ¶
Package mysql registers an "ssh+tunnel" network type with go-mysql-driver, allowing MySQL connections to be routed through an SSH tunnel.
Import this package for its side effect:
import _ "github.com/TelpeNight/mytunnel/mysql"
Then use "ssh+tunnel" as the network in your DSN. Because the MySQL DSN parser treats "@" as a delimiter, use "(a)" in place of "@" inside the tunnel address:
db_user:db_pass@ssh+tunnel(ssh_user(a)bastion.example.com/tmp/mysql.sock?ServerAliveInterval=10)/mydb
Everything inside the parentheses is passed verbatim to dial.DialContext. When the destination address is omitted, package-level defaults match go-sql-driver/mysql: 127.0.0.1:3306 for TCP and /tmp/mysql.sock for Unix.
Example ¶
package main
import (
"database/sql"
_ "github.com/TelpeNight/mytunnel/mysql"
)
func main() {
// Importing the package registers the "ssh+tunnel" network with
// go-mysql-driver. Use (a) in place of @ inside the tunnel address to
// avoid conflicts with the MySQL DSN parser.
dsn := "db_user:db_pass@ssh+tunnel(ssh_user(a)bastion.example.com/tmp/mysql.sock?ServerAliveInterval=10)/mydb"
db, err := sql.Open("mysql", dsn)
if err != nil {
panic(err)
}
defer db.Close()
// db is ready to use — connections are established lazily.
_ = db
}
Output:
Click to show internal directories.
Click to hide internal directories.