4.2.2.6 Option Defaults, Options Expecting Values, and the = Sign选项默认值、选项期望值和=符号

By convention, long forms of options that assign a value are written with an equals (=) sign, like this:按照惯例,赋值的长形式选项都用等号(=)表示,如下所示:

mysql --host=tonfisk --user=jon

For options that require a value (that is, not having a default value), the equal sign is not required, and so the following is also valid:对于需要值(即没有默认值)的选项,不需要等号,因此以下内容也有效:

mysql --host tonfisk --user jon

In both cases, the mysql client attempts to connect to a MySQL server running on the host named tonfisk using an account with the user name jon.在这两种情况下,mysql客户端都会尝试使用用户名为“jon”的帐户连接到运行在名为“tonfisk”的主机上的mysql服务器。

Due to this behavior, problems can occasionally arise when no value is provided for an option that expects one. 由于这种行为,当没有为期望值的选项提供值时,偶尔会出现问题。Consider the following example, where a user connects to a MySQL server running on host tonfisk as user jon:考虑以下示例,其中用户以用户jon连接到主机tonfisk上运行的MySQL服务器:

shell> mysql --host 85.224.35.45 --user jon
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 8.0.25 Source distribution

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> SELECT CURRENT_USER();
+----------------+
| CURRENT_USER() |
+----------------+
| jon@%          |
+----------------+
1 row in set (0.00 sec)

Omitting the required value for one of these option yields an error, such as the one shown here:省略其中一个选项的必需值会产生错误,如下所示:

shell> mysql --host 85.224.35.45 --user
mysql: option '--user' requires an argument

In this case, mysql was unable to find a value following the --user option because nothing came after it on the command line. 在这种情况下,mysql无法在--user选项后面找到值,因为命令行上没有任何值。However, if you omit the value for an option that is not the last option to be used, you obtain a different error that you may not be expecting:但是,如果省略了不是最后一个要使用的选项的值,则会得到一个您可能没有预料到的不同错误:

shell> mysql --host --user jon
ERROR 2005 (HY000): Unknown MySQL server host '--user' (1)

Because mysql assumes that any string following --host on the command line is a host name, --host --user is interpreted as --host=--user, and the client attempts to connect to a MySQL server running on a host named --user.因为mysql假设命令行上--host后面的任何字符串都是主机名,--host--user被解释为--host=--user,客户端尝试连接到运行在名为“--user”的主机上的mysql服务器。

Options having default values always require an equal sign when assigning a value; failing to do so causes an error. 具有默认值的选项在赋值时总是需要等号;否则会导致错误。For example, the MySQL server --log-error option has the default value host_name.err, where host_name is the name of the host on which MySQL is running. 例如,MySQL服务器--log-error选项的默认值为host_name.err,其中host_name是运行MySQL的主机的名称。Assume that you are running MySQL on a computer whose host name is tonfisk, and consider the following invocation of mysqld_safe:假设您在主机名为“tonfisk”的计算机上运行MySQL,并考虑以下mysqld_safe的调用:

shell> mysqld_safe &
[1] 11699
shell> 080112 12:53:40 mysqld_safe Logging to '/usr/local/mysql/var/tonfisk.err'.
080112 12:53:40 mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/var
shell>

After shutting down the server, restart it as follows:关闭服务器后,按如下步骤重新启动:

shell> mysqld_safe --log-error &
[1] 11699
shell> 080112 12:53:40 mysqld_safe Logging to '/usr/local/mysql/var/tonfisk.err'.
080112 12:53:40 mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/var
shell>

The result is the same, since --log-error is not followed by anything else on the command line, and it supplies its own default value. 结果是一样的,因为--log-error后面没有命令行上的其他任何内容,并且它提供了自己的默认值。(The & character tells the operating system to run MySQL in the background; it is ignored by MySQL itself.) &字符告诉操作系统在后台运行MySQL;它被MySQL本身忽略。)Now suppose that you wish to log errors to a file named my-errors.err. 现在假设您希望将错误记录到名为my-errors.err的文件中。You might try starting the server with --log-error my-errors, but this does not have the intended effect, as shown here:您可以尝试使用--log-error my-errors启动服务器,但这并没有达到预期的效果,如下所示:

