How to connect with MySQL using a unix socket?

huangapple go评论105阅读模式
英文:

How to connect with MySQL using a unix socket?

问题

我想使用Unix套接字连接到MySQL。我不确定如何在连接字符串中传递变量。我在某个地方读到可以使用Config.FormatDSN结构来定义这些值,但我不确定具体的用法。

// 创建数据库连接并返回其实例
func Connection() (*sql.DB, error) {
	conn, err := sql.Open("mysql", "username/password@unix(socketpath)/dbname")
	return conn, err
}
英文:

I want to connect to MySQL using a Unix socket connection. I am not sure how to pass the variables in the connection string. I have read somewhere that I can also use Config.FormatDSN struct to define the values, but I am not sure how.

// Creates a database connection and returns its instance
func Connection() (*sql.DB, error) {
	conn, err := sql.Open("mysql", "username/password@unix(socketpath)/dbname")
	return conn, err
}

答案1

得分: 4

所以,在一些尝试和试错的方法之后,我找到了解决我的问题的方法。如果你想要使用 Unix socket 连接在 Golang 中连接到 MySQL,你可以按照以下方式准备你的连接字符串:

{{username}}:{{password}}@unix({{socketPath}})/{{dbname}}?charset=utf8

{{}} 中的值是变量。

注意:socket 路径必须是绝对路径。例如:
/usr/local/bin/path/to/socket

我发现这个参考资料非常有帮助:
https://chromium.googlesource.com/external/github.com/go-sql-driver/mysql/+/a48f79b55b5a2107793c84c3bbd445138dc7f0d5/README.md

如果你感兴趣,这是完整的实现:


import (
	"database/sql"
	"fmt"

	_ "github.com/go-sql-driver/mysql"
)

type ConnectionSpecs struct {
	username   string
	password   string
	socketPath string
	database   string
}

func GetConnStr() string {
	connConfig := ConnectionSpecs{
		username:   "root",
		password:   "my-password",
		socketPath: "/usr/local/bin/path/to/socket",
		database:   "test",
	}

	connStr := connConfig.username + ":" + connConfig.password +
		"@unix(" + connConfig.socketPath + ")" +
		"/" + connConfig.database +
		"?charset=utf8"
	return connStr
}

// 创建一个数据库连接并返回该连接
func Connection() (*sql.DB, error) {
	var connStr = GetConnStr()
	fmt.Println(connStr)
	conn, err := sql.Open("mysql", GetConnStr())
	return conn, err
}

英文:

So, after some hit and trial methods, I found the solution to my problem. If you want to connect to MySQL in Golang using a Unix socket connection, you can prepare your connection string in the following manner

{{username}}:{{password}}@unix({{socketPath}})/{{dbname}}?charset=utf8

values enclosed in {{}} are variables.

Note: socket path must be absolute. For example:
/usr/local/bin/path/to/socket

I found this reference very helpful
https://chromium.googlesource.com/external/github.com/go-sql-driver/mysql/+/a48f79b55b5a2107793c84c3bbd445138dc7f0d5/README.md

Here is the full implementation if you are interested


import (
	"database/sql"
	"fmt"

	_ "github.com/go-sql-driver/mysql"
)

type ConnectionSpecs struct {
	username   string
	password   string
	socketPath string
	database   string
}

func GetConnStr() string {
	connConfig := ConnectionSpecs{
		username:   "root",
		password:   "my-password",
		socketPath: "/usr/local/bin/path/to/socket",
		database:   "test",
	}

	connStr := connConfig.username + ":" + connConfig.password +
		"@unix(" + connConfig.socketPath + ")" +
		"/" + connConfig.database +
		"?charset=utf8"
	return connStr
}

// Creates a database connection and returns the same
func Connection() (*sql.DB, error) {
	var connStr = GetConnStr()
	fmt.Println(connStr)
	conn, err := sql.Open("mysql", GetConnStr())
	return conn, err
}

huangapple
  • 本文由 发表于 2021年6月7日 15:16:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/67867424.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定