Giter Club home page Giter Club logo

go-mysql's Introduction

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)

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.

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

go-mysql's People

Contributors

serbaut avatar vanackere avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.