I am trying to do this as part of a startup script (that runs as root) on an EC2 instance
# mysqladmin -u root password mygreatnewpassword
To debug this I echoed the entire command to the log and it's correct. I did a 'ps ax' before the command in the script to be sure mysql was running. I tried it with double and single quotes around the password (shouldn't have mattered and didn't). No matter what I try, in the script it gets error
mysqladmin: connect to server at 'localhost' failed
error: 'Access denied for user 'root'@'localhost' (using password: NO)'
but when I copy and run the exact same line from the script in a shell it works. And I've verified that there is no root password before the command runs (in either case).
It can't be that I need to use -p, even though that's what the error seems like, because I don't need that when I run it from a shell.
I'm baffled.
And before anyone comments that this is insecure, it's not. The password is stored on a private EBS volume that's mounted as part of the startup and pulled out of a file on that volume and put into the startup shell's environment and then used in the script (and not echoed into syslog). Then the EBS volume is unmounted. Once that startup script ends that password is nowhere except inside mysql.
Update: I just tried this instead (found in the 5.1 MYSQL manual in 2.12.2
mysql -u root <<EOF UPDATE mysql.user SET Password = PASSWORD("$MYSQL_PWD") WHERE User = 'root'; FLUSH PRIVILEGES; EOF
Didn't work either, similar error and running
mysql -u root
from a shell gets me connected just fine (and I can enter the UPDATE and FLUSH cmds) w/o error.
I'm starting to think that mysql and mysqladmin are doing something funky like reading from a tty that is causing them to fail when run from within shell scripts. That will make it difficult to have my startup script initialize things.