MCPcopy Create free account
hub / github.com/bytebase/bytebase / getMySQLConnection

Method getMySQLConnection

backend/plugin/db/mysql/mysql.go:115–155  ·  view source on GitHub ↗
(connCfg db.ConnectionConfig)

Source from the content-addressed store, hash-verified

113}
114
115func (d *Driver) getMySQLConnection(connCfg db.ConnectionConfig) (string, error) {
116 protocol := "tcp"
117 if strings.HasPrefix(connCfg.DataSource.Host, "/") {
118 protocol = "unix"
119 }
120
121 params := []string{"multiStatements=true", "maxAllowedPacket=0"}
122 if err := validateMySQLExtraConnectionParameters(connCfg.DataSource.GetExtraConnectionParameters()); err != nil {
123 return "", err
124 }
125 for key, value := range connCfg.DataSource.GetExtraConnectionParameters() {
126 params = append(params, fmt.Sprintf("%s=%s", key, value))
127 }
128 if connCfg.DataSource.GetSshHost() != "" {
129 sshClient, err := util.GetSSHClient(connCfg.DataSource)
130 if err != nil {
131 return "", err
132 }
133 d.sshClient = sshClient
134 protocol = "mysql-tcp-" + uuid.NewString()[:8]
135 // Now we register the dialer with the ssh connection as a parameter.
136 mysql.RegisterDialContext(protocol, func(_ context.Context, addr string) (net.Conn, error) {
137 return sshClient.Dial("tcp", addr)
138 })
139 }
140
141 tlscfg, err := util.GetTLSConfig(connCfg.DataSource)
142 if err != nil {
143 return "", errors.Wrap(err, "sql: tls config error")
144 }
145 tlsKey := uuid.NewString()
146 if tlscfg != nil {
147 if err := mysql.RegisterTLSConfig(tlsKey, tlscfg); err != nil {
148 return "", errors.Wrap(err, "sql: failed to register tls config")
149 }
150 // TLS config is only used during sql.Open, so should be safe to deregister afterwards.
151 d.openCleanUp = append(d.openCleanUp, func() { mysql.DeregisterTLSConfig(tlsKey) })
152 params = append(params, fmt.Sprintf("tls=%s", tlsKey))
153 }
154 return fmt.Sprintf("%s:%s@%s(%s:%s)/%s?%s", connCfg.DataSource.Username, connCfg.Password, protocol, connCfg.DataSource.Host, connCfg.DataSource.Port, connCfg.ConnectionContext.DatabaseName, strings.Join(params, "&")), nil
155}
156
157// getRDSCertPool downloads and returns the RDS CA certificate pool.
158// AWS RDS connection with IAM require TLS connection.

Callers 1

OpenMethod · 0.95

Implementers 15

MockDriverbackend/plugin/advisor/utils_for_tests
Driverbackend/plugin/db/bigquery/bigquery.go
Driverbackend/plugin/db/mongodb/mongodb.go
Driverbackend/plugin/db/trino/trino.go
Driverbackend/plugin/db/redshift/redshift.go
Driverbackend/plugin/db/oracle/oracle.go
Driverbackend/plugin/db/dynamodb/dynamodb.go
Driverbackend/plugin/db/cosmosdb/cosmosdb.go
Driverbackend/plugin/db/spanner/spanner.go
Driverbackend/plugin/db/mssql/mssql.go
Driverbackend/plugin/db/mysql/mysql.go
Driverbackend/plugin/db/pg/pg.go

Calls 7

GetSSHClientFunction · 0.92
GetTLSConfigFunction · 0.92
JoinMethod · 0.80
DialMethod · 0.65
GetSshHostMethod · 0.45

Tested by

no test coverage detected