go mysql timeout,golang 连接mysql 参数备忘

git 地址

https://github.com/Go-SQL-Driver/MySQL

Parameters

Parameters are case-sensitive!

Notice that any of true, TRUE, True or 1 is accepted to stand for a true boolean value. Not surprisingly, false can be specified as any of: false, FALSE, False or 0.

allowAllFiles

Type: bool

Valid Values: true, false

Default: false

allowAllFiles=true disables the file Whitelist for LOAD DATA LOCAL INFILE and allows all files.

mysql导入数据load data infile用法|http://hunan.iteye.com/blog/752606

allowCleartextPasswords

Type: bool

Valid Values: true, false

Default: false

allowCleartextPasswords=true allows using the cleartext client side plugin if required by an account, such as one defined with the PAM authentication plugin. Sending passwords in clear text may be a security problem in some configurations. To avoid problems if there is any possibility that the password would be intercepted, clients should connect to MySQL Server using a method that protects the password. Possibilities include TLS / SSL, IPsec, or a private network.

allowNativePasswords

Type: bool

Valid Values: true, false

Default: true

allowNativePasswords=false disallows the usage of MySQL native password method.

allowOldPasswords

Type: bool

Valid Values: true, false

Default: false

allowOldPasswords=true allows the usage of the insecure old password method. This should be avoided, but is necessary in some cases. See also the old_passwords wiki page.

charset

Type: string

Valid Values:

Default: none

Sets the charset used for client-server interaction ("SET NAMES "). If multiple charsets are set (separated by a comma), the following charset is used if setting the charset failes. This enables for example support for utf8mb4 (introduced in MySQL 5.5.3) with fallback to utf8 for older servers (charset=utf8mb4,utf8).

Usage of the charset parameter is discouraged because it issues additional queries to the server. Unless you need the fallback behavior, please use collation instead.

collation

Type: string

Valid Values:

Default: utf8_general_ci

Sets the collation used for client-server interaction on connection. In contrast to charset, collation does not issue additional queries. If the specified collation is unavailable on the target server, the connection will fail.

A list of valid charsets for a server is retrievable with SHOW COLLATION.

clientFoundRows

Type: bool

Valid Values: true, false

Default: false

clientFoundRows=true causes an UPDATE to return the number of matching rows instead of the number of rows changed.

columnsWithAlias

Type: bool

Valid Values: true, false

Default: false

When columnsWithAlias is true, calls to sql.Rows.Columns() will return the table alias and the column name separated by a dot. For example:

SELECT u.id FROM users as u

will return u.id instead of just id if columnsWithAlias=true.

interpolateParams

Type: bool

Valid Values: true, false

Default: false

If interpolateParams is true, placeholders (?) in calls to db.Query() and db.Exec() are interpolated into a single query string with given parameters. This reduces the number of roundtrips, since the driver has to prepare a statement, execute it with given parameters and close the statement again with interpolateParams=false.

This can not be used together with the multibyte encodings BIG5, CP932, GB2312, GBK or SJIS. These are blacklisted as they mayintroduce a SQL injection vulnerability!

loc

Type: string

Valid Values:

Default: UTC

Sets the location for time.Time values (when using parseTime=true). "Local" sets the system's location. See time.LoadLocationfor details.

Note that this sets the location for time.Time values but does not change MySQL's time_zone setting. For that see the time_zone system variable, which can also be set as a DSN parameter.

Please keep in mind, that param values must be url.QueryEscape'ed. Alternatively you can manually replace the / with %2F. For example US/Pacific would be loc=US%2FPacific.

maxAllowedPacket

Type: decimal number

Default: 0

Max packet size allowed in bytes. Use maxAllowedPacket=0 to automatically fetch the max_allowed_packet variable from server.

multiStatements

Type: bool

Valid Values: true, false

Default: false

Allow multiple statements in one query. While this allows batch queries, it also greatly increases the risk of SQL injections. Only the result of the first query is returned, all other results are silently discarded.

When multiStatements is used, ? parameters must only be used in the first statement.

parseTime

Type: bool

Valid Values: true, false

Default: false

parseTime=true changes the output type of DATE and DATETIME values to time.Time instead of []byte / string

readTimeout

Type: duration

Default: 0

I/O read timeout. The value must be a decimal number with a unit suffix ("ms", "s", "m", "h"), such as "30s", "0.5m" or "1m30s".

rejectReadOnly

Type: bool

Valid Values: true, false

Default: false

rejectreadOnly=true causes the driver to reject read-only connections. This is for a possible race condition during an automatic failover, where the mysql client gets connected to a read-only replica after the failover.