shell> mysqld_safe --log-error my-errors &
[1] 31357
shell> 080111 22:53:31 mysqld_safe Logging to '/usr/local/mysql/var/tonfisk.err'.
080111 22:53:32 mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/var
080111 22:53:34 mysqld_safe mysqld from pid file /usr/local/mysql/var/tonfisk.pid ended

[1]+  Done                    ./mysqld_safe --log-error my-errors

The server attempted to start using /usr/local/mysql/var/tonfisk.err as the error log, but then shut down. Examining the last few lines of this file shows the reason:服务器试图开始使用/usr/local/mysql/var/tonfisk.err作为错误日志,但随后关闭。检查此文件的最后几行可以看出原因:

shell> tail /usr/local/mysql/var/tonfisk.err
2013-09-24T15:36:22.278034Z 0 [ERROR] Too many arguments (first extra is 'my-errors').
2013-09-24T15:36:22.278059Z 0 [Note] Use --verbose --help to get a list of available options!
2013-09-24T15:36:22.278076Z 0 [ERROR] Aborting
2013-09-24T15:36:22.279704Z 0 [Note] InnoDB: Starting shutdown...
2013-09-24T15:36:23.777471Z 0 [Note] InnoDB: Shutdown completed; log sequence number 2319086
2013-09-24T15:36:23.780134Z 0 [Note] mysqld: Shutdown complete

Because the --log-error option supplies a default value, you must use an equal sign to assign a different value to it, as shown here:因为--log-error选项提供了一个默认值,所以必须使用等号为其分配一个不同的值,如下所示:

shell> mysqld_safe --log-error=my-errors &
[1] 31437
shell> 080111 22:54:15 mysqld_safe Logging to '/usr/local/mysql/var/my-errors.err'.
080111 22:54:15 mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/var

shell>

Now the server has been started successfully, and is logging errors to the file /usr/local/mysql/var/my-errors.err.现在服务器已成功启动,并将错误记录到/usr/local/mysql/var/my-errors.err文件中。

Similar issues can arise when specifying option values in option files. 在选项文件中指定选项值时可能会出现类似的问题。For example, consider a my.cnf file that contains the following:例如,考虑一个包含以下内容的my.cnf文件:

[mysql]

host
user

When the mysql client reads this file, these entries are parsed as --host --user or --host=--user, with the result shown here:mysql客户端读取此文件时,这些条目被解析为--host --user--host=--user,结果如下:

shell> mysql
ERROR 2005 (HY000): Unknown MySQL server host '--user' (1)

However, in option files, an equal sign is not assumed. Suppose the my.cnf file is as shown here:但是,在选项文件中,不假定等号。假设my.cnf文件如下所示:

[mysql]

user jon

Trying to start mysql in this case causes a different error:在这种情况下尝试启动mysql会导致不同的错误:

shell> mysql
mysql: unknown option '--user jon'

A similar error would occur if you were to write host tonfisk in the option file rather than host=tonfisk. Instead, you must use the equal sign:如果在选项文件中写入host tonfisk而不是host=tonfisk,也会出现类似的错误。相反,您必须使用等号:

[mysql]

user=jon

Now the login attempt succeeds:现在登录尝试成功:

shell> mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 8.0.25 Source distribution

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> SELECT USER();
+---------------+
| USER()        |
+---------------+
| jon@localhost |
+---------------+
1 row in set (0.00 sec)

This is not the same behavior as with the command line, where the equal sign is not required:这与不需要等号的命令行行为不同:

shell> mysql --user jon --host tonfisk
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 8.0.25 Source distribution

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> SELECT USER();
+---------------+
| USER()        |
+---------------+
| jon@tonfisk   |
+---------------+
1 row in set (0.00 sec)

Specifying an option requiring a value without a value in an option file causes the server to abort with an error.指定一个选项,要求选项文件中没有值的值,会导致服务器中止并出错。