【问题标题】:My bash request to MySQL database making syntax error. What I am doing wrong?我对 MySQL 数据库的 bash 请求导致语法错误。我做错了什么?
【发布时间】:2017-05-23 16:22:38
【问题描述】:

我正在尝试使用 bash 脚本对 mysql 数据库进行请求。 我找到了这个例子:bash script - select from database into variable

当我尝试mysql mysql -u $user -p

user@user:~$ mysql mysql -u $user -p 
Enter password: 
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 454
Server version: 5.7.18-0ubuntu0.17.04.1 (Ubuntu)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> exit
Bye

这是查找。 但是当我尝试mysql mysql -u $user -p $pwd

user@user:~$ mysql mysql -u $user -p $pwd
mysql  Ver 14.14 Distrib 5.7.18, for Linux (x86_64) using  EditLine wrapper
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Usage: mysql [OPTIONS] [database]
  -?, --help          Display this help and exit.
  -I, --help          Synonym for -?
  --auto-rehash       Enable automatic rehashing. One doesn't need to use
                      'rehash' to get table and field completion, but startup
                      and reconnecting may take a longer time. Disable with
                      --disable-auto-rehash.
                      (Defaults to on; use --skip-auto-rehash to disable.)
 .......................................

connect-expired-password          FALSE

我检查了$user$pwd 它们包含正确的用户名和密码。

我一定是做错了什么,但我不知道是什么。如何在 bash 脚本中向 MySQL 发出请求?为什么这个例子对我来说没有按预期工作? 你能帮忙吗?

【问题讨论】:

  • 请正确标记。 MySQL 不是 Oracle。语法不同。

标签: database bash


【解决方案1】:

来自man mysql

如果使用短选项形式 (-p),则不能 选项和密码之间有一个空格。

所以应该是-p$pwd,或者rather"-p${pwd}"

【讨论】:

  • 绝对引用。大括号在这里是可选的,因为没有任何非可变字符 after $pwd.
猜你喜欢
  • 2013-11-25
  • 1970-01-01
  • 2011-06-24
  • 1970-01-01
  • 1970-01-01
  • 2011-10-22
  • 1970-01-01
  • 1970-01-01
  • 2019-06-05
相关资源
最近更新 更多