如何将密码从文件传递给 mysql 命令? [英] How to pass password from file to mysql command?
问题描述
我有一个 shell 脚本,它使用来自外部文件的一个参数调用 mysql 命令,它看起来像这样(我也在其他资源中看到了这个例子):
I have a shell script which calls the mysql command with one parameter from external file, it looks like this (also I saw this example in other resources):
mysql --user=root --password=`cat /root/.mysql`
有点不行:
无法连接到 MySQL 服务器:用户 'root'@'localhost' 的访问被拒绝(使用密码:YES).
Failed to connect to MySQL server: Access denied for user 'root'@'localhost' (using password: YES).
我尝试了不同的报价但没有成功.如何通过?
I tried different quotes without success. How to pass it?
更新1:发现我可以传递没有空格符号的密码.问题在于此,我的 root pass 包含空格.
UPDATE 1: Found that I can pass password without space symbol. The problem in this, my root pass contains spaces.
推荐答案
最后这条线工作了:
mysql --user=root --password="$(cat /root/.mysql)"
或:
mysql --user=root --password="$(< /root/.mysql)"
root 密码必须不带引号:bla bla bla
Root password must be without quotes: bla bla bla
如果您的密码不包含空格,您可以使用:
If your password not contains spaces you can use:
mysql --user=root --password=`cat /root/.mysql`
这篇关于如何将密码从文件传递给 mysql 命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!