【问题标题】:How can I execute remote MySQL queries via shell script without putting open text password in my shell script?如何通过 shell 脚本执行远程 MySQL 查询,而不在我的 shell 脚本中输入开放文本密码?
【发布时间】:2016-04-19 14:58:19
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: mysql bash shell my.cnf


    【解决方案1】:

    正如dba 所写,可以将您的my.cnf 文件配置如下:

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

    然后运行它:

    mysql --defaults-group-suffix=host1
    

    感谢Derek Downey answer at DBA

    【讨论】:

    • 有趣。 host1 是否可配置?例如,我可以使用clientmy_host_name 吗?然后运行mysql --defaults-group-suffix=my_host_name?
    【解决方案2】:

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

    例子:

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

    【讨论】:

      【解决方案3】:

      更新答案

      创建~/.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 ...

      【讨论】:

      • 这也适用于远程主机吗?如果我有一个本地 mysql 实例和一个远程 mysql 实例怎么办?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-05-21
      • 2013-04-17
      • 2014-10-06
      • 2020-05-31
      • 1970-01-01
      • 1970-01-01
      • 2018-02-19
      相关资源
      最近更新 更多