简体   繁体   中英

Inserting user defined variable in mysql CREATE USER

I would like to create a shell script to run in docker CLI and create a MySQL user with the host IP passed as a command line variable. So with my script it would be ./create_user.sh 172.17.0.1

I tried starting with inserting variable only in the sql statements part and using something like this:

#!/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

This results in syntax error, along with some others i tried :

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

This is of course only the variable within the sql statements part of the script. Using a variable in the overall shell script and passing that into the sql bash is a problem that I have not even been able to come to yet.

Thanks in advance for any help!

UPDATED

I tried Raymond Nijland's solution and it worked for the sql variable part. However, trying to pass the variable value through the command line is still failing for the following script:

#!/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

with the output

$ ./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

I understad the usual approach is to run a separate .sql script file with the -e handle but unfortunately bind-mounting this file into the default mariadb container or creating a custom image are both not possible appraoches given the current requirements.

You should be able to generate dynamic SQL statements with CONCAT, PREPARE and execute them.

Query

SET @ip = '172.17.0.1';

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

PREPARE smtpCreateUser FROM @createUser;
EXECUTE smtpCreateUser;

You need to grab the variable from the shell before it can be used in the query. I think you are looking for something like (make sure to remove the quotes from 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM