Linux - Docker MySQL镜像 - 创建MYSQL_USER

时间:2016-03-26 21:37:56

标签: mysql linux image bash docker

我是docker容器的新手,我正致力于自动化一些MySQL任务。通过使用bash脚本,我能够自动创建和转储数据库,但现在我正在尝试创建新用户从命令行获取输入,但它无法正常工作。这是我的剧本:

sudo docker run -it --link testing1:mysql --rm mysql/mysql-server:5.7.11 sh -c 'exec mysql -h"$MYSQL_PORT_3306_TCP_ADDR" -P"$MYSQL_PORT_3306_TCP_PORT" -uroot -p"$MYSQL_ENV_MYSQL_ROOT_PASSWORD" -e"CREATE USER 'docker'@'%' IDENTIFIED BY 'tester'"'

我收到的错误信息是:

+ EXPECTED_ARGS=2
+ E_BADARGS=65
++ which mysql
+ MYSQL=/usr/bin/mysql

+ sudo docker run -it --link testing1:mysql --rm mysql/mysql-server:5.7.11 sh -c 'exec mysql -h"$MYSQL_PORT_3306_TCP_ADDR" -P"$MYSQL_PORT_3306_TCP_PORT" -uroot -p"$MYSQL_ENV_MYSQL_ROOT_PASSWORD"

-e"CREATE USER docker@% IDENTIFIED BY tester"'
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '% IDENTIFIED BY tester' at line 1

3 个答案:

答案 0 :(得分:0)

您需要在引号中附上%(以允许来自任何IP的连接)。

CREATE USER docker@'%' IDENTIFIED BY tester

答案 1 :(得分:0)

您可以将此图片用作模板。 https://github.com/kliewkliew/mysql-adventureworks

我用过 mysql -e'GRANT ALL ON *.* TO user@"%" IDENTIFIED BY "password";'

答案 2 :(得分:0)

The solution was to set the ENV variables to get the user input like like this:

SOURCE="'%'"
USER="'$1'"
PASSWORD="'$2'"


 and then this:     docker exec -it ctb /usr/bin/mysql -h 127.0.0.1 -uroot -pmario123 -e "GRANT USAGE ON *.* TO ${USER}@${SOURCE} IDENTIFIED BY ${PASSWORD};"