4

I am trying to change user password using a shell script as below

#!/bin/bash
mysql.server start
mysql -u root << EOF
  SET PASSWORD FOR root@'localhost' = PASSWORD(‘admin’);
EOF

But I am getting below error:-

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 '‘admin’)' at line 1

Instead of SET PASSSWORD command if use something else will work like if I use 'create database databasename' will works.

.

Update1:

If I use "admin" or 'admin' instead of ‘admin’, I got below error.

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 '“admin2”)' at line 1

.

Update2:

When using -e flag I got below error

./mysql.sh: line 3: syntax error near unexpected token (' ./mysql.sh: line 3:mysql -u root -padmin -e “SET PASSWORD FOR root@'localhost' = PASSWORD(‘admin2’);”'

2
  • ‘admin’ can you wrap in these quotes? Try "admin" or 'admin' instead Commented Feb 14, 2017 at 10:30
  • Thanks for replying, I think issue was due to editor :) Commented Feb 14, 2017 at 10:45

2 Answers 2

15

use mysql -e instead:

#!/bin/bash
mysql.server start
mysql -u root -e "SET PASSWORD FOR root@'localhost' = PASSWORD('admin');"
Sign up to request clarification or add additional context in comments.

1 Comment

I think issue was due to some editor, I wrote same script using another editor and vola your and mine script both will work. Thanks for replying.
1

For anyone trying above and not getting it to work. I ran the below in my terminal and it worked! (Remember to set the password you want ;) )

mysql -u root -e "SET PASSWORD FOR root@localhost = PASSWORD('mypassword')";

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.