mysql - 如何强制命令行中的 MySQL 命令不提示 y/n?

标签 mysql database

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

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

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

编辑:mysql 客户端实际上不生成提示。原来我有生成提示的 mysqladmin 客户端调用。

最佳答案

很明显,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

这些是实物期权:参见 safe-updates and i-am-a-dummy在 MySQL 文档中

更新 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 确实提示删除数据库

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

关于mysql - 如何强制命令行中的 MySQL 命令不提示 y/n?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14530492/

相关文章:

mysql - 从 SQL 表中删除条目以及关系的正确方法

mysql - 在 SQL 中编写 CASE 语句以包含多列

database - 如何判断数据库中索引的类型?

asp.net - 最有效的方法...独特的随机字符串

database - 返回在 Mule 中创建的数据库记录的详细信息

python - 如何分离数据框中不同列中的数据分组?

java - JPA或Hibernate生成一个(非主键)列值,不是从1开始

PHP PDO 向所有查询添加过滤器

php 不会插入到 mysql 中。没有错误

mongodb - 哪种 NoSQL DB 最适合 OLTP 金融系统?