作者热门文章
- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我有一个调用 mysql
命令行客户端的 shell 脚本,它看起来像这样:
$ cat ms
mysql --host=titanic --user=fred --password="foobar"
它工作正常:
$ ./ms
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 810
...
现在,我想将脚本保存在 git 存储库中,但没有用户和密码详细信息。所以,我想我会有一个 SECRET
文件,其中包含: --host=titanic --user=fred --password="foobar"
我不会添加到git 存储库,然后像这样更改 ms
脚本:
mysql $(cat SECRET)
不幸的是,它不起作用。运行时出现此错误:
$ ./ms
ERROR 1045 (28000): Access denied for user 'fred'@'example.com' (using password: YES)
我无法理解 - 当评估/扩展 $(cat SECRET)
时,它看起来与直接调用 mysql
完全一样。尽管如此,它还是行不通。如果我尝试直接在交互式 shell 中执行此操作,也会发生同样的情况:
$ mysql --host=titanic --user=fred --password="foobar"
工作正常,但下面没有:
$ cat SECRET
--host=titanic --user=fred --password="foobar"
$ mysql $(cat SECRET)
ERROR 1045 (28000): Access denied for user 'fred'@'example.com' (using password: YES)
$ echo mysql $(cat SECRET)
mysql --host=titanic --user=fred --password="foobar"
任何人都可以阐明这里发生了什么以及如何解决它?非常感谢。
最佳答案
将文件更改为:
--host=titanic --user=fred --password=foobar
引号不会根据命令或变量替换的结果进行处理,只会进行分词和文件名扩展。
但更好的解决方案可能是使用 option file ,例如我的 secret .cnf:
[mysql]
user=fred
password=foobar
host=titanic
然后运行 mysql 为:
mysql --defaults-file=mysecret.cnf
关于mysql - 如何将登录参数传递给mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17070666/
我是一名优秀的程序员,十分优秀!