golang 连接mysql 参数备忘

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010136741/article/details/77978208

git 地址

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

Parameters

Parameters are case-sensitive!

Notice that any of trueTRUETrue or 1 is accepted to stand for a true boolean value. Not surprisingly, false can be specified as any of: falseFALSEFalse 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. Might be insecure!

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:   <name>
Default:        none

Sets the charset used for client-server interaction ("SET NAMES <value>"). 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:   <name>
Default:        utf8_general_ci

Sets the collation used for client-server interaction on connection. In contrast to charsetcollation 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:   <escaped name>
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, <name>
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维基页面。


字符集


类型:字符串
有效值:<name>
默认值:无
设置用于客户端 - 服务器交互的字符集(“SET NAMES <value>”)。如果设置了多个字符集(用逗号分隔),则如果设置字符集失败,则使用以下字符集。这使得例如支持utf8mb4(在MySQL 5.5.3中引入),对于较旧的服务器(charset = utf8mb4,utf8)具有后备到utf8。


不鼓励使用charset参数,因为它会向服务器发出其他查询。除非您需要回退行为,否则请使用排序规则。


整理


类型:字符串
有效值:<name>
默认值: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,<name>
默认值:false
tls = true启用与服务器的TLS / SSL加密连接。如果要使用自签名或无效的证书(服务器端),请使用跳过验证。使用注册到mysql.RegisterTLSConfig的自定义值。


writeTimeout


类型:持续时间
默认值:0
I / O写入超时。该值必须是具有单位后缀(“ms”,“s”,“m”,“h”),如“30s”,“0.5m”或“1m30s”的十进制数字。

猜你喜欢

转载自blog.csdn.net/u010136741/article/details/77978208
今日推荐