Note that this should be a fairly rare case, as an automatic failover normally happens when the primary is down, and the race condition shouldn't happen unless it comes back up online as soon as the failover is kicked off. On the other hand, when this happens, a MySQL application can get stuck on a read-only connection until restarted. It is however fairly easy to reproduce, for example, using a manual failover on AWS Aurora's MySQL-compatible cluster.

If you are not relying on read-only transactions to reject writes that aren't supposed to happen, setting this on some MySQL providers (such as AWS Aurora) is safer for failovers.

strict

Type: bool

Valid Values: true, false

Default: false

strict=true enables a driver-side strict mode in which MySQL warnings are treated as errors. This mode should not be used in production as it may lead to data corruption in certain situations.

A server-side strict mode, which is safe for production use, can be set via the sql_mode system variable.

By default MySQL also treats notes as warnings. Use sql_notes=false to ignore notes.

timeout

Type: duration

Default: OS default

Timeout for establishing connections, aka dial timeout. The value must be a decimal number with a unit suffix ("ms", "s", "m", "h"), such as "30s", "0.5m" or "1m30s".

tls

Type: bool / string

Valid Values: true, false, skip-verify,

Default: false

tls=true enables TLS / SSL encrypted connection to the server. Use skip-verify if you want to use a self-signed or invalid certificate (server side). Use a custom value registered with mysql.RegisterTLSConfig.

writeTimeout

Type: duration

Default: 0

I/O write timeout. The value must be a decimal number with a unit suffix ("ms", "s", "m", "h"), such as "30s", "0.5m" or "1m30s".

