GORM 官方支持的数据库类型有: MySQL, PostgreSQL, SQlite, SQL Server

MySQL

  1. import (
    "gorm.io/driver/mysql"
    "gorm.io/gorm"
    )

    func main() {
    // 参考 https://github.com/go-sql-driver/mysql#dsn-data-source-name 获取详情
    dsn := "user:pass@tcp(127.0.0.1:3306)/dbname?charset=utf8mb4&parseTime=True&loc=Local"
    db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
    }

注意:想要正确的处理 time.Time ,您需要带上 parseTime 参数, (更多参数) 要支持完整的 UTF-8 编码,您需要将 charset=utf8 更改为 charset=utf8mb4 查看 此文章 获取详情

MySQl 驱动程序提供了 一些高级配置 可以在初始化过程中使用,例如:

  1. db, err := gorm.Open(mysql.New(mysql.Config{
    DSN: "gorm:gorm@tcp(127.0.0.1:3306)/gorm?charset=utf8&parseTime=True&loc=Local", // DSN data source name
    DefaultStringSize: 256, // string 类型字段的默认长度
    DisableDatetimePrecision: true, // 禁用 datetime 精度,MySQL 5.6 之前的数据库不支持
    DontSupportRenameIndex: true, // 重命名索引时采用删除并新建的方式,MySQL 5.7 之前的数据库和 MariaDB 不支持重命名索引
    DontSupportRenameColumn: true, // 用 `change` 重命名列,MySQL 8 之前的数据库和 MariaDB 不支持重命名列
    SkipInitializeWithVersion: false, // 根据当前 MySQL 版本自动配置
    }), &gorm.Config{})

自定义驱动

GORM 允许通过 DriverName 选项自定义 MySQL 驱动,例如:

  1. import (
    _ "example.com/my_mysql_driver"
    "gorm.io/gorm"
    )

    db, err := gorm.Open(mysql.New(mysql.Config{
    DriverName: "my_mysql_driver",
    DSN: "gorm:gorm@tcp(localhost:9910)/gorm?charset=utf8&parseTime=True&loc=Local", // Data Source Name,参考 https://github.com/go-sql-driver/mysql#dsn-data-source-name
    }), &gorm.Config{})

现有的数据库连接

GORM 允许通过一个现有的数据库连接来初始化 *gorm.DB

  1. import (
    "database/sql"
    "gorm.io/gorm"
    )

    sqlDB, err := sql.Open("mysql", "mydb_dsn")
    gormDB, err := gorm.Open(mysql.New(mysql.Config{
    Conn: sqlDB,
    }), &gorm.Config{})

PostgreSQL

  1. import (
    "gorm.io/driver/postgres"
    "gorm.io/gorm"
    )

    dsn := "host=localhost user=gorm password=gorm dbname=gorm port=9920 sslmode=disable TimeZone=Asia/Shanghai"
    db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})

我们使用 pgx 作为 postgres 的 database/sql 驱动,默认情况下,它会启用 prepared statement 缓存,你可以这样禁用它:

  1. // https://github.com/go-gorm/postgres
    db, err := gorm.Open(postgres.New(postgres.Config{
    DSN: "user=gorm password=gorm dbname=gorm port=9920 sslmode=disable TimeZone=Asia/Shanghai",
    PreferSimpleProtocol: true, // disables implicit prepared statement usage
    }), &gorm.Config{})

自定义驱动

GORM 允许通过 DriverName 选项自定义 PostgreSQL 驱动,例如:

  1. import (
    _ "github.com/GoogleCloudPlatform/cloudsql-proxy/proxy/dialers/postgres"
    "gorm.io/gorm"
    )

    db, err := gorm.Open(postgres.New(postgres.Config{
    DriverName: "cloudsqlpostgres",
    DSN: "host=project:region:instance user=postgres dbname=postgres password=password sslmode=disable",
    })

现有的数据库连接

GORM 允许通过一个现有的数据库连接来初始化 *gorm.DB

  1. import (
    "database/sql"
    "gorm.io/gorm"
    )

    sqlDB, err := sql.Open("postgres", "mydb_dsn")
    gormDB, err := gorm.Open(postgres.New(postgres.Config{
    Conn: sqlDB,
    }), &gorm.Config{})

SQLite

  1. import (
    "gorm.io/driver/sqlite"
    "gorm.io/gorm"
    )

    // github.com/mattn/go-sqlite3
    db, err := gorm.Open(sqlite.Open("gorm.db"), &gorm.Config{})

注意: 您也可以使用 file::memory:?cache=shared 替代文件路径。 这会告诉 SQLite 在系统内存中使用一个临时数据库。 (查看 SQLite 文档 获取详情)

SQL Server

  1. import (
    "gorm.io/driver/sqlserver"
    "gorm.io/gorm"
    )

    // github.com/denisenkom/go-mssqldb
    dsn := "sqlserver://gorm:LoremIpsum86@localhost:9930?database=gorm"
    db, err := gorm.Open(sqlserver.Open(dsn), &gorm.Config{})

Clickhouse

https://github.com/go-gorm/clickhouse

  1. import (
    "gorm.io/driver/clickhouse"
    "gorm.io/gorm"
    )

    func main() {
    dsn := "tcp://localhost:9000?database=gorm&username=gorm&password=gorm&read_timeout=10&write_timeout=20"
    db, err := gorm.Open(clickhouse.Open(dsn), &gorm.Config{})

    // Auto Migrate
    db.AutoMigrate(&User{})
    // Set table options
    db.Set("gorm:table_options", "ENGINE=Distributed(cluster, default, hits)").AutoMigrate(&User{})

    // Insert
    db.Create(&user)

    // Select
    db.Find(&user, "id = ?", 10)

    // Batch Insert
    var users = []User{user1, user2, user3}
    db.Create(&users)
    // ...
    }

Connection Pool

GORM using database/sql to maintain connection pool

  1. sqlDB, err := db.DB()

    // SetMaxIdleConns sets the maximum number of connections in the idle connection pool.
    sqlDB.SetMaxIdleConns(10)

    // SetMaxOpenConns sets the maximum number of open connections to the database.
    sqlDB.SetMaxOpenConns(100)

    // SetConnMaxLifetime sets the maximum amount of time a connection may be reused.
    sqlDB.SetConnMaxLifetime(time.Hour)

Refer Generic Interface for details

Unsupported Databases

Some databases may be compatible with the mysql or postgres dialect, in which case you could just use the dialect for those databases.

For others, you are encouraged to make a driver, pull request welcome!