We recommend using the configuration component to manage database configurations and the g.DB("database group name") method in the g object management module to obtain database operation objects. The database object will automatically read the corresponding configuration items from the configuration component and automatically initialize the singleton object for that database operation. The database configuration management functionality is implemented using a configuration management component (the configuration component adopts an interface design and uses a file system implementation by default), and also supports various data formats like toml/yaml/json/xml/ini/properties. The default and recommended configuration file data format is yaml.

Simple Configuration

ORM Configuration - File - 图1tip

From version v2.2.0, when using link for database configuration, the database component unifies the configuration formats for different database types to simplify configuration management.

The simplified configuration is specified through the link configuration item, with the format as follows:

  1. type:username:password@protocol(address)[/dbname][?param1=value1&...&paramN=valueN]

That is:

  1. Type:Account:Password@Protocol(Address)/DatabaseName?FeatureConfiguration

Where:

  • DatabaseName and FeatureConfiguration are optional parameters; other parameters are required.
  • Protocol optional configurations are: tcp/udp/unix/file, commonly tcp.
  • FeatureConfiguration is defined by the third-party driver implemented by the database type, check the third-party driver’s official website for specifics. For example, for the mysql driver, the third-party driver used is: https://github.com/go-sql-driver/mysql supporting features like multiStatements and loc.

Example:

  1. database:
  2. default:
  3. link: "mysql:root:12345678@tcp(127.0.0.1:3306)/test"
  4. user:
  5. link: "sqlite::@file(/var/data/db.sqlite3)"
  6. local:
  7. link: "mysql:username:password@unix(/tmp/mysql.sock)/dbname"

link examples for different data types are as follows:

Typelink Exampleextra parameters
mysqlmysql:root:12345678@tcp(127.0.0.1:3306)/test?loc=Local&parseTime=truemysql
mariadbmariadb:root:12345678@tcp(127.0.0.1:3306)/test?loc=Local&parseTime=truemysql
tidbtidb:root:12345678@tcp(127.0.0.1:3306)/test?loc=Local&parseTime=truemysql
pgsqlpgsql:root:12345678@tcp(127.0.0.1:5432)/testpq
mssqlmssql:root:12345678@tcp(127.0.0.1:1433)/test?encrypt=disablego-mssqldb
sqlitesqlite::@file(/var/data/db.sqlite3)pure go:go-sqlite
32bit-cgo:go-sqlite3
oracleoracle:root:12345678@tcp(127.0.0.1:5432)/testgo-ora
clickhouseclickhouse:root:12345678@tcp(127.0.0.1:9000)/testclickhouse-go
dmdm:root:12345678@tcp(127.0.0.1:5236)/testdm

ORM Configuration - File - 图2tip

For more database types supported by the framework, please refer to: https://github.com/gogf/gf/tree/master/contrib/drivers

Complete Configuration

