按照惯例,分配值的选项的长格式使用等号 (=
) 书写,如下所示
Press CTRL+C to copymysql --host=tonfisk --user=jon
对于需要值的选项(即没有默认值的选项),不需要等号,因此以下写法也是有效的
Press CTRL+C to copymysql --host tonfisk --user jon
在这两种情况下,mysql 客户端都会尝试使用用户名为 “jon” 的帐户连接到名为 “tonfisk” 的主机上运行的 MySQL 服务器。
由于这种行为,当没有为需要值的选项提供值时,偶尔会出现问题。请考虑以下示例,其中用户以用户 jon
的身份连接到主机 tonfisk
上运行的 MySQL 服务器
Press CTRL+C to copy$> 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: 9.0.0 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)
省略其中一个选项的必需值会导致错误,如下所示
Press CTRL+C to copy$> mysql --host 85.224.35.45 --user mysql: option '--user' requires an argument
在这种情况下,mysql 无法在 --user
选项后找到值,因为命令行中没有其他内容。但是,如果您省略了 不是 要使用的最后一个选项的值,则会收到您可能预料不到的错误
Press CTRL+C to copy$> mysql --host --user jon ERROR 2005 (HY000): Unknown MySQL server host '--user' (1)
因为 mysql 假定命令行中 --host
后面的任何字符串都是主机名,所以 --host
--user
被解释为 --host=--user
,并且客户端尝试连接到名为 “--user” 的主机上运行的 MySQL 服务器。
具有默认值的选项在分配值时始终需要等号;否则会导致错误。例如,MySQL 服务器的 --log-error
选项的默认值为
,其中 host_name
.errhost_name
是运行 MySQL 的主机名。假设您在主机名为 “tonfisk” 的计算机上运行 MySQL,请考虑以下调用 mysqld_safe 的方式
Press CTRL+C to copy$> mysqld_safe & [1] 11699 $> 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 $>
关闭服务器后,按如下方式重新启动它
Press CTRL+C to copy$> mysqld_safe --log-error & [1] 11699 $> 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 $>
结果是一样的,因为命令行中 --log-error
后面没有其他内容,它会提供自己的默认值。(&
字符告诉操作系统在后台运行 MySQL;它被 MySQL 本身忽略。)现在假设您希望将错误记录到名为 my-errors.err
的文件中。您可以尝试使用 --log-error my-errors
启动服务器,但这不会产生预期的效果,如下所示
Press CTRL+C to copy$> mysqld_safe --log-error my-errors & [1] 31357 $> 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
服务器尝试使用 /usr/local/mysql/var/tonfisk.err
作为错误日志启动,但随后关闭。检查此文件的最后几行可以看出原因
Press CTRL+C to copy$> 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
因为 --log-error
选项提供了默认值,所以您必须使用等号为其分配不同的值,如下所示
Press CTRL+C to copy$> mysqld_safe --log-error=my-errors & [1] 31437 $> 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 $>
现在服务器已成功启动,并正在将错误记录到 /usr/local/mysql/var/my-errors.err
文件中。
在选项文件中指定选项值时,也会出现类似的问题。例如,考虑包含以下内容的 my.cnf
文件
Press CTRL+C to copy[mysql] host user
当 mysql 客户端读取此文件时,这些条目将被解析为 --host
--user
或 --host=--user
,结果如下所示
Press CTRL+C to copy$> mysql ERROR 2005 (HY000): Unknown MySQL server host '--user' (1)
但是,在选项文件中,不会假定等号。假设 my.cnf
文件如下所示
Press CTRL+C to copy[mysql] user jon
在这种情况下,尝试启动 mysql 会导致不同的错误
Press CTRL+C to copy$> mysql mysql: unknown option '--user jon'
如果您在选项文件中编写 host tonfisk
而不是 host=tonfisk
,则会出现类似的错误。相反,您必须使用等号
Press CTRL+C to copy[mysql] user=jon
现在登录尝试成功
Press CTRL+C to copy$> mysql Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 5 Server version: 9.0.0 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)
这与命令行的行为不同,在命令行中不需要等号
Press CTRL+C to copy$> mysql --user jon --host tonfisk Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 6 Server version: 9.0.0 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)
在选项文件中指定一个需要值但没有值的选项会导致服务器中止并报错。