golang-mysql包
Contents
golang原生的mysql包是https://github.com/go-sql-driver/mysql
其中DSN (Data Source Name)格式username:password@protocol(address)/dbname?param=value
| 字段 | 介绍 |
|---|---|
| username | 数据库账号名 |
| password | 数据库密码 |
| protocol | 协议,tcp, |
| address | 数据库host地址 |
| dbname | 数据库名,此项为可选项 |
| param | 参数支持多个,且区分大小写 |
| 参数 | 默认 | 介绍 |
|---|---|---|
| allowAllFiles | false | |
| allowCleartextPasswords | false | |
| allowNativePasswords | true | |
| allowOldPasswords | false | |
| charset | 常用,字符集 | |
| checkConnLiveness | true | |
| collation | utf8mb4_general_ci | |
| clientFoundRows | false | |
| columnsWithAlias | false | |
| interpolateParams | false | |
| loc | UTC | 常用,时区 |
| maxAllowedPacket | 4194304 | |
| multiStatements | false | |
| parseTime | false | |
| readTimeout | 0 | 执行读开始计算时间,超过则中断 |
| rejectReadOnly | false | |
| serverPubKey | none | |
| timeout | OS default | |
| tls | false | |
| writeTimeout | 0 |
其他参数的填写则设置为系统变量,string要用单引号圈起来且url.QueryEscape处理
设置函数
SetMaxOpenConns 为最大连接数
SetMaxIdleConns 为最大空闲数,SetMaxIdleConns<=SetMaxOpenConns,一般不推荐用
SetConnMaxLifetime 为最大空闲时间,一般小于mysql系统的wait_timeout
mysql配置
查询慢日志相关
- slow_query_log 是否开启了慢日志
- long_query_time 超过多少s开始记录
- slow_query_log_file 慢日志位置
select @@global.slow_query_log,@@global.long_query_time,@@global.slow_query_log_file
's blog