Since your database name contains a hyphen -
, you will need to backtick-quote it. In order to do that successfully inside a double-quoted Bash string, to surround a variable for expansion, you will need to backslash-escape the backticks.
Later, you reuse the variable $MYSQL_DB
as a username. In that context, it must be single-quoted as a string like '$MYSQL_DB'
.
mysql -u root -p"$MYSQL_ROOT_PASS" -e "CREATE DATABASE IF NOT EXISTS \`$MYSQL_DB\`; GRANT ALL PRIVILEGES ON \`$MYSQL_DB\`.* TO '$MYSQL_DB'@localhost;"
#------------------------------------------------------------------^^^^^^^^^^^^^^^^^-------------------------------------------^^^^^^^^^^^^
When testing this execution however, I receive:
ERROR 1470 (HY000) at line 1: String 'losk_net-simple_ru' is too long for user name (should be no longer than 16)
You must therefore choose a different username than the database name if you encounter a similar error.
For reference on what types of quotes are used on MySQL strings and identifiers, refer to this question. Although it is PHP-related, most of it is still relevant here.
In general, for this situation, I would recommend against including a -
in the database name. You will always have to backtick-quote it in every context, not just via Bash. Stick to the set of [0-9,a-z,A-Z$_]
for identifiers, to save yourself future headaches.