我对MySQL数据库的bash请求发出语法错误。我做错了什么?

时间:2017-05-23 16:22:39

标签: database bash

我正在尝试使用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发出请求?为什么这个例子对我不起作用? 你能帮忙吗?

1 个答案:

答案 0 :(得分:3)

来自man mysql

  

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

因此它应该是-p$pwdrather"-p${pwd}"