Home > front end >  Running a single mysql command with the 'runuser' command
Running a single mysql command with the 'runuser' command

Time:12-12

For some strange reason, I can't find a way to make the runuser command work. I know it is possible to achieve this with sudo -u mysql mysql -e "$DB_SETUP but since I want to do this inside a script that already runs with sudo I find this not very pretty.

Here is what I am trying to do:

DB_SETUP="CREATE USER IF NOT EXISTS $DB_USER@$BASEURL IDENTIFIED BY '$DB_PASSWORD';CREATE DATABASE IF NOT EXISTS $DB_NAME;GRANT ALL PRIVILEGES ON $DB_NAME.* TO $DB_USER@$BASEURL IDENTIFIED BY '$DB_PASSWORD';FLUSH PRIVILEGES;"
sudo runuser -u mysql "mysql -e \"$DB_SETUP\"" # does not work

It gives me this error:

runuser: failed to execute mysql -e "CREATE USER IF NOT EXISTS db_user@baseurl IDENTIFIED BY 'db_password';CREATE DATABASE IF NOT EXISTS db_name;GRANT ALL PRIVILEGES ON db_name.* TO db_user@baseurl IDENTIFIED BY 'password';": No such file or directory

CodePudding user response:

As commented above, I got it working with:

sudo runuser -u mysql mysql <<< $DB_SETUP

No quotation marks at all!

  • Related