【问题标题】:mysql_config_editor with dockermysql_config_editor 与 docker
【发布时间】:2016-09-14 16:05:09
【问题描述】:

您好,我正在尝试直接在我的 Dockerfile 中使用 mysql_config_editor 工具:

FROM mysql
RUN mysql_config_editor set --login-path=local --user=root --password

但是这个命令要求用户输入密码。知道不能直接在命令行设置密码,有没有办法直接从 Dockerfile 设置密码。

root@d80484a3177f:~# mysql_config_editor set --login-path=local --user=root --password                   
Enter password: 

root@d80484a3177f:~# mysql_config_editor set --login-path=local --user=root --password=root
mysql_config_editor: [ERROR] mysql_config_editor: option '--password' cannot take an argument

谢谢

【问题讨论】:

    标签: mysql bash docker dockerfile


    【解决方案1】:

    您可以使用expect 来执行此操作:

    这是你的 Dockerfile:

    FROM mysql:5.7
    
    RUN apt-get update && apt-get --no-install-recommends --no-install-suggests -y install expect
    
    ENV MYSQL_ROOT_PASSWORD "secure_root_password"
    
    ADD mysql_config.sh /root/mysql_config.sh
    RUN cd /root && ./mysql_config.sh $MYSQL_ROOT_PASSWORD
    

    这是 mysql_config.sh :

    #!/usr/bin/expect
    
    set pwd [lindex $argv 0];
    
    spawn mysql_config_editor set --login-path=local --host=localhost --user=root --password
    expect -nocase "Enter password:" {send "${pwd}\r"; interact}
    

    现在您可以直接连接到 MySQL 了:

    $ docker exec -it your-mysql-container mysql --login-path=local
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 6
    Server version: 5.7.18 MySQL Community Server (GPL)
    
    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>
    

    也许这不是最干净的方法,但它有效!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-22
      • 1970-01-01
      • 2023-03-07
      • 2017-07-03
      • 2019-07-22
      • 1970-01-01
      • 2017-12-03
      • 2019-09-27
      相关资源
      最近更新 更多