8.4 中文镜像

MySQL 8.4 参考手册 · 中文镜像

正文为中文,英文原文默认隐藏。点击任意中文段落即可展开 / 收起对应英文原文。

打开官方英文原文 ↗

译文由机器翻译生成,仅供理解参考;有疑问请以官方英文原文为准。

MySQL 8.4 参考手册 / 6 个 MySQL 程序 / 6.2 使用 MySQL 程序 / 6.2.5 使用类似 URI 的字符串或键值对连接到服务器
中文点击中文段落可显示 / 隐藏英文原文本页译文覆盖 98%

6.2.5 使用类似 URI 的字符串或键值对连接到服务器6.2.5 Connecting to the Server Using URI-Like Strings or Key-Value Pairs

This section describes use of URI-like connection strings or key-value pairs to specify how to establish connections to the MySQL server, for clients such as MySQL Shell. For information on establishing connections using command-line options, for clients such as mysql或者mysqldump, see Section 6.2.4, “Connecting to the MySQL Server Using Command Options”. For additional information if you are unable to connect, see Section 8.2.22, “Troubleshooting Problems Connecting to MySQL”.This section describes use of URI-like connection strings or key-value pairs to specify how to establish connections to the MySQL server, for clients such as MySQL Shell. For information on establishing connections using command-line options, for clients such as mysql or mysqldump, see Section 6.2.4, “Connecting to the MySQL Server Using Command Options”. For additional information if you are unable to connect, see Section 8.2.22, “Troubleshooting Problems Connecting to MySQL”.

Note

术语 “类 URI” 表示类似于但不完全相同于 RFC 3986 定义的 URI(统一资源标识符)语法的连接字符串语法。The term “URI-like” signifies connection-string syntax that is similar to but not identical to the URI (uniform resource identifier) syntax defined by RFC 3986.

以下 MySQL 客户端支持使用类似 URI 的连接字符串或键值对连接到 MySQL 服务器:The following MySQL clients support connecting to a MySQL server using a URI-like connection string or key-value pairs:

  • MySQL Shell

  • 实现 X DevAPI 的 MySQL 连接器MySQL Connectors which implement X DevAPI

本节记录了所有有效的类似 URI 的字符串和键值对连接参数,其中许多参数与命令行选项指定的参数类似:This section documents all valid URI-like string and key-value pair connection parameters, many of which are similar to those specified with command-line options:

  • 使用类似 URI 的字符串指定的参数,语法如 myuser@example.com:3306/main-schema。完整语法,请参见 使用类似 URI 的连接字符串。Parameters specified with a URI-like string use a syntax such as myuser@example.com:3306/main-schema. For the full syntax, see Connecting Using URI-Like Connection Strings.

  • 使用键值对指定的参数,语法如 {user:'myuser', host:'example.com', port:3306, schema:'main-schema'}。完整语法,请参见 使用键值对。Parameters specified with key-value pairs use a syntax such as {user:'myuser', host:'example.com', port:3306, schema:'main-schema'}. For the full syntax, see Connecting Using Key-Value Pairs.

连接参数不区分大小写。每个参数如果指定,只能指定一次。如果某个参数被指定多次,会发生错误。Connection parameters are not case-sensitive. Each parameter, if specified, can be given only once. If a parameter is specified more than once, an error occurs.

本节涵盖以下主题:This section covers the following topics:

基础连接参数Base Connection Parameters

以下讨论描述了在指定 MySQL 连接时可用的参数。这些参数可以通过符合基本 URI 样式语法的字符串提供(参见 Using URI-Like Connection Strings 进行连接),也可以作为键值对提供(参见 Using Key-Value Pairs 进行连接)。The following discussion describes the parameters available when specifying a connection to MySQL. These parameters can be provided using either a string that conforms to the base URI-like syntax (see Connecting Using URI-Like Connection Strings), or as key-value pairs (see Connecting Using Key-Value Pairs).

  • scheme:要使用的传输协议。使用 mysqlx 进行 X 协议连接,使用 mysql 进行经典 MySQL 协议连接。如果未指定协议,服务器将尝试自行猜测协议。支持 DNS SRV 的连接器可以使用 mysqlx+srv 方案(请参阅 使用 DNS SRV 记录的连接)。scheme: The transport protocol to use. Use mysqlx for X Protocol connections and mysql for classic MySQL protocol connections. If no protocol is specified, the server attempts to guess the protocol. Connectors that support DNS SRV can use the mysqlx+srv scheme (see Connections Using DNS SRV Records).

  • user:用于身份验证过程的 MySQL 用户账户。user: The MySQL user account to provide for the authentication process.

  • password:用于身份验证过程的密码。password: The password to use for the authentication process.

    Warning

    在连接规范中明确指定密码是不安全的,不推荐这样做。后面的讨论将展示如何让密码提示以交互方式出现。Specifying an explicit password in the connection specification is insecure and not recommended. Later discussion shows how to cause an interactive prompt for the password to occur.

  • host:服务器实例运行的主机。该值可以是主机名、IPv4 地址或 IPv6 地址。如果未指定主机,默认值为 localhost。host: The host on which the server instance is running. The value can be a host name, IPv4 address, or IPv6 address. If no host is specified, the default is localhost.

  • port:目标 MySQL 服务器监听连接的 TCP/IP 网络端口。如果未指定端口,X 协议连接的默认端口为 33060,经典 MySQL 协议连接的默认端口为 3306。port: The TCP/IP network port on which the target MySQL server is listening for connections. If no port is specified, the default is 33060 for X Protocol connections and 3306 for classic MySQL protocol connections.

  • socket:Unix 套接字文件的路径或 Windows 命名管道的名称。值是本地文件路径。在类 URI 字符串中,它们必须被编码,可以使用百分号编码或者将路径用括号括起来。括号可以避免对诸如 / 目录分隔符等字符进行百分号编码。例如,要使用 Unix 套接字 /tmp/mysql.sock 以 root@localhost 身份连接,可以使用百分号编码指定路径:root@localhost?socket=%2Ftmp%2Fmysql.sock,或者使用括号指定:root@localhost?socket=(/tmp/mysql.sock)。socket: The path to a Unix socket file or the name of a Windows named pipe. Values are local file paths. In URI-like strings, they must be encoded, using either percent encoding or by surrounding the path with parentheses. Parentheses eliminate the need to percent encode characters such as the / directory separator character. For example, to connect as root@localhost using the Unix socket /tmp/mysql.sock, specify the path using percent encoding as root@localhost?socket=%2Ftmp%2Fmysql.sock, or using parentheses as root@localhost?socket=(/tmp/mysql.sock).

  • schema:连接的默认数据库。如果未指定数据库,则连接没有默认数据库。schema: The default database for the connection. If no database is specified, the connection has no default database.

在 Unix 上处理 localhost 取决于传输协议的类型。使用经典 MySQL 协议的连接处理 localhost 的方式与其他 MySQL 客户端相同,这意味着 localhost 被认为是基于套接字的连接。对于使用 X 协议的连接,localhost 的行为有所不同,它被认为表示环回地址,例如 IPv4 地址 127.0.0.1。The handling of localhost on Unix depends on the type of transport protocol. Connections using classic MySQL protocol handle localhost the same way as other MySQL clients, which means that localhost is assumed to be for socket-based connections. For connections using X Protocol, the behavior of localhost differs in that it is assumed to represent the loopback address, for example, IPv4 address 127.0.0.1.

附加连接参数Additional Connection parameters

您可以为连接指定选项,可以通过在类似 URI 的字符串中附加 ?attribute=value 的属性,也可以以键值对的形式指定。以下选项可用:You can specify options for the connection, either as attributes in a URI-like string by appending ?attribute=value, or as key-value pairs. The following options are available:

  • ssl-mode:连接所需的安全状态。允许的模式如下:ssl-mode: The desired security state for the connection. The following modes are permissible:

    • DISABLEDDISABLED

    • PREFERREDPREFERRED

    • REQUIREDREQUIRED

    • VERIFY_CAVERIFY_CA

    • VERIFY_IDENTITYVERIFY_IDENTITY

    Important

    VERIFY_CA 和 VERIFY_IDENTITY 比默认的 PREFERRED 更好,因为它们有助于防止中间人攻击。VERIFY_CA and VERIFY_IDENTITY are better choices than the default PREFERRED, because they help prevent man-in-the-middle attacks.

    有关这些模式的信息,请参阅 --ssl-mode 选项说明,位于 加密连接的命令选项。For information about these modes, see the --ssl-mode option description in Command Options for Encrypted Connections.

  • ssl-ca:X.509 证书授权文件的 PEM 格式路径。ssl-ca: The path to the X.509 certificate authority file in PEM format.

  • ssl-capath:包含 X.509 证书授权文件的目录路径,文件为 PEM 格式。ssl-capath: The path to the directory that contains the X.509 certificates authority files in PEM format.

  • ssl-cert:PEM 格式的 X.509 证书文件路径。ssl-cert: The path to the X.509 certificate file in PEM format.

  • ssl-cipher:用于支持 TLS 协议到 TLSv1.2 的连接的加密算法。ssl-cipher: The encryption cipher to use for connections that use TLS protocols up through TLSv1.2.

  • ssl-crl:包含 PEM 格式证书吊销列表的文件路径。ssl-crl: The path to the file that contains certificate revocation lists in PEM format.

  • ssl-crlpath:包含 PEM 格式证书吊销列表文件的目录路径。ssl-crlpath: The path to the directory that contains certificate revocation-list files in PEM format.

  • ssl-key:PEM 格式的 X.509 密钥文件路径。ssl-key: The path to the X.509 key file in PEM format.

  • tls-version:用于经典 MySQL 协议加密连接的允许 TLS 协议。此选项仅由 MySQL Shell 支持。tls-version(单数)的值是以逗号分隔的列表,例如 TLSv1.2,TLSv1.3。详情请参见 第 8.3.2 节,“加密连接 TLS 协议与密码”。此选项依赖于 ssl-mode 选项未设置为 DISABLED。tls-version: The TLS protocols permitted for classic MySQL protocol encrypted connections. This option is supported by MySQL Shell only. The value of tls-version (singular) is a comma separated list, for example TLSv1.2,TLSv1.3. For details, see Section 8.3.2, “Encrypted Connection TLS Protocols and Ciphers”. This option depends on the ssl-mode option not being set to DISABLED.

  • tls-versions:用于加密 X 协议连接的允许 TLS 协议。tls-versions(复数)的值是一个数组,例如 [TLSv1.2,TLSv1.3]。详情请参见 第 8.3.2 节,“加密连接 TLS 协议与密码套件”。此选项依赖于 ssl-mode 选项未设置为 DISABLED。tls-versions: The permissible TLS protocols for encrypted X Protocol connections. The value of tls-versions (plural) is an array such as [TLSv1.2,TLSv1.3]. For details, see Section 8.3.2, “Encrypted Connection TLS Protocols and Ciphers”. This option depends on the ssl-mode option not being set to DISABLED.

  • tls-ciphersuites:允许的 TLS 密码套件。tls-ciphersuites 的值是 IANA 密码套件名称的列表,如 TLS 密码套件 所列。详情请参见 第 8.3.2 节,“加密连接 TLS 协议和密码”。此选项依赖于 ssl-mode 选项未设置为 DISABLED。tls-ciphersuites: The permitted TLS cipher suites. The value of tls-ciphersuites is a list of IANA cipher suite names as listed at TLS Ciphersuites. For details, see Section 8.3.2, “Encrypted Connection TLS Protocols and Ciphers”. This option depends on the ssl-mode option not being set to DISABLED.

  • auth-method:用于连接的认证方法。默认值为 AUTO,意味着服务器会尝试自动判断。允许使用以下方法:auth-method: The authentication method to use for the connection. The default is AUTO, meaning that the server attempts to guess. The following methods are permissible:

    • AUTOAUTO

    • MYSQL41

    • SHA256_MEMORYSHA256_MEMORY

    • FROM_CAPABILITIESFROM_CAPABILITIES

    • FALLBACKFALLBACK

    • PLAINPLAIN

    对于 X 协议连接,任何配置的 auth-method 都会被覆盖为以下身份验证方法序列:MYSQL41、SHA256_MEMORY、PLAIN。For X Protocol connections, any configured auth-method is overridden to this sequence of authentication methods: MYSQL41, SHA256_MEMORY, PLAIN.

  • get-server-public-key:从服务器请求 RSA 密钥对密码交换所需的公钥。在使用经典 MySQL 协议与 SSL 模式 DISABLED 连接 MySQL 8 及以上版本服务器时使用。在这种情况下,你必须指定协议。例如:get-server-public-key: Request from the server the public key required for RSA key pair-based password exchange. Use when connecting to MySQL 8+ servers over classic MySQL protocol with SSL mode DISABLED. You must specify the protocol in this case. For example:

    mysql://user@localhost:3306?get-server-public-key=true
    

    此选项适用于使用 caching_sha2_password 认证插件进行身份验证的客户端。对于该插件,除非请求,否则服务器不会发送公钥。对于未使用该插件进行身份验证的账户,此选项将被忽略。如果未使用基于 RSA 的密码交换(例如客户端通过安全连接连接到服务器的情况),此选项也将被忽略。This option applies to clients that authenticate with the caching_sha2_password authentication plugin. For that plugin, the server does not send the public key unless requested. This option is ignored for accounts that do not authenticate with that plugin. It is also ignored if RSA-based password exchange is not used, as is the case when the client connects to the server using a secure connection.

    如果提供了 server-public-key-path=file_name 并指定了有效的公钥文件,则该设置优先于 get-server-public-key。If server-public-key-path=file_name is given and specifies a valid public key file, it takes precedence over get-server-public-key.

    有关 caching_sha2_password 插件的信息,请参见 第 8.4.1.2 节,“缓存 SHA-2 可插拔认证”。For information about the caching_sha2_password plugin, see Section 8.4.1.2, “Caching SHA-2 Pluggable Authentication”.

  • server-public-key-path: 指向 PEM 格式文件的路径,该文件包含服务器所需的客户端副本公钥,用于基于 RSA 密钥对的密码交换。当通过 SSL 模式 DISABLED 使用经典 MySQL 协议连接 MySQL 8+ 服务器时使用。server-public-key-path: The path name to a file in PEM format containing a client-side copy of the public key required by the server for RSA key pair-based password exchange. Use when connecting to MySQL 8+ servers over classic MySQL protocol with SSL mode DISABLED.

    此选项适用于使用 sha256_password(已弃用)或 caching_sha2_password 身份验证插件进行身份验证的客户端。对于不使用这些插件之一进行身份验证的账户,该选项将被忽略。如果未使用基于 RSA 的密码交换,例如客户端使用安全连接连接到服务器时,也将忽略此选项。This option applies to clients that authenticate with the sha256_password (deprecated) or caching_sha2_password authentication plugin. This option is ignored for accounts that do not authenticate with one of those plugins. It is also ignored if RSA-based password exchange is not used, as is the case when the client connects to the server using a secure connection.

    如果提供了 server-public-key-path=file_name 并指定了有效的公钥文件,则该设置优先于 get-server-public-key。If server-public-key-path=file_name is given and specifies a valid public key file, it takes precedence over get-server-public-key.

    有关 sha256_password(已弃用)和 caching_sha2_password 插件的信息,请参阅 第 8.4.1.3 节,“SHA-256 可插拔认证”,以及 第 8.4.1.2 节,“缓存 SHA-2 可插拔认证”。For information about the sha256_password (deprecated) and caching_sha2_password plugins, see Section 8.4.1.3, “SHA-256 Pluggable Authentication”, and Section 8.4.1.2, “Caching SHA-2 Pluggable Authentication”.

  • ssh:用于通过 SSH 隧道连接到 MySQL 服务器实例的 SSH 服务器的 URI。URI 格式为 [user@]host[:port]。使用 uri 选项指定目标 MySQL 服务器实例的 URI。有关从 MySQL Shell 使用 SSH 隧道连接的信息,请参阅 使用 SSH 隧道。ssh: The URI for connection to an SSH server to access a MySQL server instance using SSH tunneling. The URI format is [user@]host[:port]. Use the uri option to specify the URI of the target MySQL server instance. For information on SSH tunnel connections from MySQL Shell, see Using an SSH Tunnel.

  • uri:要通过从ssh选项指定的服务器建立的 SSH 隧道访问的 MySQL 服务器实例的 URI。URI 格式为[scheme://][user@]host[:port]。不要使用基本连接参数(scheme、user、host、port)来指定 SSH 隧道的 MySQL 服务器连接,只需使用uri选项。uri: The URI for a MySQL server instance that is to be accessed through an SSH tunnel from the server specified by the ssh option. The URI format is [scheme://][user@]host[:port]. Do not use the base connection parameters (scheme, user, host, port) to specify the MySQL server connection for SSH tunneling, just use the uri option.

  • ssh-password:连接到 SSH 服务器的密码。ssh-password: The password for the connection to the SSH server.

    Warning

    在连接规范中指定明确的密码是不安全的,不推荐这样做。MySQL Shell 在需要时会交互式地提示输入密码。Specifying an explicit password in the connection specification is insecure and not recommended. MySQL Shell prompts for a password interactively when one is required.

  • ssh-config-file:用于连接到 SSH 服务器的 SSH 配置文件。如果未指定此选项,可以使用 MySQL Shell 配置选项 ssh.configFile 将自定义文件设置为默认值。如果未设置 ssh.configFile,默认值为标准 SSH 配置文件 ~/.ssh/config。ssh-config-file: The SSH configuration file for the connection to the SSH server. You can use the MySQL Shell configuration option ssh.configFile to set a custom file as the default if this option is not specified. If ssh.configFile has not been set, the default is the standard SSH configuration file ~/.ssh/config.

  • ssh-identity-file:用于连接到 SSH 服务器的身份文件。如果未指定此选项,默认值为在 SSH 代理中配置的任何身份文件(如果使用),或在 SSH 配置文件中配置的文件,或 SSH 配置文件夹中的标准私钥文件 (~/.ssh/id_rsa)。ssh-identity-file: The identity file to use for the connection to the SSH server. The default if this option is not specified is any identity file configured in an SSH agent (if used), or in the SSH configuration file, or the standard private key file in the SSH configuration folder (~/.ssh/id_rsa).

  • ssh-identity-pass:用于 ssh-identity-file 选项指定的身份文件的密码短语。ssh-identity-pass: The passphrase for the identity file specified by the ssh-identity-file option.

    Warning

    在连接规范中指定明确的密码是不安全的,不推荐这样做。MySQL Shell 在需要时会交互式地提示输入密码。Specifying an explicit password in the connection specification is insecure and not recommended. MySQL Shell prompts for a password interactively when one is required.

  • connect-timeout:一个整数值,用于配置客户端(如 MySQL Shell)在停止尝试连接无响应的 MySQL 服务器之前等待的秒数。connect-timeout: An integer value used to configure the number of seconds that clients, such as MySQL Shell, wait until they stop trying to connect to an unresponsive MySQL server.

  • compression:此选项用于请求或禁用连接的压缩。compression: This option requests or disables compression for the connection.

    此选项可用的值为:required,请求压缩,如果服务器不支持则失败;preferred,请求压缩,如果失败则回退到未压缩连接;以及 disabled,请求未压缩连接,如果服务器不允许则失败。preferred 是 X 协议连接的默认值,而 disabled 是经典 MySQL 协议连接的默认值。有关 X Plugin 连接压缩控制的信息,请参见 第 22.5.5 节,“X Plugin 的连接压缩”。The values available for this option are: required, which requests compression and fails if the server does not support it; preferred, which requests compression and falls back to an uncompressed connection; and disabled, which requests an uncompressed connection and fails if the server does not permit those. preferred is the default for X Protocol connections, and disabled is the default for classic MySQL protocol connections. For information on X Plugin connection compression control, see Section 22.5.5, “Connection Compression with X Plugin”.

    Note

    不同的 MySQL 客户端对连接压缩的支持实现方式不同。有关详细信息,请查阅您客户端的文档。Different MySQL clients implement their support for connection compression differently. Consult your client's documentation for details.

  • compression-algorithms 和 compression-level:这些选项在 MySQL Shell 中可用,以便对连接压缩进行更多控制。您可以指定它们来选择用于连接的压缩算法,以及与该算法一起使用的数字压缩级别。您也可以使用 compression-algorithms 代替 compression 来请求连接压缩。有关 MySQL Shell 连接压缩控制的信息,请参见 使用压缩连接。compression-algorithms and compression-level: These options are available in MySQL Shell for more control over connection compression. You can specify them to select the compression algorithm used for the connection, and the numeric compression level used with that algorithm. You can also use compression-algorithms in place of compression to request compression for the connection. For information on MySQL Shell's connection compression control, see Using Compressed Connections.

  • connection-attributes:控制应用程序在连接服务器时传递的键值对。有关连接属性的一般信息,请参见 第 29.12.9 节,“性能模式连接属性表”。客户端通常定义一组默认属性,这些属性可以禁用或启用。例如:connection-attributes: Controls the key-value pairs that application programs pass to the server at connect time. For general information about connection attributes, see Section 29.12.9, “Performance Schema Connection Attribute Tables”. Clients usually define a default set of attributes, which can be disabled or enabled. For example:

    mysqlx://user@host?connection-attributes
    mysqlx://user@host?connection-attributes=true
    mysqlx://user@host?connection-attributes=false
    

    The default behavior is to send the default attribute set. Applications can specify attributes to be passed in addition to the default attributes. You specify additional connection attributes as a connection-attributes parameter in a connection string. The connection-attributes parameter value must be empty (the same as specifying true), a Boolean value (true或者false to enable or disable the default attribute set), or a list or zero or more key=value specifiers separated by commas (to be sent in addition to the default attribute set). Within a list, a missing key value evaluates as an empty string. Further examples:The default behavior is to send the default attribute set. Applications can specify attributes to be passed in addition to the default attributes. You specify additional connection attributes as a connection-attributes parameter in a connection string. The connection-attributes parameter value must be empty (the same as specifying true), a Boolean value (true or false to enable or disable the default attribute set), or a list or zero or more key=value specifiers separated by commas (to be sent in addition to the default attribute set). Within a list, a missing key value evaluates as an empty string. Further examples:

    mysqlx://user@host?connection-attributes=[attr1=val1,attr2,attr3=]
    mysqlx://user@host?connection-attributes=[]
    

    应用程序定义的属性名称不能以 _ 开头,因为此类名称保留给内部属性使用。Application-defined attribute names cannot begin with _ because such names are reserved for internal attributes.

使用类 URI 连接字符串进行连接Connecting Using URI-Like Connection Strings

您可以使用类 URI 的字符串指定与 MySQL 服务器的连接。此类字符串可以与 MySQL Shell 的 --uri 命令选项、MySQL Shell 的 \connect 命令以及实现 X DevAPI 的 MySQL 连接器一起使用。You can specify a connection to MySQL Server using a URI-like string. Such strings can be used with the MySQL Shell with the --uri command option, the MySQL Shell \connect command, and MySQL Connectors which implement X DevAPI.

Note

术语 “类 URI” 表示类似于但不完全相同于 RFC 3986 定义的 URI(统一资源标识符)语法的连接字符串语法。The term “URI-like” signifies connection-string syntax that is similar to but not identical to the URI (uniform resource identifier) syntax defined by RFC 3986.

类似 URI 的连接字符串具有以下语法:A URI-like connection string has the following syntax:

[scheme://][user[:[password]]@]host[:port][/schema][?attribute1=value1&attribute2=value2...
Important

在 URI 类字符串的各个元素中,必须对保留字符使用百分号编码。例如,如果您指定的字符串中包含 @ 字符,该字符必须被替换为 %40。如果在 IPv6 地址中包含区域 ID,则用作分隔符的 % 字符必须被替换为 %25。Percent encoding must be used for reserved characters in the elements of the URI-like string. For example, if you specify a string that includes the @ character, the character must be replaced by %40. If you include a zone ID in an IPv6 address, the % character used as the separator must be replaced with %25.

您可以在 URI 类连接字符串中使用的参数在 基础连接参数 中有所描述。The parameters you can use in a URI-like connection string are described at Base Connection Parameters.

MySQL Shell 的 shell.parseUri() 和 shell.unparseUri() 方法可用于拆解和组装类似 URI 的连接字符串。给定一个类似 URI 的连接字符串,shell.parseUri() 会返回一个包含字符串中每个元素的字典。shell.unparseUri() 将 URI 组件和连接选项的字典转换为用于连接 MySQL 的有效类似 URI 的连接字符串,可在 MySQL Shell 中使用,或由实现 X DevAPI 的 MySQL 连接器使用。MySQL Shell's shell.parseUri() and shell.unparseUri() methods can be used to deconstruct and assemble a URI-like connection string. Given a URI-like connection string, shell.parseUri() returns a dictionary containing each element found in the string. shell.unparseUri() converts a dictionary of URI components and connection options into a valid URI-like connection string for connecting to MySQL, which can be used in MySQL Shell or by MySQL Connectors which implement X DevAPI.

如果在类似 URI 的字符串中未指定密码(这是推荐做法),交互式客户端会提示输入密码。以下示例显示了如何使用用户名 user_name 指定类似 URI 的字符串。在每种情况下,都会提示输入密码。If no password is specified in the URI-like string, which is recommended, interactive clients prompt for the password. The following examples show how to specify URI-like strings with the user name user_name. In each case, the password is prompted for.

  • 一个 X 协议连接到本地服务器实例,监听端口 33065。An X Protocol connection to a local server instance listening at port 33065.

    mysqlx://user_name@localhost:33065
    
  • 一个经典的 MySQL 协议连接到本地服务器实例,监听端口 3333。A classic MySQL protocol connection to a local server instance listening at port 3333.

    mysql://user_name@localhost:3333
    
  • 一个 X 协议连接到远程服务器实例,使用主机名、IPv4 地址和 IPv6 地址。An X Protocol connection to a remote server instance, using a host name, an IPv4 address, and an IPv6 address.

    mysqlx://user_name@server.example.com/
    mysqlx://user_name@198.51.100.14:123
    mysqlx://user_name@[2001:db8:85a3:8d3:1319:8a2e:370:7348]
    
  • 一个使用套接字的 X 协议连接,路径可以使用百分号编码或括号提供。An X Protocol connection using a socket, with the path provided using either percent encoding or parentheses.

    mysqlx://user_name@/path%2Fto%2Fsocket.sock
    mysqlx://user_name@(/path/to/socket.sock)
    
  • 可以指定一个可选路径,表示一个数据库。An optional path can be specified, which represents a database.

    # use 'world' as the default database
    mysqlx://user_name@198.51.100.1/world
    
    # use 'world_x' as the default database, encoding _ as %5F
    mysqlx://user_name@198.51.100.2:33060/world%5Fx
    
  • 可以指定一个可选查询,由每个值组成,每个值以key=value对的形式或单个key的形式给出。要指定多个值,请用,字符分隔它们。可以混合使用key=value和值key。值可以是列表类型,列表值按出现顺序排列。字符串必须进行百分比编码或用括号括起来。以下内容等效。An optional query can be specified, consisting of values each given as a key=value pair or as a single key. To specify multiple values, separate them by , characters. A mix of key=value and key values is permissible. Values can be of type list, with list values ordered by appearance. Strings must be either percent encoded or surrounded by parentheses. The following are equivalent.

    ssluser@127.0.0.1?ssl-ca=%2Froot%2Fclientcert%2Fca-cert.pem\
    &ssl-cert=%2Froot%2Fclientcert%2Fclient-cert.pem\
    &ssl-key=%2Froot%2Fclientcert%2Fclient-key
    
    ssluser@127.0.0.1?ssl-ca=(/root/clientcert/ca-cert.pem)\
    &ssl-cert=(/root/clientcert/client-cert.pem)\
    &ssl-key=(/root/clientcert/client-key)
    
  • 指定用于加密连接的 TLS 版本和密码套件:To specify a TLS version and ciphersuite to use for encrypted connections:

    mysql://user_name@198.51.100.2:3306/world%5Fx?\
    tls-versions=[TLSv1.2,TLSv1.3]&tls-ciphersuites=[TLS_DHE_PSK_WITH_AES_128_\
    GCM_SHA256, TLS_CHACHA20_POLY1305_SHA256]
    

前面的示例假设连接需要密码。对于交互式客户端,登录提示会请求指定用户的密码。如果用户账户没有密码(这是不安全的,不推荐使用),或者正在使用套接字对等凭证认证(例如,使用 Unix 套接字连接),则必须在连接字符串中明确指定不提供密码,并且不需要密码提示。为此,在字符串中的 user_name 之后放置一个 :,但不要在其后指定密码。例如:The previous examples assume that connections require a password. With interactive clients, the specified user's password is requested at the login prompt. If the user account has no password (which is insecure and not recommended), or if socket peer-credential authentication is in use (for example, with Unix socket connections), you must explicitly specify in the connection string that no password is being provided and the password prompt is not required. To do this, place a : after the user_name in the string but do not specify a password after it. For example:

mysqlx://user_name:@localhost

使用键值对进行连接Connecting Using Key-Value Pairs

在 MySQL Shell 以及一些实现了 X DevAPI 的 MySQL 连接器中,您可以使用键值对来指定与 MySQL 服务器的连接,这些键值对以适合实现的自然语言结构提供。例如,您可以在 JavaScript 中以 JSON 对象的形式,或在 Python 中以字典的形式提供连接参数。无论键值对以何种方式提供,其概念都是相同的:本节中描述的键可以被分配值,用于指定连接。您可以在 MySQL Shell 的 shell.connect() 方法或 InnoDB Cluster 的 dba.createCluster() 方法中使用键值对来指定连接,也可以在一些实现了 X DevAPI 的 MySQL 连接器中使用这些键值对。In MySQL Shell and some MySQL Connectors which implement X DevAPI, you can specify a connection to MySQL Server using key-value pairs, supplied in language-natural constructs for the implementation. For example, you can supply connection parameters using key-value pairs as a JSON object in JavaScript, or as a dictionary in Python. Regardless of the way the key-value pairs are supplied, the concept remains the same: the keys as described in this section can be assigned values that are used to specify a connection. You can specify connections using key-value pairs in MySQL Shell's shell.connect() method or InnoDB Cluster's dba.createCluster() method, and with some of the MySQL Connectors which implement X DevAPI.

通常,键值对由 { 和 } 字符包围,并且 , 字符用于键值对之间的分隔。: 字符用于键与值之间,并且字符串必须加引号(例如,使用 ' 字符)。与类似 URI 的连接字符串不同,字符串不需要进行百分比编码。Generally, key-value pairs are surrounded by { and } characters and the , character is used as a separator between key-value pairs. The : character is used between keys and values, and strings must be delimited (for example, using the ' character). It is not necessary to percent encode strings, unlike URI-like connection strings.

以键值对指定的连接具有以下格式:A connection specified as key-value pairs has the following format:

{ key: value, key: value, ...}

您可以用作连接键的参数在基础连接参数中有所描述。The parameters you can use as keys for a connection are described at Base Connection Parameters.

如果在键值对中未指定密码(这是推荐的做法),交互式客户端会提示输入密码。以下示例显示了如何使用用户名称'user_name'通过键值对指定连接。在每种情况下,都会提示输入密码。If no password is specified in the key-value pairs, which is recommended, interactive clients prompt for the password. The following examples show how to specify connections using key-value pairs with the user name 'user_name'. In each case, the password is prompted for.

  • 一个 X 协议连接到本地服务器实例,监听端口 33065。An X Protocol connection to a local server instance listening at port 33065.

    {user:'user_name', host:'localhost', port:33065}
    
  • 一个经典的 MySQL 协议连接到本地服务器实例,监听端口 3333。A classic MySQL protocol connection to a local server instance listening at port 3333.

    {user:'user_name', host:'localhost', port:3333}
    
  • 一个 X 协议连接到远程服务器实例,使用主机名、IPv4 地址和 IPv6 地址。An X Protocol connection to a remote server instance, using a host name, an IPv4 address, and an IPv6 address.

    {user:'user_name', host:'server.example.com'}
    {user:'user_name', host:198.51.100.14:123}
    {user:'user_name', host:[2001:db8:85a3:8d3:1319:8a2e:370:7348]}
    
  • 使用套接字的 X 协议连接。An X Protocol connection using a socket.

    {user:'user_name', socket:'/path/to/socket/file'}
    
  • 可以指定一个可选的模式,它表示一个数据库。An optional schema can be specified, which represents a database.

    {user:'user_name', host:'localhost', schema:'world'}
    

前面的示例假设连接需要密码。对于交互式客户端,登录提示时会要求提供指定用户的密码。如果用户账户没有密码(这是不安全且不推荐的),或者正在使用套接字对等凭证认证(例如,使用 Unix 套接字连接),则必须明确指定未提供密码并且不需要密码提示。为此,可在 password 键后提供一个空字符串 ''。例如:The previous examples assume that connections require a password. With interactive clients, the specified user's password is requested at the login prompt. If the user account has no password (which is insecure and not recommended), or if socket peer-credential authentication is in use (for example, with Unix socket connections), you must explicitly specify that no password is being provided and the password prompt is not required. To do this, provide an empty string using '' after the password key. For example:

{user:'user_name', password:'', host:'localhost'}