The data format of the complete config.yaml database configuration item is as follows:

  1. database:
  2. default: # Group Name, customizable, default is default
  3. host: "127.0.0.1" # Address
  4. port: "3306" # Port
  5. user: "root" # Username
  6. pass: "your_password" # Password
  7. name: "your_database" # Database Name
  8. type: "mysql" # Database Type (e.g., mariadb/tidb/mysql/pgsql/mssql/sqlite/oracle/clickhouse/dm)
  9. link: "" # (Optional) Custom database link information, if this field is set, the above link fields (Host, Port, User, Pass, Name, Type) will be invalid
  10. extra: "" # (Optional) Additional feature configurations for different databases, defined by the underlying database driver, please see the specific database driver overview for what configurations are available
  11. role: "master" # (Optional) Database master-slave role (master/slave), default is master. If not using application master-slave mechanism please do not configure or leave empty.
  12. debug: false # (Optional) Enable debug mode
  13. prefix: "gf_" # (Optional) Table name prefix
  14. dryRun: false # (Optional) ORM Dry Run (read-only, no writing)
  15. charset: "utf8" # (Optional) Database encoding (e.g., utf8mb4/utf8/gbk/gb2312), usually set to utf8mb4. Default is utf8.
  16. protocol: "tcp" # (Optional) Database connection protocol, default is TCP
  17. weight: 100 # (Optional) Load balancing weight, used for load balancing control, not using application layer load balancing mechanism please leave empty
  18. timezone: "Local" # (Optional) Time zone configuration, e.g., Local
  19. namespace: "" # (Optional) To support the catalog & schema distinction problem of some database services, the original schema represents the database name, while the NameSpace represents the schema of some database services
  20. maxIdle: 10 # (Optional) Maximum number of idle connections in the connection pool (default 10)
  21. maxOpen: 100 # (Optional) Maximum number of open connections in the connection pool (default unlimited)
  22. maxLifetime: "30s" # (Optional) Duration of reuse for connection objects (default 30 seconds)
  23. queryTimeout: "0" # (Optional) Query statement timeout duration (default unlimited, also affected by ctx timeout). The value is in time.Parse supported formats, such as 30s, 1m.
  24. execTimeout: "0" # (Optional) Write statement timeout duration (default unlimited, also affected by ctx timeout). The value is in time.Parse supported formats, such as 30s, 1m.
  25. tranTimeout: "0" # (Optional) Transaction processing timeout duration (default unlimited, also affected by ctx timeout). The value is in time.Parse supported formats, such as 30s, 1m.
  26. prepareTimeout: "0" # (Optional) Prepared SQL statement execution timeout duration (default unlimited, also affected by ctx timeout). The value is in time.Parse supported formats, such as 30s, 1m.
  27. createdAt: "created_at" # (Optional) Field name for automatic creation time
  28. updatedAt: "updated_at" # (Optional) Field name for automatic update time
  29. deletedAt: "deleted_at" # (Optional) Field name for soft delete time
  30. timeMaintainDisabled: false # (Optional) Whether to completely disable the time update feature, when true CreatedAt/UpdatedAt/DeletedAt will all be invalid

ORM Configuration - File - 图3note

When using this configuration method, to ensure database security, the underlying by default does not support multi-line SQL statement execution. For more configuration control, refer to the recommended simplified link configuration, and understand the function of each connection parameter in the simplified configuration, as well as the official additional configuration parameters for the corresponding driver.

Cluster Mode

gdb supports cluster mode configuration; each configuration item group in database configurations can be multiple nodes, supporting load balancing weight strategies, for example:

  1. database:
  2. default:
  3. - link: "mysql:root:12345678@tcp(127.0.0.1:3306)/test"
  4. role: "master"
  5. - link: "mysql:root:12345678@tcp(127.0.0.1:3306)/test"
  6. role: "slave"
  7. user:
  8. - link: "mysql:root:12345678@tcp(127.0.0.1:3306)/user"
  9. role: "master"
  10. - link: "mysql:root:12345678@tcp(127.0.0.1:3306)/user"
  11. role: "slave"
  12. - link: "mysql:root:12345678@tcp(127.0.0.1:3306)/user"
  13. role: "slave"

The database configuration example above includes two database groups default and user, where the default group contains one master and one slave, and the user group contains one master and two slaves. In the code, you can use g.DB() and g.DB("user") to get the corresponding database connection object.

Log Configuration

gdb supports log output, using the glog.Logger object internally for log management, and it can be configured through configuration files. By default, gdb disables the DEBUG log output; to enable DEBUG messages, set the database’s debug parameter to true. Below is a configuration file example:

  1. database:
  2. logger:
  3. path: "/var/log/gf-app/sql"
  4. level: "all"
  5. stdout: true
  6. default:
  7. link: "mysql:root:12345678@tcp(127.0.0.1:3306)/user_center"
  8. debug: true

Where database.logger is the log configuration for gdb, which is a special configuration item. When this configuration does not exist, the default configuration of the logging component will be used. For more details, refer to the Logging - Configuration section.

ORM Configuration - File - 图4warning

Please note: since the ORM uses a secure preprocessing execution method at the lower layer, the submitted SQL and parameters are actually separate. Therefore, the complete SQL recorded in logs is for reference and convenient manual reading only and is not the actual SQL submitted to the lower layer.