【问题标题】:How do you force a MySQL command from the command line to not prompt y/n?如何强制命令行中的 MySQL 命令不提示 y/n?
【发布时间】:2013-01-09 22:18:37
【问题描述】:

我有一个执行以下操作的 shell 脚本

mysql -uuser -ppass -e "DROP DATABASE IF EXISTS database"

但是,如果您确定要这样做 [Y/N],则会出现提示。我需要在脚本中使用它,所以有没有办法强制它执行?文档中的--force 选项谈到不会因错误而停止。

编辑:mysql 客户端实际上不会生成提示。事实证明我有 mysqladmin 客户端调用正在生成提示。

【问题讨论】:

  • 我相信提示来自mysqladmin客户端。我认为mysql 命令行客户端不会发出提示或等待按键继续。
  • 请发布正在回显的提示是或否的消息
  • spencer7593,你可能是对的。在将数据库与其余查询合并之前,我有 mysqladmin 调用删除数据库。我正在再次测试它。如果是这种情况,请删除此帖子。
  • 很好的侦探工作。 +1 给你!!! (对不起,超出了我的限制,我欠你一个 +1 !!!)

标签: mysql database


【解决方案1】:

很明显,shell 脚本正在等待 Y/N 响应,而不是 MySQL 客户端。

您应该可以通过复制/粘贴直接执行该行

mysql -uuser -ppass -e "DROP DATABASE IF EXISTS database"

在 Linux 命令提示符处。

如果您愿意,在此命令出现的位置,只需将 shell 脚本中的 Y/N 响应注释掉即可。

我的下一个建议是让您查看您的 my.cnf。

查看是否有 [mysql][client] 部分包含以下内容:

[mysql]
i-am-a-dummy
safe-updates

[client]
i-am-a-dummy
safe-updates

这些是真正的选择:请参阅 MySQL 文档中的 safe-updates and i-am-a-dummy

更新 2013-01-25 16:48 EDT

我的下一个猜测是操作系统。为什么???

如果您以root 身份登录Linux 或执行sudo,则您毫无疑问地有权执行DROP DATABASE IF EXISTS。在操作系统级别,mysqld 会尝试丢弃数据库的文件夹。

例如,如果datadir是/var/lib/mysql并且你执行DROp DATABASE IF EXISTS rolando;,mysqld会尝试运行rm -rf /var/lib/mysql/rolando

如果您不是rootsudo,则不是root,我希望操作系统会回显该消息。事实上,当我没有以 root 身份登录并尝试 service mysql stop 时,我已经看到来自操作系统的消息要求删除 PID 文件。

更新 2013-01-25 16:54 EDT

mysqladmin 也不会引起提示,除了密码。以下是它的所有选项:

[root@***]# mysqladmin --help
mysqladmin  Ver 8.42 Distrib 5.1.47, for redhat-linux-gnu on x86_64
Copyright 2000-2008 MySQL AB, 2008 Sun Microsystems, Inc.
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL license

Administration program for the mysqld daemon.
Usage: mysqladmin [OPTIONS] command command....
  -c, --count=#       Number of iterations to make. This works with -i
                      (--sleep) only.
  --debug-check       Check memory and open file usage at exit.
  --debug-info        Print some debug info at exit.
  -f, --force         Don't ask for confirmation on drop database; with
                      multiple commands, continue even if an error occurs.
  -C, --compress      Use compression in server/client protocol.
  --character-sets-dir=name
                      Directory for character set files.
  --default-character-set=name
                      Set the default character set.
  -?, --help          Display this help and exit.
  -h, --host=name     Connect to host.
  -b, --no-beep       Turn off beep on error.
  -p, --password[=name]
                      Password to use when connecting to server. If password is
                      not given it's asked from the tty.
  -P, --port=#        Port number to use for connection or 0 for default to, in
                      order of preference, my.cnf, $MYSQL_TCP_PORT,
                      /etc/services, built-in default (3306).
  --protocol=name     The protocol to use for connection (tcp, socket, pipe,
                      memory).
  -r, --relative      Show difference between current and previous values when
                      used with -i. Currently only works with extended-status.
  -O, --set-variable=name
                      Change the value of a variable. Please note that this
                      option is deprecated; you can set variables directly with
                      --variable-name=value.
  -s, --silent        Silently exit if one can't connect to server.
  -S, --socket=name   The socket file to use for connection.
  -i, --sleep=#       Execute commands repeatedly with a sleep between.
  --ssl               Enable SSL for connection (automatically enabled with
                      other flags). Disable with --skip-ssl.
  --ssl-ca=name       CA file in PEM format (check OpenSSL docs, implies
                      --ssl).
  --ssl-capath=name   CA directory (check OpenSSL docs, implies --ssl).
  --ssl-cert=name     X509 cert in PEM format (implies --ssl).
  --ssl-cipher=name   SSL cipher to use (implies --ssl).
  --ssl-key=name      X509 key in PEM format (implies --ssl).
  --ssl-verify-server-cert
                      Verify server's "Common Name" in its cert against
                      hostname used when connecting. This option is disabled by
                      default.
  -u, --user=name     User for login if not current user.
  -v, --verbose       Write more information.
  -V, --version       Output version information and exit.
  -E, --vertical      Print output vertically. Is similar to --relative, but
                      prints output vertically.
  -w, --wait[=#]      Wait and retry if connection is down.
  --connect_timeout=#
  --shutdown_timeout=#

嘿,我说得对

--force 提示 DROP DATABASE

好吧,我猜你找到了罪魁祸首。我今天学到了一些东西,因为我不使用mysqladmin 来删除数据库。

【讨论】:

  • 不,脚本不问。肯定是 MYSQL。
  • 你确定吗?我只是在命令提示符下测试了一下,它不问,即使数据库真的存在。
  • mysql 客户端是否配置为safe-updates(又名i-am-a-dummy)?那不是讽刺。这是一个真正的选择。只需运行mysql --help | grep dummy
  • @RolandoMySQLDBA,看来您的用户目录路径中可能有一个 .my.cnf 允许您在没有提示的情况下执行。对于这种情况,这不是标准的事情。
  • @JakeGould 我不使用安全更新。我仍然对该消息的来源感到好奇,因为在我的整个 MySQL DBA 职业生涯中,我从未遇到过数据库删除的提示。我的第一个嫌疑人是操作系统。
【解决方案2】:

可能必须将“是”输入命令。 This site 提供了如何做到这一点的想法。

yes | mysqladmin -u[username] -p[password] drop [database]

但这是this post 的另一个问题。

mysqladmin -u[username] -p[password] -f drop [database]

【讨论】:

    【解决方案3】:

    一般来说,您可以使用 -e 选项将任何查询从 shell 传递给 mysql。

    mysql -u username -ppassword -D dbname -e "DROP DATABASE" 
    

    或者您可以将密码存储在 my.cnf 中,但安全性较低。

    [client]
    host     = localhost
    user     = username.
    password = password
    socket   = /var/lib/mysql/mysql.sock
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-28
      • 2017-11-16
      • 2017-09-18
      • 2014-04-10
      • 2010-11-30
      • 2017-04-22
      相关资源
      最近更新 更多