【问题标题】:Inserting user defined variable in mysql CREATE USER在mysql CREATE USER中插入用户定义的变量
【发布时间】:2017-10-26 15:47:56
【问题描述】:

我想创建一个 shell 脚本以在 docker CLI 中运行并创建一个 MySQL 用户,并将主机 IP 作为命令行变量传递。 所以我的脚本是./create_user.sh 172.17.0.1

我尝试从仅在 sql 语句部分插入变量开始,并使用如下内容:

#!/bin/sh
docker exec -i atb-mariadb bash <<'EOF' 
mysql -uroot -pmypass
set @ip='172.17.0.1';
CREATE USER 'exporter'@@ip IDENTIFIED BY 'mypass';
GRANT PROCESS, REPLICATION CLIENT ON *.* TO 'exporter'@'172.17.0.1';
GRANT SELECT ON performance_schema.* TO 'exporter'@'172.17.0.1';
SELECT user, host FROM mysql.user;
exit
EOF

这会导致语法错误,以及我尝试过的其他一些错误:

CREATE USER CONCAT_WS('exporter','@',@ip) IDENTIFIED BY 'mypass';
CREATE USER 'exporter'@CONCAT_WS('',@ip) IDENTIFIED BY 'mypass';

这当然只是脚本中 sql 语句部分的变量。在整个 shell 脚本中使用一个变量并将其传递到 sql bash 是一个我什至无法解决的问题。

提前感谢您的帮助!

更新

我尝试了 Raymond Nijland 的解决方案,它适用于 sql 变量部分。但是,对于以下脚本,尝试通过命令行传递变量值仍然失败:

#!/bin/sh
echo script received $1
docker exec -e ipa=$1 -i atb-mariadb bash <<'EOF'
echo exec received $ipa
mysql -uroot -pmypass -e "
SET @ip='${ipa}';
SET @createUser = 'CONCAT("CREATE USER exporter@",@ip,"IDENTIFIED BY'mypass'")';
PREPARE smtpCreateUser FROM @createUser;
EXECUTE smtpCreateUser;";
exit
EOF

输出

$ ./create_user.sh 172.18.0.5
script received 172.18.0.5
exec received 172.18.0.5
mysql  Ver 15.1 Distrib 10.2.9-MariaDB, for debian-linux-gnu (x86_64) using readline 5.2
Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Usage: mysql [OPTIONS] [database]

Default options are read from the following files in the given order:
/etc/my.cnf /etc/mysql/my.cnf ~/.my.cnf
The following groups are read: mysql client client-server client-mariadb
The following options may be given as the first argument:
--print-defaults        Print the program argument list and exit.
......and so on

我了解通常的方法是使用 -e 句柄运行单独的 .sql 脚本文件,但不幸的是,鉴于当前要求,无法将此文件绑定到默认 mariadb 容器或创建自定义映像。

【问题讨论】:

  • 您是否尝试过在 mysql workbench 之类的编辑器中运行查询?错误输出是什么?

标签: mysql bash shell docker


【解决方案1】:

您应该能够使用 CONCAT、PREPARE 生成动态 SQL 语句并执行它们。

查询

SET @ip = '172.17.0.1';

SET @createUser = CONCAT(
            "CREATE USER exporter@",@ip, " IDENTIFIED BY 'mypass'" 
          );

PREPARE smtpCreateUser FROM @createUser;
EXECUTE smtpCreateUser;

【讨论】:

    【解决方案2】:

    您需要先从 shell 中获取变量,然后才能在查询中使用它。我认为您正在寻找类似的东西(确保从 EOF 中删除引号):

    #!/bin/sh
    docker exec -i atb-mariadb bash <<EOF
    ip="${1:-127.0.0.1}"
    echo mysql -uroot -pmypass
    echo CREATE USER 'exporter'@'${ip}' IDENTIFIED BY 'mypass';
    GRANT PROCESS, REPLICATION CLIENT ON *.* TO 'exporter'@'172.17.0.1';
    GRANT SELECT ON performance_schema.* TO 'exporter'@'172.17.0.1';
    SELECT user, host FROM mysql.user;
    exit
    EOF
    

    【讨论】:

    • 我试过了,但我得到了:$ ./create_user.sh bash: line 4: GRANT: command not found bash: line 5: GRANT: command not found bash: line 6: SELECT: command not found mysql -uroot -pmypass CREATE USER exporter@ IDENTIFIED BY mypass
    猜你喜欢
    • 2017-09-12
    • 2020-08-18
    • 2019-09-30
    • 1970-01-01
    • 1970-01-01
    • 2013-04-25
    • 2011-11-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多