参数 参数区分大小写 请注意,true,TRUE或True中的任何一个都被接受来代表一个真正的布尔值。毫不奇怪,false可以指定为false,FALSE,False或0。 allowAllFiles 类型:bool 有效值:true,false 默认值:false allowAllFiles = true将文件白名单禁用LOAD DATA LOCAL INFILE,并允许所有文件。可能不安全! allowCleartextPasswords 类型:bool 有效值:true,false 默认值:false 如果帐户需要,allowCleartextPasswords = true允许使用明文客户端插件,例如使用PAM认证插件定义的插件。在一些配置中以明文形式发送密码可能是一个安全问题。为了避免问题,如果有可能的密码被拦截,客户端应该使用保护密码的方法连接到MySQL服务器。可能性包括TLS / SSL,IPsec或专用网络。 allowNativePasswords 类型:bool 有效值:true,false 默认值:true allowNativePasswords = false不允许使用MySQL本地密码方法。 allowOldPasswords 类型:bool 有效值:true,false 默认值:false allowOldPasswords = true允许使用不安全的旧密码方法。应该避免这种情况,但在某些情况下是必要的。另请参阅old_passwords维基页面。 字符集 类型:字符串 有效值: 默认值:无 设置用于客户端 - 服务器交互的字符集(“SET NAMES ”)。如果设置了多个字符集(用逗号分隔),则如果设置字符集失败,则使用以下字符集。这使得例如支持utf8mb4(在MySQL 5.5.3中引入),对于较旧的服务器(charset = utf8mb4,utf8)具有后备到utf8。 不鼓励使用charset参数,因为它会向服务器发出其他查询。除非您需要回退行为,否则请使用排序规则。 整理 类型:字符串 有效值: 默认值:utf8_general_ci 设置连接时用于客户端 - 服务器交互的归类。与charset相反,排序规则不会发出其他查询。如果目标服务器上指定的排序规则不可用,则连接将失败。 使用SHOW COLLATION可以检索服务器的有效字符集列表。 clientFoundRows 类型:bool 有效值:true,false 默认值:false clientFoundRows = true会导致UPDATE返回匹配行数,而不是更改的行数。 columnsWithAlias 类型:bool 有效值:true,false 默认值:false 当columnsWithAlias为true时,对sql.Rows.Columns()的调用将返回表别名和列名称以点分隔。例如: SELECT u.id FROM users as u 如果columnsWithAlias = true,那么将返回u.id而不是id。 interpolateParams 类型:bool 有效值:true,false 默认值:false 如果interpolateParams为true,则调用db.Query()和db.Exec()中的占位符(?)将内插到具有给定参数的单个查询字符串中。这减少了往返次数,因为驱动程序必须准备一个语句,用给定的参数执行它,并用interpolateParams = false再次关闭语句。 这不能与多字节编码BIG5,CP932,GB2312,GBK或S​​JIS一起使用。这些被列入黑名单,因为它们可能会引入SQL注入漏洞! LOC 类型:字符串 有效值: 默认值:UTC 设置time.Time值的位置(使用parseTime = true时)。 “本地”设置系统的位置。详情请参阅time.LoadLocation。 请注意,这将设置time.Time值的位置,但不会更改MySQL的time_zone设置。为了看到time_zone系统变量,也可以将其设置为DSN参数。 请记住,参数值必须是url.QueryEscape'ed。或者,您可以手动替换/ /%2F。例如,美国/太平洋地区将为=美国%2FPacific。 maxAllowedPacket 类型:十进制数 默认值:0 最大包大小允许以字节为单位。使用maxAllowedPacket = 0自动从服务器提取max_allowed_pa​​cket变量。 multiStatements 类型:bool 有效值:true,false 默认值:false 在一个查询中允许多个语句。虽然这允许批量查询,但它也大大增加了SQL注入的风险。只返回第一个查询的结果,所有其他结果都被静默地丢弃。 当使用multiStatements时,?参数只能在第一个语句中使用。 分析时 类型:bool 有效值:true,false 默认值:false parseTime = true将DATE和DATETIME值的输出类型更改为time.Time而不是[] byte / string readTimeout 类型:持续时间 默认值:0 I / O读取超时。该值必须是单位后缀(“ms”,“s”,“m”,“h”)的十进制数字,例如“30s”,“0.5m”或“1m30s”。 rejectReadOnly 类型:bool 有效值:true,false 默认值:false rejectreadOnly = true会导致驱动程序拒绝只读连接。这是在自动故障切换期间的可能的竞争条件,其中mysql客户端在故障切换后连接到只读副本。 请注意,这应该是一个相当罕见的情况,因为主要故障时通常会发生自动故障转移,并且竞争条件不应发生,除非在故障转移启动后立即重新启动。另一方面,当这种情况发生时,MySQL应用程序可能会停留在只读连接上,直到重新启动。然而,相当容易重现,例如,在AWS Aurora的MySQL兼容集群上使用手动故障切换。 如果您不依赖只读事务来拒绝不应该发生的写入,那么在某些MySQL提供程序(如AWS Aurora)上设置此操作对于故障转移是更安全的。 严格 类型:bool 有效值:true,false 默认值:false strict = true使得驱动程序严格模式可以将MySQL警告视为错误。在生产中不应该使用此模式,因为在某些情况下可能会导致数据损坏。 可以通过sql_mode系统变量设置服务器端的严格模式,这对于生产使用是安全的。 默认情况下,MySQL还将注释作为警告。使用sql_notes = false来忽略注释。 时间到 类型:持续时间 默认值:OS默认值 超时用于建立连接,也称为拨号超时。该值必须是具有单位后缀(“ms”,“s”,“m”,“h”),如“30s”,“0.5m”或“1m30s”的十进制数字。 TLS 类型:bool / string 有效值:true,false,skip-verify, 默认值:false tls = true启用与服务器的TLS / SSL加密连接。如果要使用自签名或无效的证书(服务器端),请使用跳过验证。使用注册到mysql.RegisterTLSConfig的自定义值。 writeTimeout 类型:持续时间 默认值:0 I / O写入超时。该值必须是具有单位后缀(“ms”,“s”,“m”,“h”),如“30s”,“0.5m”或“1m30s”的十进制数字。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
golang连接 MySQL 数据库需要使用第三方库,比如 `go-sql-driver/mysql`。以下是连接 MySQL 数据库的示例代码: ```go import ( "database/sql" "fmt" _ "github.com/go-sql-driver/mysql" ) func main() { // 数据库连接信息 user := "root" password := "password" host := "localhost" port := "3306" dbname := "test" // 创建数据库连接 db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s:%s)/%s", user, password, host, port, dbname)) if err != nil { panic(err.Error()) } defer db.Close() // 测试连接是否成功 err = db.Ping() if err != nil { panic(err.Error()) } fmt.Println("Connected to MySQL database!") } ``` 上述代码中,`sql.Open` 函数用于创建一个数据库连接参数一是驱动名称,参数二是连接字符串。连接字符串中需要包含用户名、密码、主机名、端口号和数据库名等信息。`db.Ping()` 函数用于测试连接是否成功。 连接成功后,可以使用 `db.Query` 或 `db.Exec` 函数执行 SQL 语句。例如: ```go // 查询数据 rows, err := db.Query("SELECT * FROM users") if err != nil { panic(err.Error()) } defer rows.Close() for rows.Next() { var id int var name string var age int err := rows.Scan(&id, &name, &age) if err != nil { panic(err.Error()) } fmt.Printf("ID: %d, Name: %s, Age: %d\n", id, name, age) } // 插入数据 result, err := db.Exec("INSERT INTO users (name, age) VALUES (?, ?)", "Tom", 18) if err != nil { panic(err.Error()) } id, err := result.LastInsertId() if err != nil { panic(err.Error()) } fmt.Println("Last inserted ID:", id) ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值