1

I need to change the temporary root password that is created when the MySQL daemon is started. The problem is that the temporary password has some weird characters (e.g. like a left/right parenthesis) that needs to be escaped. Now, there are several posts (here, here, here) on how to escape characters, in general, but this post is in the context of using a bash script to change the temporary MySQL root password that may have special characters.

Currently, my script looks like the following.

function startMysql {
    sudo service mysqld start
    echo "started mysql"

    export PW=$(grep 'temporary password' /var/log/mysqld.log | awk '{print $11}')
    # export PASS=\'$PW\'
    echo "temporary password is $PW"
    mysqladmin -u root -p$PW password aaBB@@cc1122
    # the following doesn't work either
    # mysqladmin -u root -p$PASS password aaBB@@cc1122
    echo "changed mysql password"
}

Note that the temporary password may look like the following.

  • BYkc*),ZM3-_

If I type in the following on the terminal, it works.

mysqladmin -u root -p'BYkc*),ZM3-_' password aaBB@@cc1122

But inside the script, it fails. Here are some ways that I have tried to put single quotes around $PW without success.

  • mysqladmin -u root -p"'$PW'" password aaBB@@cc1122
  • mysqladmin -u root -p\''$PW'\' password aaBB@@cc1122
  • mysqladmin -u root -p"$PW" password aaBB@@cc1122
  • mysqladmin -u root -p"\"$PW\"" password aaBB@@cc1122

Any ideas on what I am doing wrong?

3
  • What does echo "temporary password is $PW" give you? mysqladmin -u root -p"$PW" password aaBB@@cc1122 should work just fine. Commented Feb 22, 2017 at 5:37
  • How does it fail? Commented Feb 22, 2017 at 10:02
  • the echo to the console shows as correct. but I still get authentication failure as if I passed in the wrong password. Commented Feb 23, 2017 at 3:03

1 Answer 1

2

It does work for me using double-quotes:

password=$(grep 'temporary password' /var/log/mysqld.log | awk '{print $11}')
mysqladmin --user=root --password="$password" password aaBB@@cc1122
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.