When using the github.com/go-sql-driver/mysql driver to connect to matrixone, connecting with the default user root works fine, but attempting to connect using the account:username:role mode fails, resulting in the error:
select failed: err[Error 20101 (HY000): internal error: there is no user.
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
"net/url"
)
func main() {
_, err := sql.Open("mysql","account:admin:role:xxx@tcp(192.168.110.210:6001)/ssb")
if err != nil {
fmt.Printf("connect failed: err[%v]", err)
return
}
}
Connection fails when including the tenant name because the username contains a colon (:), and in Go language when connecting to the MatrixOne database, the username and password are separated by a colon.
Therefore, when the username includes a colon, it causes the tenant name before the colon to be interpreted as the system tenant 'sys' username, resulting in a connection failure. To avoid this issue, you can use url.QueryEscape
to encode the username and then concatenate the string.
package main
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
"net/url"
)
func main() {
username := "account:admin:role"
encodeUsername := url.QueryEscape(username)
_, err := sql.Open("mysql", encodeUsername+":xxx@tcp(192.168.110.210:6001)/ssb")
if err != nil {
fmt.Printf("connect failed: err[%v]", err)
return
}
}