2

I need a one-liner to provision users in my databases. I'm running MySQL 5.6. Here is what I'm setting the password as a variable so that I can pass it dynamically (obviously it won't always be 'password').

mysql_password="password"
mysql -u ted -e "SET PASSWORD FOR 'ted'@'localhost' = PASSWORD($mysql_password);"

I'm getting the following error when I run this:

ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'password)' at line 1

What is wrong with the statement? Can it be corrected or is another solution available so I accomplish this via Bash?

4
  • 2
    It's a string inside the SET PASSWORD statement, and therefore needs to be surrounded with single quotes when you pass it in, just like 'ted' is. Be careful though, this is not injection-safe. Commented Jan 19, 2017 at 19:12
  • See also stackoverflow.com/questions/4383135/… Commented Jan 19, 2017 at 19:15
  • But single quotes doesn't imterpret variables in bash. Commented Jan 19, 2017 at 19:23
  • The outer string is still double quoted. You just need the single quotes inside. Example var=astring; echo "Variable is '$var'" will print "Variable is 'astring'" in bash. Commented Jan 19, 2017 at 19:29

1 Answer 1

2

You need to surround the variable by single quotes:

mysql -u root -e "SET PASSWORD FOR 'ted'@'localhost' = PASSWORD('$mysql_password');"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.