文档首页
MySQL 8.4 参考手册
8.4 中文镜像

MySQL 8.4 参考手册 · 中文镜像

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

打开官方英文原文 ↗

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

中文点击中文段落可显示 / 隐藏英文原文本页译文覆盖 100%

8.2.1 账户用户名和密码8.2.1 Account User Names and Passwords

MySQL 将账户存储在 mysql 系统数据库的 user 表中。账户是以用户名和用户可以连接到服务器的客户端主机或主机组来定义的。有关 user 表中账户表示的信息,请参见 第 8.2.3 节,“授权表”。MySQL stores accounts in the user table of the mysql system database. An account is defined in terms of a user name and the client host or hosts from which the user can connect to the server. For information about account representation in the user table, see Section 8.2.3, “Grant Tables”.

一个账户也可能拥有认证凭证,如密码。这些凭证由账户认证插件处理。MySQL 支持多种认证插件。其中一些使用内置的认证方法,而另一些则通过外部认证方法进行认证。参见 第 8.2.17 节,“可插拔认证”。An account may also have authentication credentials such as a password. The credentials are handled by the account authentication plugin. MySQL supports multiple authentication plugins. Some of them use built-in authentication methods, whereas others enable authentication using external authentication methods. See Section 8.2.17, “Pluggable Authentication”.

MySQL 和您的操作系统在使用用户名和密码的方式上有几个区别:There are several distinctions between the way user names and passwords are used by MySQL and your operating system:

  • MySQL 用于身份验证的用户名与 Windows 或 Unix 使用的用户名(登录名)无关。在 Unix 上,大多数 MySQL 客户端默认尝试使用当前 Unix 用户名作为 MySQL 用户名登录,但这只是为了方便。默认值可以很容易地被覆盖,因为客户端程序允许通过 -u 或 --user 选项指定任何用户名。这意味着任何人都可以尝试使用任何用户名连接到服务器,因此除非所有 MySQL 帐户都有密码,否则无法以任何方式使数据库安全。任何为没有密码的帐户指定用户名的人都可以成功连接到服务器。User names, as used by MySQL for authentication purposes, have nothing to do with user names (login names) as used by Windows or Unix. On Unix, most MySQL clients by default try to log in using the current Unix user name as the MySQL user name, but that is for convenience only. The default can be overridden easily, because client programs permit any user name to be specified with a -u or --user option. This means that anyone can attempt to connect to the server using any user name, so you cannot make a database secure in any way unless all MySQL accounts have passwords. Anyone who specifies a user name for an account that has no password can connect successfully to the server.

  • MySQL 用户名最长为 32 个字符。操作系统用户名的最大长度可能不同。MySQL user names are up to 32 characters long. Operating system user names may have a different maximum length.

    Warning

    MySQL 用户名长度限制在 MySQL 服务器和客户端中是硬编码的,试图通过修改 mysql 数据库中表的定义来规避这一限制 是无效的。The MySQL user name length limit is hardcoded in MySQL servers and clients, and trying to circumvent it by modifying the definitions of the tables in the mysql database does not work.

    你绝不应以任何方式更改数据库中表的结构,除非通过mysql中描述的程序。尝试以其他方式重新定义 MySQL 系统表会导致未定义且不受支持的行为。服务器可以自由忽略因这些修改而变得格式错误的行。第 3 章,升级 MySQL为了对使用内置认证方法的账户进行客户端连接认证,服务器使用存储在You should never alter the structure of tables in the mysql database in any manner whatsoever except by means of the procedure that is described in Chapter 3, Upgrading MySQL. Attempting to redefine the MySQL system tables in any other fashion results in undefined and unsupported behavior. The server is free to ignore rows that become malformed as a result of such modifications.

  • 中的密码,这意味着服务器会返回列的头信息和user表格。这些密码与用于登录您的操作系统的密码不同。您用于登录 Windows 或 Unix 机器的外部密码与您用于访问该机器上 MySQL 服务器的密码之间没有必要的联系。“外部”密码。To authenticate client connections for accounts that use built-in authentication methods, the server uses passwords stored in the user table. These passwords are distinct from passwords for logging in to your operating system. There is no necessary connection between the “external” password you use to log in to a Windows or Unix machine and the password you use to access the MySQL server on that machine.

    如果服务器使用其他插件对客户端进行认证,插件实现的认证方法可能会使用也可能不会使用存储在 user 表中的密码。在这种情况下,也可能使用外部密码来对 MySQL 服务器进行认证。If the server authenticates a client using some other plugin, the authentication method that the plugin implements may or may not use a password stored in the user table. In this case, it is possible that an external password is also used to authenticate to the MySQL server.

  • 存储在 user 表中的密码使用特定插件的算法进行加密。Passwords stored in the user table are encrypted using plugin-specific algorithms.

  • 如果用户名和密码只包含 ASCII 字符,则可以不管字符集设置如何连接到服务器。要在用户名或密码包含非 ASCII 字符时启用连接,客户端应用程序应调用 mysql_options() C API 函数,并使用 MYSQL_SET_CHARSET_NAME 选项及相应的字符集名称作为参数。这将使身份验证使用指定的字符集进行。否则,除非服务器默认字符集与身份验证默认值中的编码相同,否则身份验证将失败。If the user name and password contain only ASCII characters, it is possible to connect to the server regardless of character set settings. To enable connections when the user name or password contain non-ASCII characters, client applications should call the mysql_options() C API function with the MYSQL_SET_CHARSET_NAME option and appropriate character set name as arguments. This causes authentication to take place using the specified character set. Otherwise, authentication fails unless the server default character set is the same as the encoding in the authentication defaults.

    标准 MySQL 客户端程序支持一个 --default-character-set 选项,该选项会导致 mysql_options() 按前述方式被调用。此外,如 第 12.4 节,“连接字符集和校对规则” 所述,还支持字符集自动检测。对于使用非 C API 的连接器的程序,该连接器可能提供一个等同于 mysql_options() 的功能,可替代使用。请查阅连接器文档。Standard MySQL client programs support a --default-character-set option that causes mysql_options() to be called as just described. In addition, character set autodetection is supported as described in Section 12.4, “Connection Character Sets and Collations”. For programs that use a connector that is not based on the C API, the connector may provide an equivalent to mysql_options() that can be used instead. Check the connector documentation.

    上述说明不适用于 ucs2、utf16 和 utf32,这些字符集不允许作为客户端字符集。The preceding notes do not apply for ucs2, utf16, and utf32, which are not permitted as client character sets.

MySQL 安装过程会使用初始的 root 账户来填充授权表,如 第 2.9.4 节,“保护初始 MySQL 账户” 所述,该节还讨论了如何为其分配密码。之后,你通常会使用诸如 CREATE USER、DROP USER、GRANT 和 REVOKE 等语句来设置、修改和删除 MySQL 账户。参见 第 8.2.8 节,“添加账户、分配权限和删除账户”,以及 第 15.7.1 节,“账户管理语句”。The MySQL installation process populates the grant tables with an initial root account, as described in Section 2.9.4, “Securing the Initial MySQL Account”, which also discusses how to assign a password to it. Thereafter, you normally set up, modify, and remove MySQL accounts using statements such as CREATE USER, DROP USER, GRANT, and REVOKE. See Section 8.2.8, “Adding Accounts, Assigning Privileges, and Dropping Accounts”, and Section 15.7.1, “Account Management Statements”.

要使用命令行客户端连接到 MySQL 服务器,请根据您要使用的账号指定用户名和密码选项:To connect to a MySQL server with a command-line client, specify user name and password options as necessary for the account that you want to use:

$> mysql --user=finley --password db_name

如果您更喜欢使用简短选项,命令如下所示:If you prefer short options, the command looks like this:

$> mysql -u finley -p db_name

如果在命令行中省略了 --password 或 -p 选项后面的密码值(如上所示),客户端会提示输入密码。或者,也可以在命令行中指定密码:If you omit the password value following the --password or -p option on the command line (as just shown), the client prompts for one. Alternatively, the password can be specified on the command line:

$> mysql --user=finley --password=password db_name
$> mysql -u finley -ppassword db_name

如果您使用-p选项,必须有不能有空格。如果未指定密码选项,默认是不发送密码。-p以及以下密码值。If you use the -p option, there must be no space between -p and the following password value.

在命令行上指定密码应被认为是不安全的。请参见第 8.1.2.1 节,“终端用户密码安全指南”。为了避免在命令行上提供密码,请使用选项文件或登录路径文件。请参见第 6.2.2.2 节,“使用选项文件”,以及第 6.6.7 节,“mysql_config_editor — MySQL 配置实用程序”。Specifying a password on the command line should be considered insecure. See Section 8.1.2.1, “End-User Guidelines for Password Security”. To avoid giving the password on the command line, use an option file or a login path file. See Section 6.2.2.2, “Using Option Files”, and Section 6.6.7, “mysql_config_editor — MySQL Configuration Utility”.

有关指定用户名、密码和其他连接参数的更多信息,请参见第 6.2.4 节,“使用命令选项连接到 MySQL 服务器”。For additional information about specifying user names, passwords, and other connection parameters, see Section 6.2.4, “Connecting to the MySQL Server Using Command Options”.