README.md
1# GORM MySQL Driver
2
3## Quick Start
4
5```go
6import (
7 "gorm.io/driver/mysql"
8 "gorm.io/gorm"
9)
10
11// https://github.com/go-sql-driver/mysql
12dsn := "gorm:gorm@tcp(localhost:9910)/gorm?charset=utf8&parseTime=True&loc=Local"
13db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
14```
15
16## Configuration
17
18```go
19import (
20 "gorm.io/driver/mysql"
21 "gorm.io/gorm"
22)
23
24var datetimePrecision = 2
25
26db, err := gorm.Open(mysql.New(mysql.Config{
27 DSN: "gorm:gorm@tcp(localhost:9910)/gorm?charset=utf8&parseTime=True&loc=Local", // data source name, refer https://github.com/go-sql-driver/mysql#dsn-data-source-name
28 DefaultStringSize: 256, // add default size for string fields, by default, will use db type `longtext` for fields without size, not a primary key, no index defined and don't have default values
29 DisableDatetimePrecision: true, // disable datetime precision support, which not supported before MySQL 5.6
30 DefaultDatetimePrecision: &datetimePrecision, // default datetime precision
31 DontSupportRenameIndex: true, // drop & create index when rename index, rename index not supported before MySQL 5.7, MariaDB
32 DontSupportRenameColumn: true, // use change when rename column, rename rename not supported before MySQL 8, MariaDB
33 SkipInitializeWithVersion: false, // smart configure based on used version
34}), &gorm.Config{})
35```
36
37## Customized Driver
38
39```go
40import (
41 _ "example.com/my_mysql_driver"
42 "gorm.io/gorm"
43)
44
45db, err := gorm.Open(mysql.New(mysql.Config{
46 DriverName: "my_mysql_driver_name",
47 DSN: "gorm:gorm@tcp(localhost:9910)/gorm?charset=utf8&parseTime=True&loc=Local", // data source name, refer https://github.com/go-sql-driver/mysql#dsn-data-source-name
48})
49```
50
51Checkout [https://gorm.io](https://gorm.io) for details.
52