如何通过 shell 脚本执行远程 MySQL 查询而不在我的 shell 脚本中输入开放文本密码?

How can I execute remote MySQL queries via shell script without putting open text password in my shell script?

我不想在 shell 脚本中使用开放文本密码执行查询。

#!/bin/bash
mysql -uuser -hremotehost -pmypassword -e "update my_table set what_time = NOW()";

有没有办法将 mypassword 放入我的本地 /etc/my.cnf 文件或其他文件中以使其远离打开的文本命令行?

假设我的 remotehost 不接受 mysql 的无密码。

更新的答案

创建 ~/.my.cnf 文件并将以下内容放入其中:

[foo]
user=user
password=mypassword
host=remotehost

[bar]
user=user2
password=some-other-password
host=127.0.0.1

然后调用 mysql --login-path=foo ... 连接到本地主机的 remotehostmysql --login-path=bar ...

正如在 dba 中所写,可以按如下方式配置 my.cnf 文件:

[clienthost1]   # Note: client + host1
user=user
password=mypassword
host=remotehost

和运行它是:

mysql --defaults-group-suffix=host1

感谢Derek Downey answer at DBA

另一种选择是使用 mysql_config_editor 并将数据存储在加密文件中。

示例:

mysql_config_editor set --user=user --password --host=remotehost
Enter password: