Contents

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参数支持多个,且区分大小写
参数默认介绍
allowAllFilesfalse
allowCleartextPasswordsfalse
allowNativePasswordstrue
allowOldPasswordsfalse
charset常用,字符集
checkConnLivenesstrue
collationutf8mb4_general_ci
clientFoundRowsfalse
columnsWithAliasfalse
interpolateParamsfalse
locUTC常用,时区
maxAllowedPacket4194304
multiStatementsfalse
parseTimefalse
readTimeout0执行读开始计算时间,超过则中断
rejectReadOnlyfalse
serverPubKeynone
timeoutOS default
tlsfalse
writeTimeout0

其他参数的填写则设置为系统变量,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