ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
# 连接到数据库 GORM 官方支持的数据库类型有: MySQL, PostgreSQL, SQlite, SQL Server ## MySQL ```go 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{}) } ``` **注意:** 想要 Gorm 正确的处理 `time.Time` ,您需要带上 `parseTime` 参数。 ([查看更多参数](https://github.com/go-sql-driver/mysql#parameters)) 想要支持完整的 UTF-8 编码,您需要将 `charset=utf8` 更改为 `charset=utf8mb4`。 查看 [此文章](https://mathiasbynens.be/notes/mysql-utf8mb4) 获取详情 MySQl 驱动程序提供了 [一些高级配置](https://github.com/go-gorm/mysql) 可以在初始化过程中使用,例如: ``` 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, // 根据版本自动配置 }), &gorm.Config{}) ``` ## PostgreSQL ```go import ( "gorm.io/driver/postgres" "gorm.io/gorm" ) dsn := "user=gorm password=gorm dbname=gorm port=9920 sslmode=disable TimeZone=Asia/Shanghai" db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{}) ``` 我们使用 [pgx](https://github.com/jackc/pgx) 作为 postgres 的 database/sql 驱动器,默认情况下,它会启用 prepared statement 缓存,你可以这样禁用它: ```go // 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, // 禁用隐式 prepared statement }), &gorm.Config{}) ``` ## SQLite ```go 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 文档](https://www.sqlite.org/inmemorydb.html) 获取详情) ## SQL Server ```go 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{}) ``` Microsoft 为 GO (GORM) 使用 SQL Server 提供了 [一份指南](https://sqlchoice.azurewebsites.net/en-us/sql-server/developer-get-started/) ## 连接池 GORM 使用 [database/sql](https://pkg.go.dev/database/sql) 来维护连接池 ``` sqlDB, err := db.DB() // SetMaxIdleConns 设置空闲连接池中连接的最大数量 sqlDB.SetMaxIgleConns(10) // SetMaxOpenConns 设置打开数据库连接的最大数量 sqlDB.SetMaxOpenConns(100) // SetConnMaxLifetime 设置了连接可复用的最大时间 sqlDB.SetConnMaxLifetime(time.Hour) ``` 查看 [通用接口](../教程/generic_interface.md) 获取详情。 ## 不支持的数据库 有些数据库可能兼容 `mysql`、`postgres` 的方言,在这种情况下,你可以直接使用这些数据库的方言。 对于其它不支持的数据,[我们鼓励且欢迎大家伙开发更多数据库类型的驱动!](../高级/write_driver.md)