Skip to content

timob/go-mysql

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

66 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-mysql

A pure Go MySQL driver for database/sql.

Requires Go >= 1.0.3 and MySQL >= 4.1

Data Source Name

Format

mysql[s]://[user[:password]][@host][:port][/database][?param&...]
  • mysqls:// establishes an SSL connection
  • user defaults to root
  • password defaults to blank
  • host defaults to localhost (use (unix) for unix domain sockets)
  • port defaults to 3306

Parameters

  • strict : treat MySQL warnings as errors
  • allow-insecure-local-infile : allow LOAD DATA LOCAL INFILE
  • ssl-insecure-skip-verify : skip SSL certificate verification
  • socket : unix domain socket (default /var/run/mysqld/mysqld.sock)
  • debug : log requests and MySQL warnings to the standard logger
  • charset : set connection character set (read note below)
  • client-multi-results : set CLIENT_MULTI_RESULTS capability flag (read note below)

Examples

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

Notes

About Time

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.

Character Set

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.

Client Multi Results

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.

Installation

go get github.com/serbaut/go-mysql

Usage

import (
    "database/sql"
    _ "github.com/serbaut/go-mysql"
)

func main() {
    db, err := sql.Open("mysql", "mysql://gopher2:secret@localhost/mydb")
    ...
}

Testing

mysql@localhost> grant all on test.* to gopher1@localhost;
mysql@localhost> grant all on test.* to gopher2@localhost identified by 'secret';

$ go test

About

MySQL driver for Go database/sql

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 100.0%