A pure Go MySQL driver for database/sql.
Requires Go >= 1.0.3 and MySQL >= 4.1
mysql[s]://[user[:password]][@host][:port][/database][?param&...]
mysqls://
establishes an SSL connectionuser
defaults to rootpassword
defaults to blankhost
defaults to localhost (use(unix)
for unix domain sockets)port
defaults to 3306
strict
: treat MySQL warnings as errorsallow-insecure-local-infile
: allowLOAD DATA LOCAL INFILE
ssl-insecure-skip-verify
: skip SSL certificate verificationsocket
: unix domain socket (default/var/run/mysqld/mysqld.sock
)debug
: log requests and MySQL warnings to the standard loggercharset
: set connection character set (read note below)client-multi-results
: set CLIENT_MULTI_RESULTS capability flag (read note below)
mysql://gopher1@localhost
mysql://gopher2:secret@localhost:3306/test?strict&debug
mysqls://gopher1@localhost?ssl-insecure-skip-verify
mysql://gopher2:secret@(unix)/test?socket=/var/lib/mysql/mysql.sock
A zero time.Time argument to Query/Exec is treated as a MySQL zero timestamp (0000-00-00 00:00:00). A MySQL zero timestamp is returned as a Go zero time.
Timestamps in MySQL are assumed to be in UTC. time.Time arguments are stored as UTC and returned as UTC.
Strings are by default UTF-8 encoded in the MySQL connection; they are automatically converted by the MySQL server as needed.
It is however common in legacy MySQL implementations (notably with php
clients) to have incorrectly encoded strings in the database and you
may need to trick the server not to translate to/from UTF-8. The
charset
parameter allows you to set the character set on a connection
basis (a SET NAMES
statement is executed on connect). Please review
http://mysql.rjweb.org/doc.php/charcoll before using this option.
Whith client-multi-results
parameter, it is possible to call stored
procedures which return a result set. Query() methods from the database/sql
package when passed a query that calls such a procedure will use the returned
result set. Exec() methods will get rowsAffected/lastInsertId from such a
procedure discarding the result set. Only the first result set is used. Any other
returned result sets from a stored procedure will be discarded.
go get github.com/serbaut/go-mysql
import (
"database/sql"
_ "github.com/serbaut/go-mysql"
)
func main() {
db, err := sql.Open("mysql", "mysql://gopher2:secret@localhost/mydb")
...
}
mysql@localhost> grant all on test.* to gopher1@localhost;
mysql@localhost> grant all on test.* to gopher2@localhost identified by 'secret';
$ go test