// Connect to a MySQL server, addr can be ip:port, or a unix socket domain like /var/sock. func Connect(addr string, user string, password string, dbName string) (*Conn, error) { proto := getNetProto(addr) c := new(Conn) fmt.Printf("mysql %s %s:%s %s\n", addr, user, password, dbName) var err error conn, err := net.DialTimeout(proto, addr, 10*time.Second) if err != nil { return nil, errors.Trace(err) } c.Conn = packet.NewConn(conn) c.user = user c.password = password c.db = dbName //use default charset here, utf-8 c.charset = DEFAULT_CHARSET if err = c.handshake(); err != nil { return nil, errors.Trace(err) } return c, nil }
func NewConn(conn net.Conn, user string, password string, h Handler) (*Conn, error) { c := new(Conn) c.h = h c.user = user c.Conn = packet.NewConn(conn) c.connectionID = atomic.AddUint32(&baseConnID, 1) c.stmts = make(map[uint32]*Stmt) c.salt, _ = RandomBuf(20) c.closed.Set(false) if err := c.handshake(password); err != nil { c.Close() return nil, err } return c, nil }