Command not found running MariaDB CREATE or ALTER command

Running a packer script with WordPress and MariaDB on an EC2 Instance.

Trying to run sql commands from a shell provisioner or external script.

Keep getting command not found for CREATE or ALTER statements.

Latest attempt - packer snippet:

    {
            "type": "shell",
            "environment_vars": [
                    "db_user={{user `db_user`}}",
                    "db_password={{user `db_password`}}",
                    "db_user={{user `db_password`}}"
            ],
            "scripts": [
            "sql.sh"
            ],
            "pause_before": "4s"
    },

External sql.sh script:

#!/bin/sh

mysql -u root

CREATE USER $1@'localhost' IDENTIFIED BY $2;

ALTER USER 'root'@'localhost' IDENTIFIED BY $3;

exit

What am I doing wrong? Thanks

You don’t pass your variables to the script, so the script cannot interpolate $1, $2 and $3. Either you pass the variables to the script in their correct position or you can use your environment variables in the script.

I thought that was the purpose of the environment variables?

When I pass the variables in the normal way, Packer throws an error.

Additionally, I get the same error - Command not found - when I embed the SQL statement in a Packer inline provisioner.

Environment variables are environment variables and treated as such. And if they should be used, maybe uppercase would be the better choice.
But no matter if upper or lowercase the $1 is a positional parameter meaning first parameter sent to the script. How should Packer know that db_user should be the first in line?

You script is wrong, too. You should pass you commands to the mysql client.

mysql -u root <<< 'CREATE...'
mysql -u root <<< 'UPDATE...'

Or using a heredoc

mysql -u root <<<EOF
CREATE... 
UPDATE... 
EOF

If command not found is your problem, maybe the mysql binary isn’t installed at all, or - due to your wrong script syntax - Packer wants to execute CREATE on the shell, not the mysql client.

Some more output/ logs/ debug would be great.

The script is not wrong outside of Packer, which is the reason for this post. It looks like I need to manually add the end of line (EOF) in Packer as you have done above to resolve the problem. I will try that.

As for the environment variables, I do not actually want to use those as that is a security problem, but many posts are telling people to do that and indicating that structure creates positional parameters, which apparently does not. Will be adding this to my security class.

I was trying to make the code functional, not adhere to coding standards.