This seems like it should be simple and I swear this code has worked for months but it's not working now. I'm sure I'm just overly tired but I would appreciate a knowing nudge.
# create random password
PASSWDDB="$(openssl rand -base64 12)"
# replace "-" with "_" for database username
MAINDB=${USER_NAME//[^a-zA-Z0-9]/_}
# create database and user
mysql -e "CREATE DATABASE $MAINDB"
mysql -e "GRANT ALL PRIVILEGES ON $MAINDB.* TO $MAINDB@localhost IDENTIFIED BY '$PASSWDDB!'"
MAINDB is supplied previously in the script. What I end up with is a database, a user, and proper permissions and the user has a password - it's just not the password defined in $PASSWORDDB.
MariaDB 10.0.22 Ubuntu 14.04
This will work for you.
This is what I use: https://raw.githubusercontent.com/saadismail/useful-bash-scripts/master/db.sh
In your case you can use this:
I have created a one which you may find useful:
Usage:
For more details:
http://www.blog.magepsycho.com/bash-script-create-mysql-database-user-optional-password/
You could use heredoc to improve the lisibility of your SQL: