Jane Wayne
Jane Wayne

Reputation: 8855

How do I change the temporary root MySQL password using a bash shell script?

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.

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.

Any ideas on what I am doing wrong?

Upvotes: 1

Views: 1980

Answers (1)

BenMorel
BenMorel

Reputation: 36554

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

Upvotes: 2

Related Questions