13

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

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

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

编辑:mysql客户端实际上不会生成提示。事实证明,我mysqladmin接到了正在生成提示的客户电话。

4

3 回答 3

20

很明显,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 文档中的安全更新和 i-am-a-dummy

更新 2013-01-25 16:48 EDT

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

如果rootsudoDROP DATABASE IF EXISTS. 在操作系统级别,mysqld 会尝试丢弃数据库的文件夹。

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

if you are not root or sudo'd as root, I would expect the OS to echo that message. In fact, I have seen a message from the OS ask to delete a PID file when I was not logged in as root and attempted service mysql stop.

UPDATE 2013-01-25 16:54 EDT

mysqladmin does not cause prompting either, except for passwords. Here are all its options:

[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=#

HEY, I STAND CORRECTED

--force does prompt for DROP DATABASE

OK I guess you located the culprit. I learned something today because I do not use mysqladmin to drop databases.

于 2013-01-25T21:18:52.173 回答
9

Might have to pipe the 'yes' into the command. This site offers an idea on how to do this.

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

But here is another wrinkle via this post.

mysqladmin -u[username] -p[password] -f drop [database]
于 2013-01-25T21:22:46.857 回答
1

In general, you can pass any query to mysql from shell with -e option.

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

Or you can Store your password in my.cnf but its less secure.

[client]
host     = localhost
user     = username.
password = password
socket   = /var/lib/mysql/mysql.sock
于 2013-01-25T21:30:26.620 回答