github.com/cellofellow/gopkg@v0.0.0-20140722061823-eec0544a62ad/database/mysql/README.md (about)

     1  # Go-MySQL-Driver
     2  
     3  A MySQL-Driver for Go's [database/sql](http://golang.org/pkg/database/sql) package
     4  
     5  ![Go-MySQL-Driver logo](https://raw.github.com/wiki/go-sql-driver/mysql/gomysql_m.png "Golang Gopher holding the MySQL Dolphin")
     6  
     7  **Latest stable Release:** [Version 1.2 (June 03, 2014)](https://github.com/go-sql-driver/mysql/releases)
     8  
     9  [![Build Status](https://travis-ci.org/go-sql-driver/mysql.png?branch=master)](https://travis-ci.org/go-sql-driver/mysql)
    10  
    11  ---------------------------------------
    12    * [Features](#features)
    13    * [Requirements](#requirements)
    14    * [Installation](#installation)
    15    * [Usage](#usage)
    16      * [DSN (Data Source Name)](#dsn-data-source-name)
    17        * [Password](#password)
    18        * [Protocol](#protocol)
    19        * [Address](#address)
    20        * [Parameters](#parameters)
    21        * [Examples](#examples)
    22      * [LOAD DATA LOCAL INFILE support](#load-data-local-infile-support)
    23      * [time.Time support](#timetime-support)
    24      * [Unicode support](#unicode-support)
    25    * [Testing / Development](#testing--development)
    26    * [License](#license)
    27  
    28  ---------------------------------------
    29  
    30  ## Features
    31    * Lightweight and [fast](https://github.com/go-sql-driver/sql-benchmark "golang MySQL-Driver performance")
    32    * Native Go implementation. No C-bindings, just pure Go
    33    * Connections over TCP/IPv4, TCP/IPv6 or Unix domain sockets
    34    * Automatic handling of broken connections
    35    * Automatic Connection Pooling *(by database/sql package)*
    36    * Supports queries larger than 16MB
    37    * Full [`sql.RawBytes`](http://golang.org/pkg/database/sql/#RawBytes) support.
    38    * Intelligent `LONG DATA` handling in prepared statements
    39    * Secure `LOAD DATA LOCAL INFILE` support with file Whitelisting and `io.Reader` support
    40    * Optional `time.Time` parsing
    41  
    42  ## Requirements
    43    * Go 1.1 or higher
    44    * MySQL (4.1+), MariaDB, Percona Server, Google CloudSQL or Sphinx (2.2.3+)
    45  
    46  ---------------------------------------
    47  
    48  ## Installation
    49  Simple install the package to your [$GOPATH](http://code.google.com/p/go-wiki/wiki/GOPATH "GOPATH") with the [go tool](http://golang.org/cmd/go/ "go command") from shell:
    50  ```bash
    51  $ go get github.com/go-sql-driver/mysql
    52  ```
    53  Make sure [Git is installed](http://git-scm.com/downloads) on your machine and in your system's `PATH`.
    54  
    55  ## Usage
    56  _Go MySQL Driver_ is an implementation of Go's `database/sql/driver` interface. You only need to import the driver and can use the full [`database/sql`](http://golang.org/pkg/database/sql) API then.
    57  
    58  Use `mysql` as `driverName` and a valid [DSN](#dsn-data-source-name)  as `dataSourceName`:
    59  ```go
    60  import "database/sql"
    61  import _ "github.com/go-sql-driver/mysql"
    62  
    63  db, err := sql.Open("mysql", "user:password@/dbname")
    64  ```
    65  
    66  [Examples are available in our Wiki](https://github.com/go-sql-driver/mysql/wiki/Examples "Go-MySQL-Driver Examples").
    67  
    68  
    69  ### DSN (Data Source Name)
    70  
    71  The Data Source Name has a common format, like e.g. [PEAR DB](http://pear.php.net/manual/en/package.database.db.intro-dsn.php) uses it, but without type-prefix (optional parts marked by squared brackets):
    72  ```
    73  [username[:password]@][protocol[(address)]]/dbname[?param1=value1&...&paramN=valueN]
    74  ```
    75  
    76  A DSN in its fullest form:
    77  ```
    78  username:password@protocol(address)/dbname?param=value
    79  ```
    80  
    81  Except for the databasename, all values are optional. So the minimal DSN is:
    82  ```
    83  /dbname
    84  ```
    85  
    86  If you do not want to preselect a database, leave `dbname` empty:
    87  ```
    88  /
    89  ```
    90  This has the same effect as an empty DSN string:
    91  ```
    92  
    93  ```
    94  
    95  #### Password
    96  Passwords can consist of any character. Escaping is **not** necessary.
    97  
    98  #### Protocol
    99  See [net.Dial](http://golang.org/pkg/net/#Dial) for more information which networks are available.
   100  In general you should use an Unix domain socket if available and TCP otherwise for best performance.
   101  
   102  #### Address
   103  For TCP and UDP networks, addresses have the form `host:port`.
   104  If `host` is a literal IPv6 address, it must be enclosed in square brackets.
   105  The functions [net.JoinHostPort](http://golang.org/pkg/net/#JoinHostPort) and [net.SplitHostPort](http://golang.org/pkg/net/#SplitHostPort) manipulate addresses in this form.
   106  
   107  For Unix domain sockets the address is the absolute path to the MySQL-Server-socket, e.g. `/var/run/mysqld/mysqld.sock` or `/tmp/mysql.sock`.
   108  
   109  #### Parameters
   110  *Parameters are case-sensitive!*
   111  
   112  Notice that any of `true`, `TRUE`, `True` or `1` is accepted to stand for a true boolean value. Not surprisingly, false can be specified as any of: `false`, `FALSE`, `False` or `0`.
   113  
   114  ##### `allowAllFiles`
   115  
   116  ```
   117  Type:           bool
   118  Valid Values:   true, false
   119  Default:        false
   120  ```
   121  
   122  `allowAllFiles=true` disables the file Whitelist for `LOAD DATA LOCAL INFILE` and allows *all* files.
   123  [*Might be insecure!*](http://dev.mysql.com/doc/refman/5.7/en/load-data-local.html)
   124  
   125  ##### `allowOldPasswords`
   126  
   127  ```
   128  Type:           bool
   129  Valid Values:   true, false
   130  Default:        false
   131  ```
   132  `allowOldPasswords=true` allows the usage of the insecure old password method. This should be avoided, but is necessary in some cases. See also [the old_passwords wiki page](https://github.com/go-sql-driver/mysql/wiki/old_passwords).
   133  
   134  ##### `charset`
   135  
   136  ```
   137  Type:           string
   138  Valid Values:   <name>
   139  Default:        none
   140  ```
   141  
   142  Sets the charset used for client-server interaction (`"SET NAMES <value>"`). If multiple charsets are set (separated by a comma), the following charset is used if setting the charset failes. This enables for example support for `utf8mb4` ([introduced in MySQL 5.5.3](http://dev.mysql.com/doc/refman/5.5/en/charset-unicode-utf8mb4.html)) with fallback to `utf8` for older servers (`charset=utf8mb4,utf8`).
   143  
   144  Usage of the `charset` parameter is discouraged because it issues additional queries to the server.
   145  Unless you need the fallback behavior, please use `collation` instead.
   146  
   147  ##### `collation`
   148  
   149  ```
   150  Type:           string
   151  Valid Values:   <name>
   152  Default:        utf8_general_ci
   153  ```
   154  
   155  Sets the collation used for client-server interaction on connection. In contrast to `charset`, `collation` does not issue additional queries. If the specified collation is unavailable on the target server, the connection will fail.
   156  
   157  A list of valid charsets for a server is retrievable with `SHOW COLLATION`.
   158  
   159  ##### `clientFoundRows`
   160  
   161  ```
   162  Type:           bool
   163  Valid Values:   true, false
   164  Default:        false
   165  ```
   166  
   167  `clientFoundRows=true` causes an UPDATE to return the number of matching rows instead of the number of rows changed.
   168  
   169  
   170  ##### `loc`
   171  
   172  ```
   173  Type:           string
   174  Valid Values:   <escaped name>
   175  Default:        UTC
   176  ```
   177  
   178  Sets the location for time.Time values (when using `parseTime=true`). *"Local"* sets the system's location. See [time.LoadLocation](http://golang.org/pkg/time/#LoadLocation) for details.
   179  
   180  Please keep in mind, that param values must be [url.QueryEscape](http://golang.org/pkg/net/url/#QueryEscape)'ed. Alternatively you can manually replace the `/` with `%2F`. For example `US/Pacific` would be `loc=US%2FPacific`.
   181  
   182  
   183  ##### `parseTime`
   184  
   185  ```
   186  Type:           bool
   187  Valid Values:   true, false
   188  Default:        false
   189  ```
   190  
   191  `parseTime=true` changes the output type of `DATE` and `DATETIME` values to `time.Time` instead of `[]byte` / `string`
   192  
   193  
   194  ##### `strict`
   195  
   196  ```
   197  Type:           bool
   198  Valid Values:   true, false
   199  Default:        false
   200  ```
   201  
   202  `strict=true` enables the strict mode in which MySQL warnings are treated as errors.
   203  
   204  By default MySQL also treats notes as warnings. Use [`sql_notes=false`](http://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_sql_notes) to ignore notes. See the [examples](#examples) for an DSN example.
   205  
   206  
   207  ##### `timeout`
   208  
   209  ```
   210  Type:           decimal number
   211  Default:        OS default
   212  ```
   213  
   214  *Driver* side connection timeout. The value must be a string of decimal numbers, each with optional fraction and a unit suffix ( *"ms"*, *"s"*, *"m"*, *"h"* ), such as *"30s"*, *"0.5m"* or *"1m30s"*. To set a server side timeout, use the parameter [`wait_timeout`](http://dev.mysql.com/doc/refman/5.6/en/server-system-variables.html#sysvar_wait_timeout).
   215  
   216  
   217  ##### `tls`
   218  
   219  ```
   220  Type:           bool / string
   221  Valid Values:   true, false, skip-verify, <name>
   222  Default:        false
   223  ```
   224  
   225  `tls=true` enables TLS / SSL encrypted connection to the server. Use `skip-verify` if you want to use a self-signed or invalid certificate (server side). Use a custom value registered with [`mysql.RegisterTLSConfig`](http://godoc.org/github.com/go-sql-driver/mysql#RegisterTLSConfig).
   226  
   227  
   228  ##### System Variables
   229  
   230  All other parameters are interpreted as system variables:
   231    * `autocommit`: `"SET autocommit=<value>"`
   232    * `time_zone`: `"SET time_zone=<value>"`
   233    * [`tx_isolation`](https://dev.mysql.com/doc/refman/5.5/en/server-system-variables.html#sysvar_tx_isolation): `"SET tx_isolation=<value>"`
   234    * `param`: `"SET <param>=<value>"`
   235  
   236  *The values must be [url.QueryEscape](http://golang.org/pkg/net/url/#QueryEscape)'ed!*
   237  
   238  #### Examples
   239  ```
   240  user@unix(/path/to/socket)/dbname
   241  ```
   242  
   243  ```
   244  root:pw@unix(/tmp/mysql.sock)/myDatabase?loc=Local
   245  ```
   246  
   247  ```
   248  user:password@tcp(localhost:5555)/dbname?tls=skip-verify&autocommit=true
   249  ```
   250  
   251  Use the [strict mode](#strict) but ignore notes:
   252  ```
   253  user:password@/dbname?strict=true&sql_notes=false
   254  ```
   255  
   256  TCP via IPv6:
   257  ```
   258  user:password@tcp([de:ad:be:ef::ca:fe]:80)/dbname?timeout=90s&collation=utf8mb4_unicode_ci
   259  ```
   260  
   261  TCP on a remote host, e.g. Amazon RDS:
   262  ```
   263  id:password@tcp(your-amazonaws-uri.com:3306)/dbname
   264  ```
   265  
   266  Google Cloud SQL on App Engine:
   267  ```
   268  user@cloudsql(project-id:instance-name)/dbname
   269  ```
   270  
   271  TCP using default port (3306) on localhost:
   272  ```
   273  user:password@tcp/dbname?charset=utf8mb4,utf8&sys_var=esc%40ped
   274  ```
   275  
   276  Use the default protocol (tcp) and host (localhost:3306):
   277  ```
   278  user:password@/dbname
   279  ```
   280  
   281  No Database preselected:
   282  ```
   283  user:password@/
   284  ```
   285  
   286  ### `LOAD DATA LOCAL INFILE` support
   287  For this feature you need direct access to the package. Therefore you must change the import path (no `_`):
   288  ```go
   289  import "github.com/go-sql-driver/mysql"
   290  ```
   291  
   292  Files must be whitelisted by registering them with `mysql.RegisterLocalFile(filepath)` (recommended) or the Whitelist check must be deactivated by using the DSN parameter `allowAllFiles=true` ([*Might be insecure!*](http://dev.mysql.com/doc/refman/5.7/en/load-data-local.html)).
   293  
   294  To use a `io.Reader` a handler function must be registered with `mysql.RegisterReaderHandler(name, handler)` which returns a `io.Reader` or `io.ReadCloser`. The Reader is available with the filepath `Reader::<name>` then.
   295  
   296  See the [godoc of Go-MySQL-Driver](http://godoc.org/github.com/go-sql-driver/mysql "golang mysql driver documentation") for details.
   297  
   298  
   299  ### `time.Time` support
   300  The default internal output type of MySQL `DATE` and `DATETIME` values is `[]byte` which allows you to scan the value into a `[]byte`, `string` or `sql.RawBytes` variable in your programm.
   301  
   302  However, many want to scan MySQL `DATE` and `DATETIME` values into `time.Time` variables, which is the logical opposite in Go to `DATE` and `DATETIME` in MySQL. You can do that by changing the internal output type from `[]byte` to `time.Time` with the DSN parameter `parseTime=true`. You can set the default [`time.Time` location](http://golang.org/pkg/time/#Location) with the `loc` DSN parameter.
   303  
   304  **Caution:** As of Go 1.1, this makes `time.Time` the only variable type you can scan `DATE` and `DATETIME` values into. This breaks for example [`sql.RawBytes` support](https://github.com/go-sql-driver/mysql/wiki/Examples#rawbytes).
   305  
   306  Alternatively you can use the [`NullTime`](http://godoc.org/github.com/go-sql-driver/mysql#NullTime) type as the scan destination, which works with both `time.Time` and `string` / `[]byte`.
   307  
   308  
   309  ### Unicode support
   310  Since version 1.1 Go-MySQL-Driver automatically uses the collation `utf8_general_ci` by default.
   311  
   312  Other collations / charsets can be set using the [`collation`](#collation) DSN parameter.
   313  
   314  Version 1.0 of the driver recommended adding `&charset=utf8` (alias for `SET NAMES utf8`) to the DSN to enable proper UTF-8 support. This is not necessary anymore. The [`collation`](#collation) parameter should be preferred to set another collation / charset than the default.
   315  
   316  See http://dev.mysql.com/doc/refman/5.7/en/charset-unicode.html for more details on MySQL's Unicode support.
   317  
   318  
   319  ## Testing / Development
   320  To run the driver tests you may need to adjust the configuration. See the [Testing Wiki-Page](https://github.com/go-sql-driver/mysql/wiki/Testing "Testing") for details.
   321  
   322  Go-MySQL-Driver is not feature-complete yet. Your help is very appreciated.
   323  If you want to contribute, you can work on an [open issue](https://github.com/go-sql-driver/mysql/issues?state=open) or review a [pull request](https://github.com/go-sql-driver/mysql/pulls).
   324  
   325  See the [Contribution Guidelines](https://github.com/go-sql-driver/mysql/blob/master/CONTRIBUTING.md) for details.
   326  
   327  ---------------------------------------
   328  
   329  ## License
   330  Go-MySQL-Driver is licensed under the [Mozilla Public License Version 2.0](https://raw.github.com/go-sql-driver/mysql/master/LICENSE)
   331  
   332  Mozilla summarizes the license scope as follows:
   333  > MPL: The copyleft applies to any files containing MPLed code.
   334  
   335  
   336  That means:
   337    * You can **use** the **unchanged** source code both in private as also commercial
   338    * You **needn't publish** the source code of your library as long the files licensed under the MPL 2.0 are **unchanged**
   339    * You **must publish** the source code of any **changed files** licensed under the MPL 2.0 under a) the MPL 2.0 itself or b) a compatible license (e.g. GPL 3.0 or Apache License 2.0)
   340  
   341  Please read the [MPL 2.0 FAQ](http://www.mozilla.org/MPL/2.0/FAQ.html) if you have further questions regarding the license.
   342  
   343  You can read the full terms here: [LICENSE](https://raw.github.com/go-sql-driver/mysql/master/LICENSE)
   344  
   345  ![Go Gopher and MySQL Dolphin](https://raw.github.com/wiki/go-sql-driver/mysql/go-mysql-driver_m.jpg "Golang Gopher transporting the MySQL Dolphin in a wheelbarrow")
   346