Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Mysql change password - bash script 1

Status
Not open for further replies.

Borvik

Programmer
Jan 2, 2002
1,392
US
Hello,

I'm trying to write a fairly simple bash script that installs mysql via yum (this is FC9) and sets the root password upon install.

Apparently the password is not being set properly as after I run the password script (I separated it from the install for testing purposes), I cannot log in.

Here is what I have for the password change script:
Code:
#!/bin/bash
mysqlPassword=""
mysqlPasswordRetype="s"
while [[ "$mysqlPassword" = "" && "$mysqlPassword" != "$mysqlPasswordRetype" ]]; do
  echo -n "Please enter the desired mysql root password: "
  stty -echo
  read mysqlPassword
  echo
  echo -n "Retype password: "
  read mysqlPasswordRetype
  stty echo
  echo
  if [ "$mysqlPassword" != "$mysqlPasswordRetype" ]; then
    echo "Passwords do not match!"
  fi
done

/usr/bin/mysqladmin -u root password '${mysqlPassword}'

I have also tried it with the following line
Code:
/usr/bin/mysqladmin -u root password '$mysqlPassword'

Once mysql is installed, I start the service using: "service mysqld start" and then run the above script. After the script completes I then proceed to login using "mysql -p" and entering the same password as I used during the script. It fails with a response saying that Access denied for user 'root'@'localhost' (using password: YES)'.

Can anybody see what might be going wrong?

Thanks.
 
Lose the single quotes on the final line. They are preventing bash from interpolating the variable.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Gotcha - that makes sense.

But then what if the variable holds characters that need the single quotes? Should I then add the single quotes to the beginning and end of the variable?

I should probably check for single quotes within the password as well and escape them if need be.
 
If you have a single quote in the password, you will need to escape it with \ but I'm not sure how you'd identify this and change it in bash - a call out to sed or awk maybe? (I normally use Perl for all my scripting, where this kind of thing is easy and you don't have to keep shelling out to assorted commands to make up for the deficiencies in the language).

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Thanks a ton stevexff! That single quote preventing the variable from being parsed allowed me to get it working for the most part.

This will even escape quotes without using sed:
Code:
mysqlPassword=${mysqlPassword//\'/\\\'}

Now I just have to get everything to play nicely with the command - (currently even escaping single quotes is causing issues - now I don't even know my password, time to start over).

Thanks again stevexff.
 
Rather than continually screwing up your password, prefix the last line with echo while you are testing - then you can see exactly what command will be issued without actually running it. Once you are happy with it, remove the echo.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Yea, I started doing that.

Apparently there is a difference when calling mysqladmin via command line (manually typing it in) vs a bash script.

From the command line the single quotes around the text 'test' work just fine and results in a password of test, but when the mysqlPassword variable in the script contains 'test' (including the quotes, an echo results in the same command line as I would have typed in), I end up with a password of 'test' (including the quotes).

Perhaps I do not need to escape or quote anything on that call in the script - that's what I'll test next once I have everything re-installed.
 
Yup, that was indeed the case.

I changed the read lines to "read -r mysqlPassword" to allow backslashed to be entered rather than treated as escape characters.

So the final script looks like this:
Code:
#!/bin/bash
mysqlPassword=""
mysqlPasswordRetype="s"
while [[ "$mysqlPassword" = "" && "$mysqlPassword" != "$mysqlPasswordRetype" ]]; do
  echo -n "Please enter the desired mysql root password: "
  stty -echo
  read -r mysqlPassword
  echo
  echo -n "Retype password: "
  read -r mysqlPasswordRetype
  stty echo
  echo
  if [ "$mysqlPassword" != "$mysqlPasswordRetype" ]; then
    echo "Passwords do not match!"
  fi
done

/usr/bin/mysqladmin -u root password $mysqlPassword

I've tested it with passwords such as: test, t'est, t\est, and t\'est. All of those passwords worked when I tried logging into the mysql client.

Cheers!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top