本文整理汇总了Golang中github.com/percona/percona-agent/mysql.DSN.DSN方法的典型用法代码示例。如果您正苦于以下问题:Golang DSN.DSN方法的具体用法?Golang DSN.DSN怎么用?Golang DSN.DSN使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/percona/percona-agent/mysql.DSN
的用法示例。
在下文中一共展示了DSN.DSN方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: createMySQLInstance
func (i *Installer) createMySQLInstance(dsn mysql.DSN) (*proto.MySQLInstance, error) {
// First use instance.Manager to fill in details about the MySQL server.
dsnString, _ := dsn.DSN()
mi := &proto.MySQLInstance{
Hostname: i.hostname,
DSN: dsnString,
}
if err := instance.GetMySQLInfo(mi); err != nil {
if i.flags["debug"] {
log.Printf("err=%s\n", err)
}
return nil, err
}
// POST <api>/instances/mysql
data, err := json.Marshal(mi)
if err != nil {
return nil, err
}
url := pct.URL(i.agentConfig.ApiHostname, "instances", "mysql")
if i.flags["debug"] {
log.Println(url)
}
resp, _, err := i.api.Post(i.agentConfig.ApiKey, url, data)
if i.flags["debug"] {
log.Printf("resp=%#v\n", resp)
log.Printf("err=%s\n", err)
}
if err != nil {
return nil, err
}
// Create new instance, if it already exist then just use it
// todo: better handling of duplicate instance
if resp.StatusCode != http.StatusCreated && resp.StatusCode != http.StatusConflict {
return nil, fmt.Errorf("Failed to create MySQL instance (status code %d)", resp.StatusCode)
}
// API returns URI of new resource in Location header
uri := resp.Header.Get("Location")
if uri == "" {
return nil, fmt.Errorf("API did not return location of new MySQL instance")
}
// GET <api>/instances/mysql/id (URI)
code, data, err := i.api.Get(i.agentConfig.ApiKey, uri)
if i.flags["debug"] {
log.Printf("code=%d\n", code)
log.Printf("err=%s\n", err)
}
if err != nil {
return nil, err
}
if code != http.StatusOK {
return nil, fmt.Errorf("Failed to get new MySQL instance (status code %d)", code)
}
if err := json.Unmarshal(data, mi); err != nil {
return nil, err
}
return mi, nil
}
示例2: TestMySQLConnection
func TestMySQLConnection(dsn mysql.DSN) error {
dsnString, err := dsn.DSN()
if err != nil {
return err
}
fmt.Printf("Testing MySQL connection %s...\n", dsn)
conn := mysql.NewConnection(dsnString)
if err := conn.Connect(1); err != nil {
return err
}
defer conn.Close()
return nil
}
示例3: verifyMySQLConnection
func (i *Installer) verifyMySQLConnection(dsn mysql.DSN) (err error) {
dsnString, err := dsn.DSN()
if err != nil {
return err
}
if i.flags.Bool["debug"] {
log.Printf("verifyMySQLConnection: %#v %s\n", dsn, dsnString)
}
conn := mysql.NewConnection(dsnString)
if err := conn.Connect(1); err != nil {
return err
}
conn.Close()
return nil
}
示例4: TestAllFields
func (s *DSNTestSuite) TestAllFields(t *C) {
dsn := mysql.DSN{
Username: "user",
Password: "pass",
Hostname: "host.example.com",
Port: "3306",
}
str, err := dsn.DSN()
t.Check(err, IsNil)
t.Check(str, Equals, "user:[email protected](host.example.com:3306)/?parseTime=true")
// Stringify DSN removes password, e.g. makes it safe to print log, etc.
str = fmt.Sprintf("%s", dsn)
t.Check(str, Equals, "user:<password-hidden>@tcp(host.example.com:3306)")
}
示例5: createMySQLUser
func (i *Installer) createMySQLUser(dsn mysql.DSN) (mysql.DSN, error) {
// Same host:port or socket, but different user and pass.
userDSN := dsn
userDSN.Username = "percona-agent"
userDSN.Password = fmt.Sprintf("%p%d", &dsn, rand.Uint32())
userDSN.OldPasswords = i.flags.Bool["old-passwords"]
dsnString, _ := dsn.DSN()
conn := mysql.NewConnection(dsnString)
if err := conn.Connect(1); err != nil {
return userDSN, err
}
defer conn.Close()
grants := MakeGrant(dsn, userDSN.Username, userDSN.Password, i.flags.Int64["mysql-max-user-connections"])
for _, grant := range grants {
if i.flags.Bool["debug"] {
log.Println(grant)
}
_, err := conn.DB().Exec(grant)
if err != nil {
return userDSN, fmt.Errorf("Error executing %s: %s", grant, err)
}
}
// Go MySQL driver resolves localhost to 127.0.0.1 but localhost is a special
// value for MySQL, so 127.0.0.1 may not work with a grant @localhost, so we
// add a 2nd grant @127.0.0.1 to be sure.
if dsn.Hostname == "localhost" {
dsn2 := dsn
dsn2.Hostname = "127.0.0.1"
grants := MakeGrant(dsn2, userDSN.Username, userDSN.Password, i.flags.Int64["mysql-max-user-connections"])
for _, grant := range grants {
if i.flags.Bool["debug"] {
log.Println(grant)
}
_, err := conn.DB().Exec(grant)
if err != nil {
return userDSN, fmt.Errorf("Error executing %s: %s", grant, err)
}
}
}
return userDSN, nil
}